[c]
int read_cmd(byte *buf, int *size)
{
int len;
if (read_exact(buf, 2) != 2)
return(-1);
len = (buf[0] << 8) | buf[1];
if (len > *size) {
buf = (byte *) realloc(buf, len);
if (buf == NULL)
return -1;
*size = len;
}
return read_exact(buf, len);
}
[/c]
if the size of binary data is more than the size of the buffer then data is
reallocated, but the pointer in the main function doesn't change.
I think it should be something like this:
[c]
int read_cmd(byte **buf_ptr, int *size)
{
int len;
char *buf = *buf_ptr;;
if (read_exact(buf, 2) != 2)
return -1;
len = (buf[0] << 8) | buf[1];
if (len > *size) {
buf = (byte *) realloc(buf, len);
if (buf == NULL)
return -1;
*buf_ptr = buf;
*size = len;
}
return read_exact(buf, len);
}
[/c]
The call of read_cmd in the main function should be changed into
[c]
while (read_cmd(&buf, &size) > 0) {
[/c]
Besides the code doesn't work properly in the system with the char defined
as signed type.
Hope this information will be helpful for someone.
--
Mikl
--
View this message in context: http://www.nabble.com/Bug-in-trapexit-article-%22How-to-use-ei-...%22-tp20516290p20516290.html
Sent from the Erlang Questions mailing list archive at Nabble.com.
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions
Serge
_______________________________________________
Serge Aleynikov-2 wrote:
>
> Thanks for pointing it out. This code was more-or-less a quick&dirty
> copy/paste from
> http://www.erlang.org/doc/tutorial/erl_interface.html#5.2 that suffers
> from a buffer overrun issue. I posted the corrections that you found.
>
It seems that you don't update buf_ptr in the new version of read_cmd
function.
Anyway, thanks for this article. It is very helpful and clear.
Mikl
--
View this message in context: http://www.nabble.com/Bug-in-trapexit-article-%22How-to-use-ei-...%22-tp20516290p20534211.html
Sent from the Erlang Questions mailing list archive at Nabble.com.
_______________________________________________
Note that while this code is quite primitive, where it's purpose is to
show a simple concept, if you need a more convenient C++ wrapper around
ei, you can look at ei++ in this project:
http://code.google.com/p/erlexec/source/browse/branches/auto/c_src/ei%2B%2B.h
Currently ei::Serializer only has support for marshaling basic types
(int, double, string, atom), but it's more convenient for a C++ user
then plain ei.
Serge
Mikl Kurkov wrote:
>
> Serge Aleynikov-2 wrote:
>> Thanks for pointing it out. This code was more-or-less a quick&dirty
>> copy/paste from
>> http://www.erlang.org/doc/tutorial/erl_interface.html#5.2 that suffers
>> from a buffer overrun issue. I posted the corrections that you found.
>>
>
> It seems that you don't update buf_ptr in the new version of read_cmd
> function.
> Anyway, thanks for this article. It is very helpful and clear.
>
> Mikl
>
_______________________________________________