Skip to content

Commit

Permalink
lua-yaml: encode malformed error messages in base64
Browse files Browse the repository at this point in the history
If MP_ERROR contains an error message with invalid UTF-8 sequences,
encode it in Base64 to obtain printable string.

Closes tarantool#6781
Closes tarantool#6934

NO_DOC=bugfix
  • Loading branch information
Gumix authored and mkokryashkin committed Sep 9, 2022
1 parent 17c58b5 commit d2773c5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelogs/unreleased/gh-6934-nonprintable-error-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## bugfix/core

* Fixed tarantool terminations on error messages with invalid UTF-8
sequences (gh-6781 and gh-6934).
17 changes: 17 additions & 0 deletions test/box-luatest/gh_6934_nonprintable_error_message_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local console = require('console')
local t = require('luatest')
local g = t.group()

-- Check that an error message with invalid UTF-8 sequences is encoded to Base64

g.test_bad_utf8_in_error_msg1 = function()
local res = console.eval("box.error.new(box.error.ILLEGAL_PARAMS, 'bad: \x80')")
local ref = "---\n- !!binary SWxsZWdhbCBwYXJhbWV0ZXJzLCBiYWQ6IIA=\n...\n"
t.assert_equals(res, ref)
end

g.test_bad_utf8_in_error_msg2 = function()
local res = console.eval("require('net.box').self:call('bad: \x8a')")
local ref = "---\n- error: !!binary UHJvY2VkdXJlICdiYWQ6IIonIGlzIG5vdCBkZWZpbmVk\n...\n"
t.assert_equals(res, ref)
end
8 changes: 8 additions & 0 deletions third_party/lua-yaml/lyaml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,14 @@ static int dump_node(struct lua_yaml_dumper *dumper)
case MP_ERROR:
str = field.errorval->errmsg;
len = strlen(str);
if (!utf8_check_printable(str, len)) {
is_binary = 1;
lua_pop(dumper->L, 1);
lua_pushlstring(dumper->L, str, len);
tobase64(dumper->L, -1);
str = lua_tolstring(dumper->L, -1, &len);
tag = (yaml_char_t *) LUAYAML_TAG_PREFIX "binary";
}
break;
case MP_DATETIME:
len = datetime_to_string(field.dateval, buf, sizeof(buf));
Expand Down

0 comments on commit d2773c5

Please sign in to comment.