|
On 18/01/2023 21:13, Roberto Ierusalimschy wrote:
I was wondering why Lua's string.format() limits the width and precision fields to two digits. E.g. this doesn't work in Lua because we use a width of 100 (99 works fine): string.format('%100s', 'hello world') Is there any particular reason why Lua imposes this limit on string.format()? I'm just wondering because this restriction sometimes makes it difficult to port C code to Lua.It is a simple and secure way to limit the maximum size of formatted items, to avoid buffer overflow. (Do you really need larger values?)
A couple use cases just off the top of my head: machine generated XML files with long lines; log files with lots of data on each entry (line); scientific data files where a single line holds dozens of values in text form.
BTW, a single big field could be needed to format not a single item, but a bunch of item collected in a single string in a previous processing step.
All in all, just 100 chars seems quite an artificially low value. If a hard limit must be chosen, I'd raise it /at least/ by two, i.e. allowing "%9999s" for example.
See comments around [1]. [1] https://www.lua.org/source/5.4/lstrlib.c.html#MAX_ITEMF -- Roberto
-- Lorenzo