Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH] lua/key_def: fix compare_with_key() part count check
@ 2020-12-14 15:35 Sergey Nikiforov
  2020-12-16 22:34 ` Vladislav Shpilevoy
  2020-12-17 12:37 ` Alexander Turenko
  0 siblings, 2 replies; 7+ messages in thread
From: Sergey Nikiforov @ 2020-12-14 15:35 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Vladislav Shpilevoy

Added corresponding test

Fixes: #5307
---
 src/box/lua/key_def.c                         |  9 +++++++
 .../gh-5307-key_def-part-count-check.result   |  1 +
 .../gh-5307-key_def-part-count-check.test.lua | 25 +++++++++++++++++++
 3 files changed, 35 insertions(+)
 create mode 100644 test/app-tap/gh-5307-key_def-part-count-check.result
 create mode 100755 test/app-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..674891a85 100644
--- a/src/box/lua/key_def.c
+++ b/src/box/lua/key_def.c
@@ -362,6 +362,15 @@ lbox_key_def_compare_with_key(struct lua_State *L)
 	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 (part_count > key_def->part_count) {
+		region_truncate(region, region_svp);
+		tuple_unref(tuple);
+		diag_set(ClientError, ER_KEY_PART_COUNT,
+			 key_def->part_count, part_count);
+		return luaT_error(L);
+	}
+
 	if (key_validate_parts(key_def, key, part_count, true,
 			       &key_end) != 0) {
 		region_truncate(region, region_svp);
diff --git a/test/app-tap/gh-5307-key_def-part-count-check.result b/test/app-tap/gh-5307-key_def-part-count-check.result
new file mode 100644
index 000000000..d47a905a5
--- /dev/null
+++ b/test/app-tap/gh-5307-key_def-part-count-check.result
@@ -0,0 +1 @@
+Failed (as it should), error message: Invalid key part count (expected [0..1], got 9)
diff --git a/test/app-tap/gh-5307-key_def-part-count-check.test.lua b/test/app-tap/gh-5307-key_def-part-count-check.test.lua
new file mode 100755
index 000000000..8c8f6f34b
--- /dev/null
+++ b/test/app-tap/gh-5307-key_def-part-count-check.test.lua
@@ -0,0 +1,25 @@
+#!/usr/bin/env tarantool
+
+key_def = require('key_def')
+kd = key_def.new({{fieldno = 1, type = 'unsigned'}})
+
+-- Should succeed
+if (kd:compare_with_key({1}, {1}) ~= 0) then
+    print("Error: should be equal")
+end
+
+-- Should succeed
+if (kd:compare_with_key({1}, {2}) >= 0) then
+    print("Error: should be less")
+end
+
+-- Should fail
+ok, err = pcall(function()
+	kd:compare_with_key({1}, {1, 2, 3, 4, 5, 6, 7, 8, 9})
+    end)
+if (ok) then
+    print("Error: it should not have succeeded")
+else
+    print("Failed (as it should), error message: " .. err)
+end
+
-- 
2.25.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-12-20 16:33 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-14 15:35 [Tarantool-patches] [PATCH] lua/key_def: fix compare_with_key() part count check Sergey Nikiforov
2020-12-16 22:34 ` Vladislav Shpilevoy
2020-12-17 11:28   ` Alexander Turenko
2020-12-17 13:27   ` Sergey Nikiforov
2020-12-17 16:41     ` Alexander Turenko
2020-12-20 16:33     ` Vladislav Shpilevoy
2020-12-17 12:37 ` Alexander Turenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox