Inconsistencies in Lua 5.4 Reference Manual

114 views
Skip to first unread message

Sergey Bronnikov

unread,
Aug 8, 2025, 9:12:30 AMAug 8
to lua-l
Hello,

I've observed some inconsistencies in Lua 5.4 Reference Manual.

1. string.byte() can return nil

Lua 5.4 Reference Manual describes the following possible values
returned by the function string.byte():

> Returns the internal numeric codes of the characters s[i], s[i+1], ..., s[j].

However, string.byte does not check bounds of i and j and when i and j are less than zero
of greater that length of the "s" the nil is returned:

lua5.4 -e "print(string.byte('A', 2, 3))" -- nil

The same behavior is in Lua 5.1. I suppose i and j should be checked, or this behavior should be described in the Manual.

2. Some possible values of `namewhat` in activation record are missed in Reference Manual

Lua 5.4 Reference Manual describes the following possible values for `namewhat` field:

> namewhat: explains the name field. The value of namewhat can be "global",
> "local", "method", "field", "upvalue", or "" (the empty string),
> according to how the function was called. (Lua uses the empty string
> when no other option seems to apply.)

However, these values are also possible: "hook", "metamethod", "method", "for iterator" and nil. I suppose these values should be described in the Manual.

https://www.lua.org/manual/5.4/manual.html#lua_Debug

Sergey

Roberto Ierusalimschy

unread,
Aug 9, 2025, 12:36:40 PMAug 9
to lu...@googlegroups.com
> 1. string.byte() can return nil
>
> Lua 5.4 Reference Manual describes the following possible values
> returned by the function string.byte():
>
> > Returns the internal numeric codes of the characters s[i], s[i+1], ...,
> s[j].
>
> However, string.byte does not check bounds of i and j and when i and j are
> less than zero
> of greater that length of the "s" the nil is returned:
>
> lua5.4 -e "print(string.byte('A', 2, 3))" -- nil

I could not reproduce that. In my machine, that code doesn't print
anything (that is, string.byte doesn't return any value), as there are
no characters s[2] or s[3].

-- Roberto

Sainan

unread,
Aug 9, 2025, 1:24:04 PMAug 9
to lu...@googlegroups.com
I guess the actual reproduction case would be:

print((string.byte('A', 2, 3))) --> nil

But this behaviour is totally to be expected; 0 return values get 'nil-padded' to make it 1 value.

-- Sainan

Sergey Bronnikov

unread,
Aug 9, 2025, 1:56:53 PMAug 9
to lua-l
This is exactly the behavior I'm talking about. The documentation doesn't say that the function can return nothing.
And is it expected that i and j indices can be out of bound?

Sergey

Shmuel Zeigerman

unread,
Aug 9, 2025, 2:15:19 PMAug 9
to lu...@googlegroups.com
> tostring((function() end)())
stdin:1: bad argument #1 to 'tostring' (value expected)
stack traceback:
        [C]: in function 'tostring'
        stdin:1: in main chunk
        [C]: in ?

--
Shmuel

Sainan

unread,
Aug 9, 2025, 2:22:45 PMAug 9
to lu...@googlegroups.com
> > tostring((function() end)())
> stdin:1: bad argument #1 to 'tostring' (value expected)

Indeed, you would have to do this to have it not be an error:

tostring(((function() end)()))

-- Sainan

Roberto Ierusalimschy

unread,
Aug 9, 2025, 4:28:45 PMAug 9
to lu...@googlegroups.com
> This is exactly the behavior I'm talking about. The documentation doesn't
> say that the function can return nothing.

It says the function returns the characters s[i] ... s[j]; when j < i,
that gives nothing.


> And is it expected that i and j indices can be out of bound?

Yes.

-- Roberto
Reply all
Reply to author
Forward
0 new messages