NUL byte in string literal

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

NUL byte in string literal

Matthew Wild
Hey,

Just a bit of feedback that I'm surprised hasn't come up before, but I
couldn't find it in the archives.

I feel the manual is a bit misleading when it says the following:

>  Strings in Lua can contain any 8-bit value, including embedded zeros, which can be specified as '\0'.

While this is correct in most cases, it's not technically accurate
that you can actually always use \0. If it is followed by numeric
digits, they will instead be interpreted instead as a numeric escape
sequence.

That leaves the only truly correct way of representing \0 (that
doesn't depend on context) as \000.

Regards,
Matthew

Reply | Threaded
Open this post in threaded view
|

Re: NUL byte in string literal

Dirk Laurie-2
2018-04-02 11:24 GMT+02:00 Matthew Wild <[hidden email]>:

> Just a bit of feedback that I'm surprised hasn't come up before, but I
> couldn't find it in the archives.
>
> I feel the manual is a bit misleading when it says the following:
>
> >  Strings in Lua can contain any 8-bit value, including embedded zeros, which can be specified as '\0'.
>
> While this is correct in most cases, it's not technically accurate
> that you can actually always use \0. If it is followed by numeric
> digits, they will instead be interpreted instead as a numeric escape
> sequence.
>
> That leaves the only truly correct way of representing \0 (that
> doesn't depend on context) as \000.

Not so. \x00 also works, and is IMHO preferable.

Anyway, all this is equally true of \1, \2, etc, so the manual is
justified in making no song and dance about it when embedded zeros are
discussed.

Reply | Threaded
Open this post in threaded view
|

Re: NUL byte in string literal

KHMan
On 4/2/2018 5:58 PM, Dirk Laurie wrote:

> 2018-04-02 11:24 GMT+02:00 Matthew Wild:
>
>> Just a bit of feedback that I'm surprised hasn't come up before, but I
>> couldn't find it in the archives.
>>
>> I feel the manual is a bit misleading when it says the following:
>>
>>>   Strings in Lua can contain any 8-bit value, including embedded zeros, which can be specified as '\0'.
>>
>> While this is correct in most cases, it's not technically accurate
>> that you can actually always use \0. If it is followed by numeric
>> digits, they will instead be interpreted instead as a numeric escape
>> sequence.
>>
>> That leaves the only truly correct way of representing \0 (that
>> doesn't depend on context) as \000.
>
> Not so. \x00 also works, and is IMHO preferable.
>
> Anyway, all this is equally true of \1, \2, etc, so the manual is
> justified in making no song and dance about it when embedded zeros are
> discussed.

I guess the important words in that sentence from the manual are
"can be".

[OT] That reminds me of a character called 'Canby' in a certain
classic children's book... written by an architect.

--
Cheers,
Kein-Hong Man (esq.)
Selangor, Malaysia



Reply | Threaded
Open this post in threaded view
|

Re: NUL byte in string literal

Albert Chan
In reply to this post by Dirk Laurie-2
On Apr 2, 2018, at 5:58 AM, Dirk Laurie <[hidden email]> wrote:

> 2018-04-02 11:24 GMT+02:00 Matthew Wild <[hidden email]>:
>> That leaves the only truly correct way of representing \0 (that
>> doesn't depend on context) as \000.
>
> Not so. \x00 also works, and is IMHO preferable.
>

Agreed.

lua decimal escape is easily confused with octal, especially with
leading zero, like \012.

Any other programming languages use decimal escapes ?

Reply | Threaded
Open this post in threaded view
|

Re: NUL byte in string literal

Dirk Laurie-2
2018-04-02 14:15 GMT+02:00 Albert Chan <[hidden email]>:

> On Apr 2, 2018, at 5:58 AM, Dirk Laurie <[hidden email]> wrote:
>
>> 2018-04-02 11:24 GMT+02:00 Matthew Wild <[hidden email]>:
>>> That leaves the only truly correct way of representing \0 (that
>>> doesn't depend on context) as \000.
>>
>> Not so. \x00 also works, and is IMHO preferable.
>>
>
> Agreed.
>
> lua decimal escape is easily confused with octal, especially with
> leading zero, like \012.
>
> Any other programming languages use decimal escapes ?

Pascal does, but the cute thing is that it isn't really an escape, it
sits outside the string quotes.

'this'#9'is'#13'OK'#8#8'allowed'#10

Reply | Threaded
Open this post in threaded view
|

Re: NUL byte in string literal

Roberto Ierusalimschy
In reply to this post by Matthew Wild
> I feel the manual is a bit misleading when it says the following:
>
> >  Strings in Lua can contain any 8-bit value, including embedded zeros, which can be specified as '\0'.

I could not find that sentence in the current manual (Lua 5.3.4).

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: NUL byte in string literal

Matthew Wild
On 2 April 2018 at 15:39, Roberto Ierusalimschy <[hidden email]> wrote:
>> I feel the manual is a bit misleading when it says the following:
>>
>> >  Strings in Lua can contain any 8-bit value, including embedded zeros, which can be specified as '\0'.
>
> I could not find that sentence in the current manual (Lua 5.3.4).

Oh, please accept my apologies, I had the 5.2 manual open instead of 5.3.

5.3 has: "Lua is 8-bit clean: strings can contain any 8-bit value,
including embedded zeros ('\0')."

which resolves the issue.

Regards,
Matthew