embedded zeroes and io.lines

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

embedded zeroes and io.lines

Brian R. Gaeke
Hello,

I encountered the following behavior I didn't expect with regards
to embedded zeroes and io.lines:

% lua -v
Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
% cat zero.lua
for line in io.lines(arg[1]) do print(tostring(string.len(line))) end
% wc zero.lua
       1       7      70 zero.lua
% lua zero.lua zero.lua
69
% od -c zero
0000000   \0  \n                                                        
0000002
% wc zero
       1       1       2 zero
% lua zero.lua zero
% 

Specifically note the lack of output from the last command.  I
feel that it should have printed 1, or 0 if string.len is supposed
to follow C-string semantics.  (Is it?)   More to the point, is
there a way that I can input lines from files with embedded zeroes
using io.lines or file:lines?  I found that I could read the zero
using e.g. io.read(100), but I don't want to have to find the ends
of the lines myself.  I am running Lua on Mac OS X 10.3.9.  Thanks
for any suggestions.

-Brian Gaeke


Reply | Threaded
Open this post in threaded view
|

Re: embedded zeroes and io.lines

Roberto Ierusalimschy
> More to the point, is there a way that I can input lines from files
> with embedded zeroes using io.lines or file:lines?

No. Lua assumes that "lines" are only meaningful in text files.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: embedded zeroes and io.lines

Edgar Toernig
Roberto Ierusalimschy wrote:
>
> > More to the point, is there a way that I can input lines from files
> > with embedded zeroes using io.lines or file:lines?
> 
> No. Lua assumes that "lines" are only meaningful in text files.

Anyway, afaics the code in liolib:read_line accesses out of bound
memory for "\0\n":

    l = strlen(p);
    if (p[l-1] != '\n')

Ciao, ET.