From cb8e1a321a7f2de15682e8b988bae76178c9007b Mon Sep 17 00:00:00 2001
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Date: Tue, 11 Jul 2017 20:08:30 +0300
Subject: [PATCH] schema: increase BOX_NAME_MAX

Closes #944
---
 src/box/alter.cc               | 12 ++++++----
 src/box/key_def.h              |  5 +++-
 test/box/access.result         | 42 ++++++++++++++++++++++++++++++++++
 test/box/access.test.lua       | 20 ++++++++++++++++
 test/box/alter_limits.result   | 24 +++++++++----------
 test/box/alter_limits.test.lua | 10 ++++----
 6 files changed, 91 insertions(+), 22 deletions(-)

diff --git a/src/box/alter.cc b/src/box/alter.cc
index 05826ee4b3..ad9ac3c07e 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -369,7 +369,8 @@ index_def_new_from_tuple(struct tuple *tuple, struct space *old_space)
 		parts = tuple_field(tuple, BOX_INDEX_FIELD_PARTS_165);
 	}
 	if (name_len > BOX_NAME_MAX)
-		tnt_raise(ClientError, ER_MODIFY_INDEX, tt_cstr(name, name_len),
+		tnt_raise(ClientError, ER_MODIFY_INDEX,
+			  tt_cstr(name, BOX_INVALID_NAME_MAX),
 			  space_name(old_space), "index name is too long");
 	index_def = index_def_new(id, index_id, name, name_len, type, &opts,
 				  part_count);
@@ -438,7 +439,8 @@ space_def_new_from_tuple(struct tuple *tuple, uint32_t errcode)
 	const char *name =
 		tuple_field_str_xc(tuple, BOX_SPACE_FIELD_NAME, &name_len);
 	if (name_len > BOX_NAME_MAX)
-		tnt_raise(ClientError, errcode, tt_cstr(name, BOX_NAME_MAX),
+		tnt_raise(ClientError, errcode,
+			  tt_cstr(name, BOX_INVALID_NAME_MAX),
 			  "space name is too long");
 	size_t size = space_def_sizeof(name_len);
 	struct space_def *def = (struct space_def *) malloc(size);
@@ -1669,7 +1671,8 @@ user_def_new_from_tuple(struct tuple *tuple)
 					      &name_len);
 	if (name_len > BOX_NAME_MAX) {
 		tnt_raise(ClientError, ER_CREATE_USER,
-			  tt_cstr(name, name_len), "user name is too long");
+			  tt_cstr(name, BOX_INVALID_NAME_MAX),
+			  "user name is too long");
 	}
 	size_t size = user_def_sizeof(name_len);
 	/* Use calloc: in case user password is empty, fill it with \0 */
@@ -1813,7 +1816,8 @@ func_def_new_from_tuple(const struct tuple *tuple)
 					      &len);
 	if (len > BOX_NAME_MAX)
 		tnt_raise(ClientError, ER_CREATE_FUNCTION,
-			  tt_cstr(name, len), "function name is too long");
+			  tt_cstr(name, BOX_INVALID_NAME_MAX),
+			  "function name is too long");
 	struct func_def *def = (struct func_def *) malloc(func_def_sizeof(len));
 	if (def == NULL)
 		tnt_raise(OutOfMemory, func_def_sizeof(len), "malloc", "def");
diff --git a/src/box/key_def.h b/src/box/key_def.h
index cd19fa29fd..3698550619 100644
--- a/src/box/key_def.h
+++ b/src/box/key_def.h
@@ -50,7 +50,8 @@ enum {
 	BOX_SPACE_MAX = INT32_MAX,
 	BOX_FUNCTION_MAX = 32000,
 	BOX_INDEX_MAX = 128,
-	BOX_NAME_MAX = 64,
+	BOX_NAME_MAX = 65000,
+	BOX_INVALID_NAME_MAX = 64,
 	ENGINE_NAME_MAX = 16,
 	FIELD_TYPE_NAME_MAX = 16,
 	GRANT_NAME_MAX = 16,
@@ -66,6 +67,8 @@ enum {
 	 */
 	BOX_INDEX_PART_MAX = UINT8_MAX
 };
+static_assert(BOX_INVALID_NAME_MAX <= BOX_NAME_MAX,
+	      "invalid name max is less than name max");
 
 /*
  * Different objects which can be subject to access
diff --git a/test/box/access.result b/test/box/access.result
index c954d37e0a..2d78c49080 100644
--- a/test/box/access.result
+++ b/test/box/access.result
@@ -139,6 +139,27 @@ box.space['_user']:delete{uid}
 box.schema.user.drop('test')
 ---
 ...
+-- gh-944 name is too long
+name = string.rep('a', box.schema.NAME_MAX - 1)
+---
+...
+box.schema.user.create(name..'aa')
+---
+- error: 'Failed to create user ''aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'':
+    user name is too long'
+...
+box.schema.user.create(name..'a')
+---
+...
+box.schema.user.drop(name..'a')
+---
+...
+box.schema.user.create(name)
+---
+...
+box.schema.user.drop(name)
+---
+...
 -- sudo
 box.schema.user.create('tester')
 ---
@@ -720,6 +741,27 @@ box.space._user.index.name:delete{'public'}
 ---
 - true
 ...
+-- gh-944 name is too long
+name = string.rep('a', box.schema.NAME_MAX - 1)
+---
+...
+box.schema.func.create(name..'aa')
+---
+- error: 'Failed to create function ''aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'':
+    function name is too long'
+...
+box.schema.func.create(name..'a')
+---
+...
+box.schema.func.drop(name..'a')
+---
+...
+box.schema.func.create(name)
+---
+...
+box.schema.func.drop(name)
+---
+...
 -- A test case for: http://bugs.launchpad.net/bugs/712456
 -- Verify that when trying to access a non-existing or
 -- very large space id, no crash occurs.
diff --git a/test/box/access.test.lua b/test/box/access.test.lua
index 5ca1bfd1d2..35dfa1ee70 100644
--- a/test/box/access.test.lua
+++ b/test/box/access.test.lua
@@ -62,6 +62,16 @@ box.schema.user.revoke('rich', 'public')
 box.space['_user']:delete{uid}
 box.schema.user.drop('test')
 
+-- gh-944 name is too long
+name = string.rep('a', box.schema.NAME_MAX - 1)
+box.schema.user.create(name..'aa')
+
+box.schema.user.create(name..'a')
+box.schema.user.drop(name..'a')
+
+box.schema.user.create(name)
+box.schema.user.drop(name)
+
 -- sudo
 box.schema.user.create('tester')
 -- admin -> user
@@ -275,6 +285,16 @@ box.schema.role.drop('public')
 box.space._user.index.name:delete{'public'}
 #box.schema.role.info('public') > 0
 
+-- gh-944 name is too long
+name = string.rep('a', box.schema.NAME_MAX - 1)
+box.schema.func.create(name..'aa')
+
+box.schema.func.create(name..'a')
+box.schema.func.drop(name..'a')
+
+box.schema.func.create(name)
+box.schema.func.drop(name)
+
 -- A test case for: http://bugs.launchpad.net/bugs/712456
 -- Verify that when trying to access a non-existing or
 -- very large space id, no crash occurs.
diff --git a/test/box/alter_limits.result b/test/box/alter_limits.result
index de519be478..ed65baa856 100644
--- a/test/box/alter_limits.result
+++ b/test/box/alter_limits.result
@@ -25,7 +25,7 @@ box.schema.INDEX_FIELD_MAX
 ...
 box.schema.NAME_MAX
 ---
-- 64
+- 65000
 ...
 box.schema.INDEX_ID
 ---
@@ -105,9 +105,9 @@ s:drop()
 ---
 ...
 -- too long space name
-box.schema.space.create(string.rep('tweedledee', 100))
+box.schema.space.create(string.rep('t', box.schema.NAME_MAX + 1))
 ---
-- error: 'Failed to create space ''tweedledeetweedledeetweedledeetweedledeetweedledeetweedledeetwee'':
+- error: 'Failed to create space ''tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt'':
     space name is too long'
 ...
 -- too long space engine name
@@ -124,9 +124,9 @@ box.schema.space.create(string.rep('t', box.schema.NAME_MAX)..'_')
 s = box.schema.space.create(string.rep('t', box.schema.NAME_MAX - 1)..'_')
 ---
 ...
-s.name
+s.name:len()
 ---
-- ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt_
+- 65000
 ...
 s:drop()
 ---
@@ -134,9 +134,9 @@ s:drop()
 s = box.schema.space.create(string.rep('t', box.schema.NAME_MAX - 2)..'_')
 ---
 ...
-s.name
+s.name:len()
 ---
-- tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt_
+- 64999
 ...
 s:drop()
 ---
@@ -626,7 +626,7 @@ s.index.new.name
 -- too long name
 s.index[0]:rename(string.rep('t', box.schema.NAME_MAX)..'_')
 ---
-- error: 'Can''t create or modify index ''tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt_''
+- error: 'Can''t create or modify index ''tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt''
     in space ''test'': index name is too long'
 ...
 s.index[0].name
@@ -636,16 +636,16 @@ s.index[0].name
 s.index[0]:rename(string.rep('t', box.schema.NAME_MAX - 1)..'_')
 ---
 ...
-s.index[0].name
+s.index[0].name:len()
 ---
-- ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt_
+- 65000
 ...
 s.index[0]:rename(string.rep('t', box.schema.NAME_MAX - 2)..'_')
 ---
 ...
-s.index[0].name
+s.index[0].name:len()
 ---
-- tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt_
+- 64999
 ...
 s.index[0]:rename('primary')
 ---
diff --git a/test/box/alter_limits.test.lua b/test/box/alter_limits.test.lua
index 5c72b4527d..ec6fb02aac 100644
--- a/test/box/alter_limits.test.lua
+++ b/test/box/alter_limits.test.lua
@@ -40,16 +40,16 @@ box.schema.space.create('tweedledee', { id = 3000 })
 box.schema.space.create('tweedledee', { id = 'tweedledee' })
 s:drop()
 -- too long space name
-box.schema.space.create(string.rep('tweedledee', 100))
+box.schema.space.create(string.rep('t', box.schema.NAME_MAX + 1))
 -- too long space engine name
 box.schema.space.create('tweedleedee', { engine = string.rep('too-long', 100) })
 -- space name limit
 box.schema.space.create(string.rep('t', box.schema.NAME_MAX)..'_')
 s = box.schema.space.create(string.rep('t', box.schema.NAME_MAX - 1)..'_')
-s.name
+s.name:len()
 s:drop()
 s = box.schema.space.create(string.rep('t', box.schema.NAME_MAX - 2)..'_')
-s.name
+s.name:len()
 s:drop()
 -- space with no indexes - test update, delete, select, truncate
 s = box.schema.space.create('tweedledum')
@@ -226,9 +226,9 @@ s.index.new.name
 s.index[0]:rename(string.rep('t', box.schema.NAME_MAX)..'_')
 s.index[0].name
 s.index[0]:rename(string.rep('t', box.schema.NAME_MAX - 1)..'_')
-s.index[0].name
+s.index[0].name:len()
 s.index[0]:rename(string.rep('t', box.schema.NAME_MAX - 2)..'_')
-s.index[0].name
+s.index[0].name:len()
 s.index[0]:rename('primary')
 s.index.primary.name
 -- cleanup
-- 
GitLab