[erlang-patches] Patch for term_to_binary() stack overflow

Jan Lehnardt <>
Tue Mar 10 23:40:44 CET 2009

Dear OTP Team,

https://issues.apache.org/jira/browse/COUCHDB-275 describes an issue
with Erlang's term_to_binary() BIF and includes a patch. The core of the
issue is that term_to_binary() is implemented in a recursive C function
that blows the stack when converting deeply nested term structures. We
see this when people try to read deeply nested JSON structures that we
convert from Erlang terms to binaries representing JSON for delivery.

The easiest way to reproduce this is

term_to_binary(lists:foldl(fun(E,A) -> [E, A] end, [], lists:seq(1,  

See https://issues.apache.org/jira/secure/attachment/12401866/term_to_binary_fix.diff

for a non-recursive version of term_to_binary().

How do other projects deal with this issue? If there is no workaround,  
it possible to apply the patch in the next OTP release?

On behalf of Damien Katz and the CouchDB team,

Jan Lehnardt

More information about the erlang-patches mailing list