From fa7e6f7df8745a6f40bf4cb6a8e01be3fffbc93d Mon Sep 17 00:00:00 2001
From: Mergen Imeev <imeevma@gmail.com>
Date: Tue, 30 Mar 2021 07:08:13 +0300
Subject: [PATCH] sql: ignore \0 in string passed to C-function

Prior to this patch string passed to user-defined C-function from SQL
was cropped in case it contains '\0'. At the same time, it wasn't
cropped if it is passed to the function from BOX. Now it isn't cropped
when passed from SQL.

Part of #5938
---
 src/box/sql/func.c                            |  3 +-
 test/CMakeLists.txt                           |  1 +
 test/sql-tap/CMakeLists.txt                   |  2 +
 test/sql-tap/gh-5938-wrong-string-length.c    | 41 +++++++++++++++++++
 .../gh-5938-wrong-string-length.test.lua      | 28 +++++++++++++
 5 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 test/sql-tap/CMakeLists.txt
 create mode 100644 test/sql-tap/gh-5938-wrong-string-length.c
 create mode 100755 test/sql-tap/gh-5938-wrong-string-length.test.lua

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index f15d270518..c3c14bd226 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -175,7 +175,8 @@ port_vdbemem_get_msgpack(struct port *base, uint32_t *size)
 		}
 		case MP_STR: {
 			const char *str = (const char *) sql_value_text(param);
-			mpstream_encode_str(&stream, str);
+			mpstream_encode_strn(&stream, str,
+					     sql_value_bytes(param));
 			break;
 		}
 		case MP_BIN:
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 7fe078835c..7276996d95 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -62,6 +62,7 @@ add_subdirectory(app)
 add_subdirectory(app-tap)
 add_subdirectory(box)
 add_subdirectory(box-tap)
+add_subdirectory(sql-tap)
 if(ENABLE_FUZZER)
     add_subdirectory(fuzz)
 endif()
diff --git a/test/sql-tap/CMakeLists.txt b/test/sql-tap/CMakeLists.txt
new file mode 100644
index 0000000000..6e2eae2ff1
--- /dev/null
+++ b/test/sql-tap/CMakeLists.txt
@@ -0,0 +1,2 @@
+include_directories(${MSGPUCK_INCLUDE_DIRS})
+build_module(gh-5938-wrong-string-length gh-5938-wrong-string-length.c)
diff --git a/test/sql-tap/gh-5938-wrong-string-length.c b/test/sql-tap/gh-5938-wrong-string-length.c
new file mode 100644
index 0000000000..e53163fd2d
--- /dev/null
+++ b/test/sql-tap/gh-5938-wrong-string-length.c
@@ -0,0 +1,41 @@
+#include "msgpuck.h"
+#include "module.h"
+
+enum {
+	BUF_SIZE = 512,
+};
+
+int
+ret_str(box_function_ctx_t *ctx, const char *args, const char *args_end)
+{
+	uint32_t arg_count = mp_decode_array(&args);
+	if (arg_count != 1) {
+		return box_error_set(__FILE__, __LINE__, ER_PROC_C,
+				     "invalid argument count");
+	}
+	if (mp_typeof(*args) != MP_STR) {
+		return box_error_set(__FILE__, __LINE__, ER_PROC_C,
+				     "argument should be string");
+	}
+	const char* str;
+	uint32_t str_n;
+	str = mp_decode_str(&args, &str_n);
+
+	uint32_t size = mp_sizeof_array(1) + mp_sizeof_str(str_n);
+	if (size > BUF_SIZE) {
+		return box_error_set(__FILE__, __LINE__, ER_PROC_C,
+				     "string is too long");
+	}
+
+	char tuple_buf[BUF_SIZE];
+	char *d = tuple_buf;
+	d = mp_encode_array(d, 1);
+	d = mp_encode_str(d, str, str_n);
+	assert(d <= tuple_buf + size);
+
+	box_tuple_format_t *fmt = box_tuple_format_default();
+	box_tuple_t *tuple = box_tuple_new(fmt, tuple_buf, d);
+	if (tuple == NULL)
+		return -1;
+	return box_return_tuple(ctx, tuple);
+}
diff --git a/test/sql-tap/gh-5938-wrong-string-length.test.lua b/test/sql-tap/gh-5938-wrong-string-length.test.lua
new file mode 100755
index 0000000000..943389e345
--- /dev/null
+++ b/test/sql-tap/gh-5938-wrong-string-length.test.lua
@@ -0,0 +1,28 @@
+#!/usr/bin/env tarantool
+local build_path = os.getenv("BUILDDIR")
+package.cpath = build_path..'/test/sql-tap/?.so;'..build_path..'/test/sql-tap/?.dylib;'..package.cpath
+
+local test = require("sqltester")
+test:plan(1)
+
+box.schema.func.create("gh-5938-wrong-string-length.ret_str", {
+    language = "C",
+    param_list = { "string" },
+    returns = "string",
+    exports = { "LUA", "SQL" },
+    is_deterministic = true
+})
+
+test:execsql([[CREATE TABLE t (i INT PRIMARY KEY, s STRING);]])
+box.space.T:insert({1, 'This is a complete string'})
+box.space.T:insert({2, 'This is a cropped\0 string'})
+
+test:do_execsql_test(
+    "gh-5938-1",
+    [[
+        SELECT "gh-5938-wrong-string-length.ret_str"(s) from t;
+    ]], {
+        "This is a complete string","This is a cropped\0 string"
+    })
+
+test:finish_test()
-- 
GitLab