[erlang-questions] binary_to_term

Kostis Sagonas kostis@REDACTED
Wed Jun 10 18:04:01 CEST 2009


Claes Wikstrom wrote:
> 
> 1> B = term_to_binary(18).
> <<131,97,18>>
> 2> B2 = <<B/binary, B/binary>>.
> <<131,97,18,131,97,18>>
> 3> binary_to_term(B2).
> 18
> 
> 
> 
> opinions anyone ?
> 
> I for one don't like the above.

You are so kind.  I personally think it is terrible!

The issue has been discussed before, both in meetings and in this 
mailing list, and my understanding is that there are actually 
applications out there which depend on this "feature" (i.e., 
binary_to_term/1 succeeding when there is a prefix of the binary which 
can be turned into some Erlang term).

This means that you can take some binary and add anything to its end and 
binary_to_term/1 will still succeed:

   1> B = term_to_binary(42).
   <<131,97,42>>
   2> binary_to_term(<<B/binary,0,0,0>>).
   42

I have trouble appreciating why people should not get what they deserve 
for relying on this property, but I am sure you've heard the arguments 
for backwards compatibility before...

But being in academia, I can perhaps afford the luxury to not care much 
about these things.  I definitely vote for changing it and have it throw 
an exception instead.

Kostis


More information about the erlang-questions mailing list