lua-users home
lua-l archive

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


On Fri, Mar 13, 2020, at 10:04 AM, Francisco Olarte wrote:
> On Thu, Mar 12, 2020 at 8:55 PM cherue <cherue.lual@fastmail.com> wrote:
> > I have two suggestions to clear this up for future users:
> > - Error on a set sign bit in the "J" and "I8" conversion options
> 
> J  => unsigned do not have sign bit, the point of using J is typically
> to use the extra bit.
> 
> I8 => How do you propose to unpack negative numbers?
> 
> FOS.

J => if you use that bit you get an overflow

I8 => use the appropriate i8

How about this:

diff --git a/lstrlib.c b/lstrlib.c
index 28df2d45..056a6417 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1684,6 +1684,13 @@ static lua_Integer unpackint (lua_State *L, const char *str,
         luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
     }
   }
+  else if (size == SZINT) {
+    if (!issigned) {
+      if ((lua_Integer)res < 0) {
+        luaL_error(L, "does not fit into Lua Integer");
+      }
+    }
+  }
   return (lua_Integer)res;
 }
 
@@ -1800,4 +1807,3 @@ LUAMOD_API int luaopen_string (lua_State *L) {
   createmetatable(L);
   return 1;
 }
-