lua-users home
lua-l archive

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


I'd like to suggest the following patch so that 'string.unpack("z", "abc")' raises an error instead of returning as if it "consumed" more bytes than are present in the string.

diff --git a/lstrlib.c b/lstrlib.c
index 934b7db..72b0dd1 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1522,6 +1522,7 @@ static int str_unpack (lua_State *L) {
       }
       case Kzstr: {
         size_t len = (int)strlen(data + pos);
+        luaL_argcheck(L, len < ld, 2, "data string too short");
         lua_pushlstring(L, data + pos, len);
         pos += len + 1;  /* skip string plus final '\0' */
         break;

diff --git a/tpack.lua b/tpack.lua
index 592f561..e7c9f82 100644
--- a/tpack.lua
+++ b/tpack.lua
@@ -204,6 +204,9 @@ do
 
   checkerror("contains zeros", pack, "z", "alo\0");
 
+  checkerror("too short", unpack, "z", s)
+  checkerror("too short", unpack, "z", "")
+
   for i = 2, NB do
     local s1 = pack("s" .. i, s)
     assert(unpack("s" .. i, s1) == s and #s1 == #s + i)