Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH v1 1/2] sql: ignore \0 in string passed to C-function
Date: Thu, 1 Apr 2021 11:32:05 +0300	[thread overview]
Message-ID: <20210401083205.GA51868@tarantool.org> (raw)
In-Reply-To: <ced6bbd2-4714-0746-a3f4-9a76fbc0b9f4@tarantool.org>

Hi! Thank you for the review! My answers, diff and new version below.

On Wed, Mar 31, 2021 at 10:25:21PM +0200, Vladislav Shpilevoy wrote:
> Hi! Thanks for the patch!
> 
> See 4 comments below.
> 
> On 30.03.2021 13:21, Mergen Imeev via Tarantool-patches wrote:
> > 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
> > ---
> > 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 000000000..96823f049
> > --- /dev/null
> > +++ b/test/sql-tap/gh-5938-wrong-string-length.c
> > @@ -0,0 +1,42 @@
> > +#include <msgpuck.h>
> 
> 1. We use "" for non-system headers, not <>.
Fixed.

> 
> > +#include "module.h"
> > +
> > +enum
> > +{
> 
> 2. Enums have { on the same line as 'enum'.
Fixed.

> 
> > +	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, "%s",
> > +			"invalid argument count");
> 
> 3. You don't need "%s", you can pass the error message right away.
Thanks, fixed.

> 
> 4. The expression is misaligned. The same for the other box_error_set().
Fixed.

> 
> > +	}
> > +	if (mp_typeof(*args) != MP_STR) {
> > +		return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s",
> > +			"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, "%s",
> > +			"string is too long");
> > +	}


Diff:


diff --git a/test/sql-tap/gh-5938-wrong-string-length.c b/test/sql-tap/gh-5938-wrong-string-length.c
index 96823f049..e53163fd2 100644
--- a/test/sql-tap/gh-5938-wrong-string-length.c
+++ b/test/sql-tap/gh-5938-wrong-string-length.c
@@ -1,8 +1,7 @@
-#include <msgpuck.h>
+#include "msgpuck.h"
 #include "module.h"
 
-enum
-{
+enum {
 	BUF_SIZE = 512,
 };
 
@@ -11,12 +10,12 @@ 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, "%s",
-			"invalid argument count");
+		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, "%s",
-			"argument should be string");
+		return box_error_set(__FILE__, __LINE__, ER_PROC_C,
+				     "argument should be string");
 	}
 	const char* str;
 	uint32_t str_n;
@@ -24,8 +23,8 @@ ret_str(box_function_ctx_t *ctx, const char *args, const char *args_end)
 
 	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, "%s",
-			"string is too long");
+		return box_error_set(__FILE__, __LINE__, ER_PROC_C,
+				     "string is too long");
 	}
 
 	char tuple_buf[BUF_SIZE];



New patch:


commit dc1daab44f8960ff111eb3127252e51f1ed5c403
Author: Mergen Imeev <imeevma@gmail.com>
Date:   Tue Mar 30 07:08:13 2021 +0300

    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

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index f15d27051..c3c14bd22 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 7fe078835..7276996d9 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 000000000..6e2eae2ff
--- /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 000000000..e53163fd2
--- /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 000000000..943389e34
--- /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()

  reply	other threads:[~2021-04-01  8:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-30 11:21 [Tarantool-patches] [PATCH v1 0/2] sql: ignore \0 in string passed to user function Mergen Imeev via Tarantool-patches
2021-03-30 11:21 ` [Tarantool-patches] [PATCH v1 1/2] sql: ignore \0 in string passed to C-function Mergen Imeev via Tarantool-patches
2021-03-31 20:25   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-01  8:32     ` Mergen Imeev via Tarantool-patches [this message]
2021-03-30 11:21 ` [Tarantool-patches] [PATCH v1 2/2] sql: ignore \0 in string passed to Lua-function Mergen Imeev via Tarantool-patches
2021-03-31 20:25   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-01  8:41     ` Mergen Imeev via Tarantool-patches
2021-04-01 12:01       ` Mergen Imeev via Tarantool-patches
2021-04-01 19:51       ` Vladislav Shpilevoy via Tarantool-patches
2021-04-01 22:50         ` Mergen Imeev via Tarantool-patches
2021-04-01 23:09 ` [Tarantool-patches] [PATCH v1 0/2] sql: ignore \0 in string passed to user function Vladislav Shpilevoy via Tarantool-patches
2021-04-02  7:55 ` Kirill Yukhin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210401083205.GA51868@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v1 1/2] sql: ignore \0 in string passed to C-function' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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