From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 47CAC4765E0 for ; Mon, 21 Dec 2020 15:08:38 +0300 (MSK) From: Sergey Nikiforov Date: Mon, 21 Dec 2020 15:08:24 +0300 Message-Id: <20201221120824.760453-1-void@tarantool.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v2] lua/key_def: fix compare_with_key() part count check List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org Cc: Vladislav Shpilevoy , Alexander Turenko Added corresponding test Fixes: #5307 --- Branch: https://github.com/tarantool/tarantool/tree/void234/gh-5307-fix-key_def-part-count-check-v2 Issue: https://github.com/tarantool/tarantool/issues/5307 src/box/lua/key_def.c | 7 ++--- .../gh-5307-key_def-part-count-check.test.lua | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100755 test/box-tap/gh-5307-key_def-part-count-check.test.lua diff --git a/src/box/lua/key_def.c b/src/box/lua/key_def.c index a781aeff9..6544c20bd 100644 --- a/src/box/lua/key_def.c +++ b/src/box/lua/key_def.c @@ -360,15 +360,14 @@ lbox_key_def_compare_with_key(struct lua_State *L) struct region *region = &fiber()->gc; size_t region_svp = region_used(region); size_t key_len; - const char *key_end, *key = lbox_encode_tuple_on_gc(L, 3, &key_len); - uint32_t part_count = mp_decode_array(&key); - if (key_validate_parts(key_def, key, part_count, true, - &key_end) != 0) { + const char *key = lbox_encode_tuple_on_gc(L, 3, &key_len); + if (box_key_def_validate_key(key_def, key, NULL)) { region_truncate(region, region_svp); tuple_unref(tuple); return luaT_error(L); } + uint32_t part_count = mp_decode_array(&key); int rc = tuple_compare_with_key(tuple, HINT_NONE, key, part_count, HINT_NONE, key_def); region_truncate(region, region_svp); diff --git a/test/box-tap/gh-5307-key_def-part-count-check.test.lua b/test/box-tap/gh-5307-key_def-part-count-check.test.lua new file mode 100755 index 000000000..dfd50f799 --- /dev/null +++ b/test/box-tap/gh-5307-key_def-part-count-check.test.lua @@ -0,0 +1,29 @@ +#!/usr/bin/env tarantool + +local tap = require('tap') +local mytest = tap.test('key_def part count tests') + +mytest:plan(3) + +local key_def = require('key_def') +local kd = key_def.new({{fieldno = 1, type = 'unsigned'}}) +local ok, res + +-- Should succeed +ok, res = pcall(kd.compare_with_key, kd, {1}, {1}) +print("First: ", ok, res) +mytest:ok(ok and res == 0, "Simple equality") + +-- Should succeed +ok, res = pcall(kd.compare_with_key, kd, {1}, {2}) +print("Second: ", ok, res) +mytest:ok(ok and res < 0, "Simple inequality") + +-- Should fail +local exp_err = "Invalid key part count (expected [0..1], got 9)" +ok, res = pcall(kd.compare_with_key, kd, {1}, {1, 2, 3, 4, 5, 6, 7, 8, 9}) +print("Third: ", ok, res) +mytest:is_deeply({ok, tostring(res)}, {false, exp_err}, + "Invalid key part count") + +os.exit(mytest:check() and 0 or 1) -- 2.25.1