Bug in trapexit article "How to use ei ..."

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Bug in trapexit article "How to use ei ..."

Mikl Kurkov
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
Reply | Threaded
Open this post in threaded view
|

Re: Bug in trapexit article "How to use ei ..."

Serge Aleynikov-2
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 wrote:

> 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

_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Bug in trapexit article "How to use ei ..."

Mikl Kurkov
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 | Threaded
Open this post in threaded view
|

Bug in trapexit article "How to use ei ..."

Mikl Kurkov-2
In reply to this post by Serge Aleynikov-2


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.



Reply | Threaded
Open this post in threaded view
|

Re: Bug in trapexit article "How to use ei ..."

Serge Aleynikov-2
In reply to this post by Mikl Kurkov
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
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions