From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id CA1C96EC60; Thu, 1 Apr 2021 11:32:08 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CA1C96EC60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1617265928; bh=RV8q5GVBEOZsi+BKNZL+0OCCP8KKFLjWwrLtmvjEUQE=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=UwthyIQMZesF8zYdoXnzp+DcVzDtYXB8hHghFG+WNKidD0FIGibgkG7cCFqAQ3grX sH0DPI2j+hfocSVeZD75kcy86kMY2oUUZtT7It5H+LbXkvQV1hbU3GYBsON9MRRENh ZjoxONxL2+y7unxQ8VbLEn19xyuHexC6D9CrNI8A= Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (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 EDD706EC60 for ; Thu, 1 Apr 2021 11:32:07 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org EDD706EC60 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1lRskJ-00017i-3u; Thu, 01 Apr 2021 11:32:07 +0300 Date: Thu, 1 Apr 2021 11:32:05 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20210401083205.GA51868@tarantool.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9ED7173E37F4E3294CA3588DDE0233B0D17711AF1EA2D7DB9182A05F538085040BE3BD1669F9B25EFFDC3C3D6D667B74863A92EF64F99828D7185CB50D2983D12 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7BB17EE3498E810FEEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006378ABD31E9FF1CD53C8638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95CE99938B3FD79E1DF5A4239E35B3C97253FADB0833BE0D8D2A471835C12D1D9774AD6D5ED66289B5259CC434672EE6371117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE71AE4D56B06699BBC9FA2833FD35BB23DF004C906525384302BEBFE083D3B9BA71A620F70A64A45A98AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDC0F6C5B2EEF3D0C75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: 0D63561A33F958A5A3DA4E3CD9E60F4DD72CCC25BEE660C70E531BEC63C5C438D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34C75F910DBB8BE898ACAA5D86C3E4F26894F5BA2005D5EF28BDB6A0509CA358BB42F8F1A88DF3E4AB1D7E09C32AA3244C9E7490785CA5B534ABDA39B6678D55B48A6D4CC6FBFAC251729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojetunDCtJ20JiIvK2L/1ayA== X-Mailru-Sender: 689FA8AB762F73936BC43F508A0638227743BD3D62CAE07CA93FEC1EDF067AF083D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v1 1/2] sql: ignore \0 in string passed to C-function X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: Mergen Imeev Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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 > > 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 +#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 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()