[tarantool-patches] [PATCH] msgpackffi.decode can now be assigned to buf.rpos

  • From: Maria <maria.khaydich@xxxxxxxxxxxxx>
  • To: tarantool-patches@xxxxxxxxxxxxx
  • Date: Thu, 12 Sep 2019 20:44:48 +0300

Function decode of module msgpackffi was passing
value of type const unsigned char * to a C function
that accepts arguments of type const char *.

Closes #3926

Test added
---
 src/lua/msgpackffi.lua           | 8 +++++---
 test/app-tap/msgpackffi.test.lua | 6 ++++++
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/lua/msgpackffi.lua b/src/lua/msgpackffi.lua
index f7ee44291..18a3cb3d5 100644
--- a/src/lua/msgpackffi.lua
+++ b/src/lua/msgpackffi.lua
@@ -11,6 +11,7 @@ local uint16_ptr_t = ffi.typeof('uint16_t *')
 local uint32_ptr_t = ffi.typeof('uint32_t *')
 local uint64_ptr_t = ffi.typeof('uint64_t *')
 local const_char_ptr_t = ffi.typeof('const char *')
+local char_ptr_t = ffi.typeof('char *')
 
 ffi.cdef([[
 char *
@@ -602,11 +603,12 @@ local function decode_unchecked(str, offset)
         local buf = ffi.cast(const_char_ptr_t, str)
         bufp[0] = buf + offset - 1
         local r = decode_r(bufp)
-        return r, bufp[0] - buf + 1
-    elseif ffi.istype(const_char_ptr_t, str) then
+        return r, ffi.cast(char_ptr_t, bufp[0]) - buf + 1
+    elseif ffi.istype(const_char_ptr_t, str) or
+        ffi.istype(char_ptr_t, str) then
         bufp[0] = str
         local r = decode_r(bufp)
-        return r, bufp[0]
+        return r, ffi.cast(char_ptr_t, bufp[0])
     else
         error("msgpackffi.decode_unchecked(str, offset) -> res, new_offset | 
"..
               "msgpackffi.decode_unchecked(const char *buf) -> res, new_buf")
diff --git a/test/app-tap/msgpackffi.test.lua b/test/app-tap/msgpackffi.test.lua
index f2a8f254b..3ca09b304 100755
--- a/test/app-tap/msgpackffi.test.lua
+++ b/test/app-tap/msgpackffi.test.lua
@@ -4,6 +4,7 @@ package.path = "lua/?.lua;"..package.path
 
 local tap = require('tap')
 local common = require('serializer_test')
+local buffer = require('buffer')
 
 local function is_map(s)
     local b = string.byte(string.sub(s, 1, 1))
@@ -68,6 +69,11 @@ local function test_other(test, s)
     test:is(#s.encode(-0x8001), 5, "len(encode(-0x8001))")
     test:is(#s.encode(-0x80000000), 5, "len(encode(-0x80000000))")
     test:is(#s.encode(-0x80000001), 9, "len(encode(-0x80000001))")
+    
+    -- gh-3926: decode result cannot be assigned to buffer.rpos
+    local buf = buffer.ibuf()
+    local res, buf.rpos = msgpackffi.decode(buf.rpos, buf.wpos - buf.rpos)
+    test:iscdata(buf.rpos, ‘char *’)
 end
 
 tap.test("msgpackffi", function(test)
-- 
2.20.1 (Apple Git-117)


Other related posts: