[erlang-questions] binary_to_term

Michael Radford <>
Wed Jun 10 18:58:31 CEST 2009

Kostis Sagonas writes:
> 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).

I personally don't see what's so terrible about this behavior.

The external term format has to be self-delimiting anyway for safety.
All types are prefixed with a header, including a length if
variable-length, so it's not possible for a prefix of a valid encoded
term to also be a valid encoded term.

So behaving "correctly" in this case would just mean a check for extra
bytes after the term has already been successfully decoded -- a pop quiz
to force the programmer to construct a binary with the exact length of
the term data.

The only apparent benefit would be that we all get to feel more
comfortable that binary_to_term is the exact mathematical inverse of
term_to_binary.  But there may be a real cost in forcing the
construction of an extra binary.


More information about the erlang-questions mailing list