[erlang-patches] fread failing to handle newlines gracefully

Matthew Palmer mpalmer@REDACTED
Fri Jan 30 13:12:23 CET 2009


Hi,

I recently fielded a question on Stack Overflow regarding some strange
behaviour of fread, with the ~d format spec
(http://stackoverflow.com/questions/473327/unexpected-behavior-of-iofread-in-erlang/490023#490023). 
Digging deep, it appears to be a bit of a bug in the way that newlines are
handled in the format parser, as demonstrated here:

3> io_lib_fread:fread([], "10 11\n12 13 14\n", "~d").
{done,{ok,"\n"}," 1112 13 14\n"}

It's eaten the newline between "11" and "12", which means that on the next
pass through it'll read the next number as "1112" instead of "11", which
poses some obvious problems.

I've come up with the attached patch to fix the problem.  I'm not very
experienced with Erlang yet, so it might not be the cleanest way to do it,
but it does definitely fix the problem, and without (apparently) breaking
any other common use case.  Comments appreciated.  If there's a test suite I
should be adding to as well, I'm more than happy to do that, I just couldn't
find one in my source tarball.

- Matt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fread_eats_newlines.patch
Type: text/x-diff
Size: 1177 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-patches/attachments/20090130/7bc87505/attachment.bin>


More information about the erlang-patches mailing list