[erlang-questions] Bug in trapexit article "How to use ei ..."

0 views
Skip to first unread message

Mikl Kurkov

unread,
Nov 15, 2008, 10:03:37 AM11/15/08
to erlang-q...@erlang.org

It seems that the code in the trapexit article
http://www.trapexit.org/How_to_use_ei_to_marshal_binary_terms_in_port_programs
has some bugs that I ran into.
In the next code

[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 Aleynikov

unread,
Nov 16, 2008, 10:39:27 PM11/16/08
to Mikl Kurkov, erlang-q...@erlang.org
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.

Serge

_______________________________________________

Mikl Kurkov

unread,
Nov 17, 2008, 12:58:41 AM11/17/08
to erlang-q...@erlang.org

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.

_______________________________________________

Serge Aleynikov

unread,
Nov 17, 2008, 10:01:38 PM11/17/08
to Mikl Kurkov, Erlang Users' List
Thanks. Not bad for the Sun 10pm code review. ;-)

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
>

_______________________________________________

Reply all
Reply to author
Forward
0 new messages