Re: An unsigned overflow in luai_makeseed()

"'Yao Zi' via lua-l" <[email protected]>
Newsgroups gmane.comp.lang.lua.general
Message-ID <aZRhYxtg4vwGK8YR@pie>
On Tue, Feb 17, 2026 at 01:50:39AM -0800, Sergey Bronnikov wrote:
> Hello,
> 
> the function luai_makeseed() in lauxlib.c has the following expression [1]:
> 
> res ^= (res >> 3) + (res << 7) + buff[i];
> 
> where res has type "unsigned integer". This arithmetic has an unsigned 
> overflow.
> This is of course undefined behavior from a language point of view, but 

If res has type "signed int", this is true, ISO 9899:1999 6.5.5 states

> If an exceptional condition occurs during the evaluation of an
> expression (that is, if the result is not mathematically defined or
> not in the range of representable values for its type), the behavior
> is undefined.

However res has type "unsigned int", and for unsigned types we have an
extra rule in 6.2.5.9,

> A computation involving unsigned operands can never overflow, because
> a result that cannot be represented by the resulting unsigned integer
> type is reduced modulo the number that is one greater than the largest
> value that can be represented by the resulting type.

So the behavior is of course defined :)

> maybe it is used here intentionally?

Sure, having the arithmetic result truncated isn't a problem here. We're
making a seed instead of calculating a specific value, what res is in
the end doesn't matter as long as it makes a good use of the entropy.

> 1. https://github.com/lua/lua/blob/c6b484823806e08e1756b1a6066a3ace6f080fae/lauxlib.c#L1167
> 
> Sergey

Best regards,
Yao Zi

-- 
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/aZRhYxtg4vwGK8YR%40pie.
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.