lua-users home
lua-l archive

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


Would anyone be opposed to a patch that would cause the huge number becoming a negative number to stop occuring? I think I found a simple solution that should be safe for any architecture:


static int str_rep (lua_State *L) {
  size_t l;
  luaL_Buffer b;
  const char *s = luaL_checklstring(L, 1, &l);
  /* int n = luaL_checkint(L, 2);*/
  int n = (int)(((unsigned int)-1) >> 1);           /* Determine the maximum value of int (most compilers should opimize to do at compile time) */
  lua_Number no = luaL_checknumber(L, 2);           /* Get the parameter as a lua_Number first */
  if (no > n)                                       /* If we can't fit it, don't use it */
  {
      lua_pushstring(L, "string is too long");
      lua_error(L);
  }
  else if (no < 0)                                  /* If it's invalid, don't use it */
  {
      lua_pushstring(L, "string repetitions cannot be negative");
      lua_error(L);
  }
  else
      n = (int)no;
        


  luaL_buffinit(L, &b);
  while (n-- > 0)
    luaL_addlstring(&b, s, l);
  luaL_pushresult(&b);
  return 1;
}


Again, any criticism, etc., welcome,
-- Matthew P. Del Buono a.k.a. Shirik