From 8c48974448f846629873302f0200c0a1505d40b7 Mon Sep 17 00:00:00 2001 From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> Date: Fri, 12 Mar 2021 23:21:25 +0100 Subject: [PATCH] iconv: take errno before reseting the context In Lua iconv_convert() in case ffi.C.tnt_iconv() with normal arguments failed, tried to clear iconv context by calling the function again with all arguments NULL. Then it looked at errno. But the second call could do anything with errno. For instance, it could also fail, and change errno. The patch saves errno into a variable before calling tnt_iconv() second time. It still does not give a perfect protection as it was discovered in scope of #5632, but still better. The patch is mostly motivated by the next patches about #5632 which will add another call to the error path, and it should better be after errno save. Needed for #5632 --- src/lua/iconv.lua | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/lua/iconv.lua b/src/lua/iconv.lua index ade66d7609..e68509dec4 100644 --- a/src/lua/iconv.lua +++ b/src/lua/iconv.lua @@ -43,15 +43,18 @@ local function iconv_convert(iconv, data) buf_left[0] = buf:unused() local res = ffi.C.tnt_iconv(iconv, data_ptr, data_left, buf_ptr, buf_left) - if res == ffi.cast('size_t', -1) and errno() ~= E2BIG then - ffi.C.tnt_iconv(iconv, nil, nil, nil, nil) - if errno() == EINVAL then - error('Invalid multibyte sequence') + if res == ffi.cast('size_t', -1) then + local err = errno() + if err ~= E2BIG then + ffi.C.tnt_iconv(iconv, nil, nil, nil, nil) + if err == EINVAL then + error('Invalid multibyte sequence') + end + if err == EILSEQ then + error('Incomplete multibyte sequence') + end + error('Unknown conversion error: ' .. errno.strerror(err)) end - if errno() == EILSEQ then - error('Incomplete multibyte sequence') - end - error('Unknown conversion error: ' .. errno.strerror()) end buf:alloc(buf:unused() - buf_left[0]) end -- GitLab