Re: luaO_pushvfstring reads past end of format string on trailing %

Francisco Olarte <[email protected]>
Newsgroups gmane.comp.lang.lua.general
Message-ID <CA+bJJbx5N2F2FVrQuqN3SmeZW+sQmtYh_AMAxNhz=y4oawLSnw@mail.gmail.com>
On Wed, 12 Aug 2026 at 16:48, Roberto Ierusalimschy
<[email protected]> wrote:
> The manual says this:
>     Every occurrence of % in the string fmt must form a valid conversion
>     specifier.
> So, this 'just "keep unknown format in the result"' is undocumented
> behavior. As you said, in C there are endless ways to shoot your own
> foot, for instance by calling 'lua_pushfstring(L, "%s", 42l)'.
> A '%' in the end of the format string is just one more way.

I know, in fact I think I quoted that from the manual too.

I would like to wrap up a proposal ( for consideration in future versions).

- keep default in switch as is.
- Add "&& e[1]" just after strchr in while. If strchr succeeds e[1] is not UB.
- Let the tail after the while handle the dangling %.
- Change the manual to something like "Any occurrence of % in the
string fmt not starting a valid conversion specifier will be copied
to the output."

IMO this is a small change which, getting rid of the MUST, leads to
less UB in the manual.

Besides, and this is a personal taste, the e[1] check makes me feel
better as it insures e is a "valid C string with at least two chars (
i.e., strlen>=2 ) starting with '%' " before the switch.

Francisco Olarte.

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CA%2BbJJbx5N2F2FVrQuqN3SmeZW%2BsQmtYh_AMAxNhz%3Dy4oawLSnw%40mail.gmail.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.