[quickjs-devel] Re: Bug with `readAsString()` and null-bytes

  • From: Fabrice Bellard <fabrice@xxxxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Tue, 17 Mar 2020 10:48:04 +0100

Hi John,

readAsString() and getline() correctly handle null bytes but the bug is in console.log() and print() which stop printing when encountering a null character. All text output functions have the same problem. You can do a workaround by explicitly splitting the string at every null character. It will be fixed in the next release.

Best regards,

Fabrice.

On 03/17/2020 05:50 AM, John Gardner wrote:

I noticed this also affects the file.getline() method too.

On Tue, 17 Mar 2020 at 15:04, John Gardner <gardnerjohng@xxxxxxxxx
<mailto:gardnerjohng@xxxxxxxxx>> wrote:

    When reading a FILE object as a string, the input is truncated at
    the first null-byte:

        *$ printf 'ABC\0XYZ\0EOF' > foo.txt*
        *$ cat test.mjs*
        const file = std.open("foo.txt", "r");
        const str = file.readAsString();
        file.close();
        console.log(str);

        *$ qjs --std ./test.mjs*
        ABC


    Presumably, this is because the method's implementation uses a
    NUL-terminated string in C. I discovered this while reading the
    output of `git status --porcelain -z`, which uses null-bytes to
    delimit fields.

    Cheers,
    — John


Other related posts: