2

Lua script:

i=io.read()
print(i)

Command line:

echo -e "sala\x00m" | lua ll.lua

Output:

sala

I want it to print all character from input, similar to this:

salam

in HEX editor:

0000000: 7361 6c61 006d 0a                         sala.m.

How can I print all character from input?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Baba
  • 852
  • 1
  • 17
  • 31

1 Answers1

4

You tripped over one of the few places where the Lua standard library is still not 8-bit-clean.
Specifically, file reading line-by-line is not embedded-0 proof.

The reason it isn't yet is an unfortunate combination of:

  • Only standard C90 or equally portable constructs are allowed for the core, which does not provide for efficient 0-clean text parsing.
  • Every solution discussed to date on the mailinglist under that constraint has considerable overhead.
  • Embedded 0-bytes in text files are quite rare.

Workarounds:

  • Use a modified library, fixing these formats: "*l" "*L" for file:read(...)
  • parse your raw data yourself. (read a block using a number or as much as possible using "*a")
  • Badger the Lua developers/maintainers for a bugfix until they give in.
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 1
    I should also mention that Lua 5.1's `print()` *cannot* handle embedded NULLs whereas Lua 5.2's *can* handle them. This means that the asker's code won't work, on 5.1, even if he hadn't the `*l` deficiency. – Niccolo M. May 12 '14 at 17:00
  • @NiccoloM. Making sure everything is 8-bit clean is an ongoing campaign. There were more library functions which were not 0-proof back then (8 years ago), not only `print`. Imho `file:read` is special because no fix was accepted even though its brokenness is well known. – Deduplicator May 12 '14 at 17:12