[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Negative table index
- From: Roberto Ierusalimschy <roberto@...>
- Date: Tue, 23 Dec 2014 09:14:59 -0200
> [...] What about this check?
>
> n = e - f;
> luaL_argcheck(L, t+n >= t, 4, "destination index overflows");
> for (i = 0; i <= n; i++)
A good compiler can throw away the test 't+n>=t': It knows that 'n'
is non negative, because of a previous 'if e >= f', and it can assume
that signed arithmetic operations do not overflow (as overflows have
undefined behavior in the standard).
(Your code also has a problem when 'n' is maxinteger, because then the
for loop will never end.)
-- Roberto