From 9cfb3f342d64dce1860c47a31c222b0456824226 Mon Sep 17 00:00:00 2001
From: Damien Lefortier <damien.lefortier@gmail.com>
Date: Mon, 21 Mar 2011 22:57:40 +0100
Subject: [PATCH] A fix and a test case for Bug#702397

A fix and a test case for
https://bugs.launchpad.net/tarantool/+bug/702397
"If SELECT request specifies tuple count 0, no error"

A check was missing.
---
 mod/silverbox/box.c      |  3 +++
 test/CMakeLists.txt      |  4 ++++
 test/box/protocol.c      | 33 +++++++++++++++++++++++++++++++++
 test/box/protocol.result |  2 ++
 test/box/protocol.test   |  7 +++++++
 5 files changed, 49 insertions(+)
 create mode 100644 test/box/protocol.c
 create mode 100644 test/box/protocol.result
 create mode 100644 test/box/protocol.test

diff --git a/mod/silverbox/box.c b/mod/silverbox/box.c
index 4f8ff6dd22..596b31273e 100644
--- a/mod/silverbox/box.c
+++ b/mod/silverbox/box.c
@@ -559,6 +559,9 @@ process_select(struct box_txn *txn, u32 limit, u32 offset, struct tbuf *data)
 	struct box_tuple *tuple;
 	uint32_t *found;
 	u32 count = read_u32(data);
+	if (count == 0)
+		box_raise(ERR_CODE_ILLEGAL_PARAMS,
+			  "tuple count must be greater than zero");
 
 	found = palloc(fiber->pool, sizeof(*found));
 	add_iov(found, sizeof(*found));
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index d96c3520ce..5b6ade761e 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -2,6 +2,10 @@ add_custom_target(test
     COMMAND python ${PROJECT_SOURCE_DIR}/test/test-run.py --builddir=${PROJECT_BINARY_DIR} --vardir=${PROJECT_BINARY_DIR}/test/var
     )
 
+add_executable(box/protocol
+  ${CMAKE_SOURCE_DIR}/test/box/protocol.c)
+target_link_libraries (box/protocol client)
+
 install (PROGRAMS tarantool DESTINATION bin)
 install (DIRECTORY lib DESTINATION bin)
 install (FILES box/tarantool.cfg box/00000000000000000001.snap
diff --git a/test/box/protocol.c b/test/box/protocol.c
new file mode 100644
index 0000000000..4ad75a341b
--- /dev/null
+++ b/test/box/protocol.c
@@ -0,0 +1,33 @@
+#include <connector/c/client.h>
+#include <stdio.h>
+
+int main() {
+  struct tnt_connection *conn = tnt_connect("localhost", 33013);
+  if (conn == NULL)
+	  return 1;
+
+  {
+    const char message[]= {
+	    0xd, 0x0, 0x0, 0x0,    0x11, 0x0, 0x0, 0x0,    0x0, 0x0, 0x0, 0x0,
+	    0x0, 0x0, 0x0, 0x0,    0x0, 0x0, 0x0, 0x0,     0x1, 0x0, 0x0, 0x0,
+	    0x4,    0x1, 0x0, 0x0, 0x0 };
+    int res = tnt_execute_raw(conn, message, sizeof message);
+    printf("return_code: %d\n", res); // =0
+  }
+  {
+    /*
+     * A test case for Bug#702397
+     * https://bugs.launchpad.net/tarantool/+bug/702397
+     * "If SELECT request specifies tuple count 0, no error"
+     */
+    const char message[]= {
+	    0x11, 0x0, 0x0, 0x0,    0x14, 0x0, 0x0, 0x0,    0x0, 0x0, 0x0, 0x0,
+	    0x0, 0x0, 0x0, 0x0,     0x0, 0x0, 0x0, 0x0,     0x0, 0x0, 0x0, 0x0,
+	    0xff, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0 };
+    int res = tnt_execute_raw(conn, message, sizeof message);
+    printf("return_code: %d\n", res); // =2
+  }
+
+  tnt_disconnect(conn);
+  return 0;
+}
diff --git a/test/box/protocol.result b/test/box/protocol.result
new file mode 100644
index 0000000000..309dfed10b
--- /dev/null
+++ b/test/box/protocol.result
@@ -0,0 +1,2 @@
+return_code: 0
+return_code: 2
diff --git a/test/box/protocol.test b/test/box/protocol.test
new file mode 100644
index 0000000000..bde4516a73
--- /dev/null
+++ b/test/box/protocol.test
@@ -0,0 +1,7 @@
+import subprocess
+import sys
+
+p = subprocess.Popen([ "box/protocol" ], stdout=subprocess.PIPE)
+p.wait()
+for line in p.stdout.readlines():
+      sys.stdout.write(line)
-- 
GitLab