lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mar 30, 2014, at 6:02 PM, Doug Currie <doug.currie@gmail.com> wrote:

> On Sun, Mar 30, 2014 at 11:54 AM, Doug Currie <doug.currie@gmail.com> wrote:
> On Sun, Mar 30, 2014 at 8:55 AM, Paige DePol <lual@serfnet.org> wrote:
> 
> sort.lua fails the assertion at line 31:
> 
> assert(not pcall(unpack, {}, 0, 2^31-1))
> 
> 
> -- also on OSX 10.9.2...
> $ lua
> 
> Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> 
> > unpack {}
> 
> > unpack ({}, 0, 2^31-1)
> 
> Segmentation fault: 11 
> 
> unpack may be fixed in Clang with
> 
> //  if (n <= 0 || !lua_checkstack(L, n))  /* n <= 0 means arith. overflow */
>   if (__builtin_sadd_overflow(e - i, 1, &n) || !lua_checkstack(L, n))
> 
> Too bad there is no portable simple way to do this. In this case, this would also work:
> 
> //  n = e - i + 1;  /* number of elements */
> //  if (n <= 0 || !lua_checkstack(L, n))  /* n <= 0 means arith. overflow */
>   n = e - i;  /* number of elements minus one */
>   if (n > (INT_MAX - 1) || !lua_checkstack(L, n+1))  /* n > (INT_MAX - 1) means arith. overflow */
> 
> e

Since you recommend using __builtin_sadd_overflow can you explain what the compiler is doing that is causing the issue in the first place?

~Paige