From 841fdb35b3732200408927f34ea1703a123975db Mon Sep 17 00:00:00 2001
From: Vladimir Davydov <vdavydov@tarantool.org>
Date: Mon, 27 Sep 2021 14:52:11 +0300
Subject: [PATCH] Do not check return value of mh(new)

It never returns NULL anymore, because it uses xmalloc for memory
allocations.
---
 src/box/alter.cc           |  4 ----
 src/box/box.cc             |  4 +---
 src/box/coll_id_cache.c    | 14 +-------------
 src/box/coll_id_cache.h    |  3 +--
 src/box/func.c             |  8 +-------
 src/box/func.h             |  2 +-
 src/box/iproto.cc          |  6 ------
 src/box/lua/lib.c          |  2 --
 src/box/lua/net_box.c      | 10 ++--------
 src/box/memtx_bitset.c     |  2 --
 src/box/memtx_tx.c         | 11 -----------
 src/box/module_cache.c     |  8 +-------
 src/box/module_cache.h     |  2 +-
 src/box/session.cc         |  7 -------
 src/box/space.c            |  5 -----
 src/box/sql/func.c         |  2 --
 src/box/sql_stmt_cache.c   |  2 --
 src/box/tuple.c            |  8 ++------
 src/box/tuple_dictionary.c |  6 ------
 src/box/tuple_format.c     | 15 ++-------------
 src/box/tuple_format.h     |  3 +--
 src/box/vy_log.c           |  8 --------
 src/lib/coll/coll.c        |  3 +--
 src/lib/core/backtrace.cc  |  5 -----
 src/lib/json/json.c        |  3 +--
 src/lib/json/json.h        |  4 +---
 src/lib/swim/swim.c        |  6 ------
 test/unit/json.c           |  8 +++-----
 28 files changed, 20 insertions(+), 141 deletions(-)

diff --git a/src/box/alter.cc b/src/box/alter.cc
index 3bd56feb95..e43830ca6b 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -950,10 +950,6 @@ class AlterSpaceLock {
 	AlterSpaceLock(struct alter_space *alter) {
 		if (registry == NULL) {
 			registry = mh_i32_new();
-			if (registry == NULL) {
-				tnt_raise(OutOfMemory, 0, "mh_i32_new",
-					  "alter lock registry");
-			}
 		}
 		space_id = alter->old_space->def->id;
 		if (mh_i32_find(registry, space_id, NULL) != mh_end(registry)) {
diff --git a/src/box/box.cc b/src/box/box.cc
index 7b11d56d62..e082e1a3db 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -3467,9 +3467,7 @@ box_init(void)
 	 * as a default session user when running triggers.
 	 */
 	session_init();
-
-	if (schema_module_init() != 0)
-		diag_raise();
+	schema_module_init();
 
 	if (tuple_init(lua_hash) != 0)
 		diag_raise();
diff --git a/src/box/coll_id_cache.c b/src/box/coll_id_cache.c
index 22673efd06..1b3a45cca5 100644
--- a/src/box/coll_id_cache.c
+++ b/src/box/coll_id_cache.c
@@ -38,23 +38,11 @@ static struct mh_strnptr_t *coll_cache_name = NULL;
 /** mhash table (id -> collation) */
 static struct mh_i32ptr_t *coll_id_cache = NULL;
 
-int
+void
 coll_id_cache_init(void)
 {
 	coll_id_cache = mh_i32ptr_new();
-	if (coll_id_cache == NULL) {
-		diag_set(OutOfMemory, sizeof(*coll_id_cache), "malloc",
-			 "coll_id_cache");
-		return -1;
-	}
 	coll_cache_name = mh_strnptr_new();
-	if (coll_cache_name == NULL) {
-		diag_set(OutOfMemory, sizeof(*coll_cache_name), "malloc",
-			 "coll_cache_name");
-		mh_i32ptr_delete(coll_id_cache);
-		return -1;
-	}
-	return 0;
 }
 
 void
diff --git a/src/box/coll_id_cache.h b/src/box/coll_id_cache.h
index b959b2eb3e..d5325b6aee 100644
--- a/src/box/coll_id_cache.h
+++ b/src/box/coll_id_cache.h
@@ -40,9 +40,8 @@ struct coll_id;
 
 /**
  * Create global hash tables.
- * @return - 0 on success, -1 on memory error.
  */
-int
+void
 coll_id_cache_init(void);
 
 /** Delete global hash tables. */
diff --git a/src/box/func.c b/src/box/func.c
index 04ae1b9580..95a08b4af2 100644
--- a/src/box/func.c
+++ b/src/box/func.c
@@ -109,16 +109,10 @@ func_split_name(const char *str, struct func_name *name)
 /** Schema modules hash. */
 static struct mh_strnptr_t *modules = NULL;
 
-int
+void
 schema_module_init(void)
 {
 	modules = mh_strnptr_new();
-	if (modules == NULL) {
-		diag_set(OutOfMemory, sizeof(*modules), "malloc",
-			  "modules hash table");
-		return -1;
-	}
-	return 0;
 }
 
 void
diff --git a/src/box/func.h b/src/box/func.h
index 987ca70dbf..30e748dbbe 100644
--- a/src/box/func.h
+++ b/src/box/func.h
@@ -74,7 +74,7 @@ struct func {
 /**
  * Initialize schema modules subsystem.
  */
-int
+void
 schema_module_init(void);
 
 /**
diff --git a/src/box/iproto.cc b/src/box/iproto.cc
index 5d28422d71..9e73d7e327 100644
--- a/src/box/iproto.cc
+++ b/src/box/iproto.cc
@@ -1343,12 +1343,6 @@ iproto_connection_new(struct iproto_thread *iproto_thread, int fd)
 		return NULL;
 	}
 	con->streams = mh_i64ptr_new();
-	if (con->streams == NULL) {
-		diag_set(OutOfMemory, sizeof(*(con->streams)),
-			 "mh_streams_new", "streams");
-		mempool_free(&con->iproto_thread->iproto_connection_pool, con);
-		return NULL;
-	}
 	con->iproto_thread = iproto_thread;
 	con->input.data = con->output.data = con;
 	con->loop = loop();
diff --git a/src/box/lua/lib.c b/src/box/lua/lib.c
index fa44163e0b..8ee6c328c4 100644
--- a/src/box/lua/lib.c
+++ b/src/box/lua/lib.c
@@ -586,8 +586,6 @@ void
 box_lua_lib_init(struct lua_State *L)
 {
 	func_hash = mh_strnptr_new();
-	if (func_hash == NULL)
-		panic("box.lib: Can't allocate func hash table");
 
 	static const struct luaL_Reg top_methods[] = {
 		{ "load",		lbox_module_load	},
diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c
index aaa058a170..5393da09b7 100644
--- a/src/box/lua/net_box.c
+++ b/src/box/lua/net_box.c
@@ -314,7 +314,7 @@ netbox_request_push_result(struct netbox_request *request, struct lua_State *L)
 	return 1;
 }
 
-static int
+static void
 netbox_transport_create(struct netbox_transport *transport)
 {
 	ibuf_create(&transport->send_buf, &cord()->slabc, NETBOX_READAHEAD);
@@ -322,11 +322,6 @@ netbox_transport_create(struct netbox_transport *transport)
 	fiber_cond_create(&transport->on_send_buf_empty);
 	transport->next_sync = 1;
 	transport->requests = mh_i64ptr_new();
-	if (transport->requests == NULL) {
-		diag_set(OutOfMemory, 0, "mhash", "netbox_transport");
-		return -1;
-	}
-	return 0;
 }
 
 static void
@@ -1774,8 +1769,7 @@ netbox_new_transport(struct lua_State *L)
 {
 	struct netbox_transport *transport = lua_newuserdata(
 		L, sizeof(*transport));
-	if (netbox_transport_create(transport) != 0)
-		luaT_error(L);
+	netbox_transport_create(transport);
 	luaL_getmetatable(L, netbox_transport_typename);
 	lua_setmetatable(L, -2);
 	return 1;
diff --git a/src/box/memtx_bitset.c b/src/box/memtx_bitset.c
index 75ff8446fc..7275266fe2 100644
--- a/src/box/memtx_bitset.c
+++ b/src/box/memtx_bitset.c
@@ -548,8 +548,6 @@ memtx_bitset_index_new(struct memtx_engine *memtx, struct index_def *def)
 		      memtx_index_extent_alloc, memtx_index_extent_free, memtx);
 
 	index->tuple_to_id = mh_bitset_index_new();
-	if (index->tuple_to_id == NULL)
-		panic("failed to allocate memtx bitset index");
 #endif /* #ifndef OLD_GOOD_BITSET */
 
 	tt_bitset_index_create(&index->index, realloc);
diff --git a/src/box/memtx_tx.c b/src/box/memtx_tx.c
index 18c6ebf5e0..733a63bd61 100644
--- a/src/box/memtx_tx.c
+++ b/src/box/memtx_tx.c
@@ -241,13 +241,9 @@ memtx_tx_manager_init()
 			       cord_slab_cache(), item_size);
 	}
 	txm.history = mh_history_new();
-	if (txm.history == NULL)
-		panic("mh_history_new()");
 	mempool_create(&txm.point_hole_item_pool,
 		       cord_slab_cache(), sizeof(struct point_hole_item));
 	txm.point_holes = mh_point_holes_new();
-	if (txm.point_holes == NULL)
-		panic("mh_history_new()");
 	mempool_create(&txm.gap_item_mempoool,
 		       cord_slab_cache(), sizeof(struct gap_item));
 	mempool_create(&txm.full_scan_item_mempool,
@@ -2464,13 +2460,6 @@ memtx_tx_snapshot_cleaner_create(struct memtx_tx_snapshot_cleaner *cleaner,
 	if (space == NULL || rlist_empty(&space->memtx_stories))
 		return 0;
 	struct mh_snapshot_cleaner_t *ht = mh_snapshot_cleaner_new();
-	if (ht == NULL) {
-		diag_set(OutOfMemory, sizeof(*ht),
-			 index_name, "snapshot cleaner");
-		free(ht);
-		return -1;
-	}
-
 	struct memtx_story *story;
 	rlist_foreach_entry(story, &space->memtx_stories, in_space_stories) {
 		struct tuple *tuple = story->tuple;
diff --git a/src/box/module_cache.c b/src/box/module_cache.c
index 7f81902bd1..fc87e44346 100644
--- a/src/box/module_cache.c
+++ b/src/box/module_cache.c
@@ -474,14 +474,8 @@ module_free(void)
 	module_cache = NULL;
 }
 
-int
+void
 module_init(void)
 {
 	module_cache = mh_strnptr_new();
-	if (module_cache == NULL) {
-		diag_set(OutOfMemory, sizeof(*module_cache),
-			 "malloc", "module_cache");
-		return -1;
-	}
-	return 0;
 }
diff --git a/src/box/module_cache.h b/src/box/module_cache.h
index 24fdf8d85c..d767aec762 100644
--- a/src/box/module_cache.h
+++ b/src/box/module_cache.h
@@ -193,7 +193,7 @@ void
 module_unref(struct module *m);
 
 /** Initialize modules subsystem. */
-int
+void
 module_init(void);
 
 /** Free modules subsystem. */
diff --git a/src/box/session.cc b/src/box/session.cc
index 7ba7235feb..963ec1973d 100644
--- a/src/box/session.cc
+++ b/src/box/session.cc
@@ -193,11 +193,6 @@ session_add_stmt_id(struct session *session, uint32_t id)
 {
 	if (session->sql_stmts == NULL) {
 		session->sql_stmts = mh_i32ptr_new();
-		if (session->sql_stmts == NULL) {
-			diag_set(OutOfMemory, 0, "mh_i32ptr_new",
-				 "session stmt hash");
-			return -1;
-		}
 	}
 	return sql_session_stmt_hash_add_id(session->sql_stmts, id);
 }
@@ -281,8 +276,6 @@ void
 session_init(void)
 {
 	session_registry = mh_i64ptr_new();
-	if (session_registry == NULL)
-		panic("out of memory");
 	mempool_create(&session_pool, &cord()->slabc, sizeof(struct session));
 	credentials_create(&admin_credentials, admin_user);
 	session_settings_init();
diff --git a/src/box/space.c b/src/box/space.c
index c6d6a630ee..db53e4e615 100644
--- a/src/box/space.c
+++ b/src/box/space.c
@@ -207,11 +207,6 @@ space_create(struct space *space, struct engine *engine,
 		}
 	}
 	space->constraint_ids = mh_strnptr_new();
-	if (space->constraint_ids == NULL) {
-		diag_set(OutOfMemory, sizeof(*space->constraint_ids), "malloc",
-			 "constraint_ids");
-		goto fail;
-	}
 	rlist_create(&space->memtx_stories);
 	return 0;
 
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 9009f9e4f2..362793a8ed 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -2358,8 +2358,6 @@ void
 sql_built_in_functions_cache_init(void)
 {
 	built_in_functions = mh_strnptr_new();
-	if (built_in_functions == NULL)
-		panic("Out of memory on creating SQL built-in functions hash");
 	for (uint32_t i = 0; i < nelem(dictionaries); ++i)
 		built_in_func_put(&dictionaries[i]);
 
diff --git a/src/box/sql_stmt_cache.c b/src/box/sql_stmt_cache.c
index 66f7a7f8aa..3b4f82911d 100644
--- a/src/box/sql_stmt_cache.c
+++ b/src/box/sql_stmt_cache.c
@@ -42,8 +42,6 @@ void
 sql_stmt_cache_init(void)
 {
 	sql_stmt_cache.hash = mh_i32ptr_new();
-	if (sql_stmt_cache.hash == NULL)
-		panic("out of memory");
 	sql_stmt_cache.mem_quota = 0;
 	sql_stmt_cache.mem_used = 0;
 	rlist_create(&sql_stmt_cache.gc_queue);
diff --git a/src/box/tuple.c b/src/box/tuple.c
index 7174584b4c..75e4416d1d 100644
--- a/src/box/tuple.c
+++ b/src/box/tuple.c
@@ -292,9 +292,7 @@ tuple_bigref_tuple_count()
 int
 tuple_init(field_name_hash_f hash)
 {
-	if (tuple_format_init() != 0)
-		return -1;
-
+	tuple_format_init();
 	field_name_hash = hash;
 	/*
 	 * Create a format for runtime tuples
@@ -320,9 +318,7 @@ tuple_init(field_name_hash_f hash)
 
 	tuple_uploaded_refs = mh_tuple_uploaded_refs_new();
 
-	if (coll_id_cache_init() != 0)
-		return -1;
-
+	coll_id_cache_init();
 	return 0;
 }
 
diff --git a/src/box/tuple_dictionary.c b/src/box/tuple_dictionary.c
index 25157efa82..a626a43f4e 100644
--- a/src/box/tuple_dictionary.c
+++ b/src/box/tuple_dictionary.c
@@ -145,11 +145,6 @@ tuple_dictionary_new(const struct field_def *fields, uint32_t field_count)
 		goto err_memory;
 	}
 	dict->hash = mh_strnu32_new();
-	if (dict->hash == NULL) {
-		diag_set(OutOfMemory, sizeof(*dict->hash),
-			 "mh_strnu32_new", "dict->hash");
-		goto err_hash;
-	}
 	mh_strnu32_reserve(dict->hash, field_count, NULL);
 	char *pos = (char *) dict->names + names_offset;
 	for (uint32_t i = 0; i < field_count; ++i) {
@@ -165,7 +160,6 @@ tuple_dictionary_new(const struct field_def *fields, uint32_t field_count)
 
 err_name:
 	tuple_dictionary_delete_hash(dict->hash);
-err_hash:
 	free(dict->names);
 err_memory:
 	free(dict);
diff --git a/src/box/tuple_format.c b/src/box/tuple_format.c
index ad84f2d1de..80eed4b434 100644
--- a/src/box/tuple_format.c
+++ b/src/box/tuple_format.c
@@ -649,12 +649,7 @@ tuple_format_alloc(struct key_def * const *keys, uint16_t key_count,
 			 "tuple format");
 		return NULL;
 	}
-	if (json_tree_create(&format->fields) != 0) {
-		diag_set(OutOfMemory, 0, "json_lexer_create",
-			 "tuple field tree");
-		free(format);
-		return NULL;
-	}
+	json_tree_create(&format->fields);
 	for (uint32_t fieldno = 0; fieldno < field_count; fieldno++) {
 		struct tuple_field *field = tuple_field_new();
 		if (field == NULL)
@@ -981,16 +976,10 @@ tuple_format_min_field_count(struct key_def * const *keys, uint16_t key_count,
 	return min_field_count;
 }
 
-int
+void
 tuple_format_init()
 {
 	tuple_formats_hash = mh_tuple_format_new();
-	if (tuple_formats_hash == NULL) {
-		diag_set(OutOfMemory, sizeof(struct mh_tuple_format_t), "malloc",
-			 "tuple format hash");
-		return -1;
-	}
-	return 0;
 }
 
 /** Destroy tuple format subsystem and free resourses */
diff --git a/src/box/tuple_format.h b/src/box/tuple_format.h
index c94bfc1ec5..b6d912790c 100644
--- a/src/box/tuple_format.h
+++ b/src/box/tuple_format.h
@@ -434,9 +434,8 @@ tuple_field_map_create(struct tuple_format *format, const char *tuple,
 
 /**
  * Initialize tuple format subsystem.
- * @retval 0 on success, -1 otherwise.
  */
-int
+void
 tuple_format_init();
 
 
diff --git a/src/box/vy_log.c b/src/box/vy_log.c
index 8214b054e2..cd608fc450 100644
--- a/src/box/vy_log.c
+++ b/src/box/vy_log.c
@@ -2397,14 +2397,6 @@ vy_recovery_new_f(va_list ap)
 	recovery->range_hash = mh_i64ptr_new();
 	recovery->run_hash = mh_i64ptr_new();
 	recovery->slice_hash = mh_i64ptr_new();
-	if (recovery->index_id_hash == NULL ||
-	    recovery->lsm_hash == NULL ||
-	    recovery->range_hash == NULL ||
-	    recovery->run_hash == NULL ||
-	    recovery->slice_hash == NULL) {
-		diag_set(OutOfMemory, 0, "mh_i64ptr_new", "mh_i64ptr_t");
-		goto fail_free;
-	}
 
 	/*
 	 * We don't create a log file if there are no objects to
diff --git a/src/lib/coll/coll.c b/src/lib/coll/coll.c
index 9d528df193..c94777a85d 100644
--- a/src/lib/coll/coll.c
+++ b/src/lib/coll/coll.c
@@ -423,8 +423,7 @@ coll_init(void)
 	coll_cache = mh_coll_new();
 	icu_ucase_default_map = ucasemap_open("", 0, &err);
 	icu_utf8_conv = ucnv_open("utf8", &err);
-	if (coll_cache == NULL || icu_ucase_default_map == NULL ||
-	    icu_utf8_conv == NULL)
+	if (icu_ucase_default_map == NULL || icu_utf8_conv == NULL)
 		panic("Can not create system collations cache");
 }
 
diff --git a/src/lib/core/backtrace.cc b/src/lib/core/backtrace.cc
index 85c1aaefd0..d6641347d2 100644
--- a/src/lib/core/backtrace.cc
+++ b/src/lib/core/backtrace.cc
@@ -94,11 +94,6 @@ get_proc_name(unw_cursor_t *unw_cur, unw_word_t *offset, bool skip_cache)
 	if (proc_cache == NULL) {
 		region_create(&cache_region, &cord()->slabc);
 		proc_cache = mh_i64ptr_new();
-		if (proc_cache == NULL) {
-			unw_get_proc_name(unw_cur, proc_name, sizeof(proc_name),
-					  offset);
-			goto error;
-		}
 	}
 
 	k  = mh_i64ptr_find(proc_cache, ip, NULL);
diff --git a/src/lib/json/json.c b/src/lib/json/json.c
index c4aa1367e9..0cfb0bfeec 100644
--- a/src/lib/json/json.c
+++ b/src/lib/json/json.c
@@ -446,7 +446,7 @@ json_token_hash(struct json_token *token)
 	return PMurHash32_Result(h, carry, data_size);
 }
 
-int
+void
 json_tree_create(struct json_tree *tree)
 {
 	memset(tree, 0, sizeof(struct json_tree));
@@ -455,7 +455,6 @@ json_tree_create(struct json_tree *tree)
 	tree->root.max_child_idx = -1;
 	tree->root.sibling_idx = -1;
 	tree->hash = mh_json_new();
-	return tree->hash == NULL ? -1 : 0;
 }
 
 static void
diff --git a/src/lib/json/json.h b/src/lib/json/json.h
index 3218769a11..9ea8eff2f2 100644
--- a/src/lib/json/json.h
+++ b/src/lib/json/json.h
@@ -330,10 +330,8 @@ json_tree_snprint_path(char *buf, int size, const struct json_token *token,
 
 /**
  * Initialize a new empty JSON tree.
- *
- * Returns 0 on success, -1 on memory allocation error.
  */
-int
+void
 json_tree_create(struct json_tree *tree);
 
 /**
diff --git a/src/lib/swim/swim.c b/src/lib/swim/swim.c
index 3955c31309..98cafd1601 100644
--- a/src/lib/swim/swim.c
+++ b/src/lib/swim/swim.c
@@ -1907,12 +1907,6 @@ swim_new(uint64_t generation)
 	}
 	swim->initial_generation = generation;
 	swim->members = mh_swim_table_new();
-	if (swim->members == NULL) {
-		free(swim);
-		diag_set(OutOfMemory, sizeof(*swim->members),
-			 "mh_swim_table_new", "members");
-		return NULL;
-	}
 	rlist_create(&swim->round_queue);
 	swim_ev_timer_init(&swim->round_tick, swim_begin_step,
 			   0, HEARTBEAT_RATE_DEFAULT);
diff --git a/test/unit/json.c b/test/unit/json.c
index 4d3423142a..5b2fad4715 100644
--- a/test/unit/json.c
+++ b/test/unit/json.c
@@ -219,8 +219,7 @@ test_tree()
 	plan(65);
 
 	struct json_tree tree;
-	int rc = json_tree_create(&tree);
-	fail_if(rc != 0);
+	json_tree_create(&tree);
 
 	struct test_struct records[7];
 	for (int i = 0; i < 6; i++)
@@ -519,8 +518,7 @@ test_path_snprint()
 	plan(9);
 
 	struct json_tree tree;
-	int rc = json_tree_create(&tree);
-	fail_if(rc != 0);
+	json_tree_create(&tree);
 	struct test_struct records[6];
 	const char *path = "[1][*][20][\"file\"][8]";
 	int path_len = strlen(path);
@@ -533,7 +531,7 @@ test_path_snprint()
 	char buf[64];
 	int bufsz = sizeof(buf);
 
-	rc = json_tree_snprint_path(buf, bufsz, &node->node, INDEX_BASE);
+	int rc = json_tree_snprint_path(buf, bufsz, &node->node, INDEX_BASE);
 	is(rc, path_len, "full path - retval");
 	is(buf[path_len], '\0', "full path - terminating nul");
 	is(memcmp(buf, path, path_len), 0, "full path - output");
-- 
GitLab