[Tarantool-patches] [PATCH] net.box: allow to store user-defined fields in future object

Vladimir Davydov vdavydov at tarantool.org
Wed Aug 11 10:49:38 MSK 2021


On Tue, Aug 10, 2021 at 08:15:27PM +0300, Cyrill Gorcunov via Tarantool-patches wrote:
> On Tue, Aug 10, 2021 at 07:53:54PM +0300, Vladimir Davydov via Tarantool-patches wrote:
> > +
> > +static int
> > +luaT_netbox_request_newindex(struct lua_State *L)
> > +{
> > +	struct netbox_request *request = luaT_check_netbox_request(L, 1);
> > +	struct mh_strnptr_t *h = request->index;
> > +	size_t field_name_len;
> > +	const char *field_name = lua_tolstring(L, 2, &field_name_len);
> > +	if (field_name == NULL)
> > +		return luaL_error(L, "invalid index");
> > +	int field_value_ref = luaL_ref(L, LUA_REGISTRYINDEX);
> > +	if (field_value_ref == LUA_REFNIL) {
> > +		/* The field is set to nil. Delete it from the index. */
> > +		if (h == NULL)
> > +			return 0;
> > +		mh_int_t k = mh_strnptr_find_inp(h, field_name,
> > +						 field_name_len);
> 
> We're guaranteed that field_name present in a hash and never
> ever return nil, right?

Of course, we are not. Fixed. Thanks!

Incremental diff:
--
diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c
index 162ff6c82fb0..6ab0f71cdbe9 100644
--- a/src/box/lua/net_box.c
+++ b/src/box/lua/net_box.c
@@ -1413,9 +1413,11 @@ luaT_netbox_request_newindex(struct lua_State *L)
 			return 0;
 		mh_int_t k = mh_strnptr_find_inp(h, field_name,
 						 field_name_len);
-		int ref = (intptr_t)mh_strnptr_node(h, k)->val;
-		luaL_unref(L, LUA_REGISTRYINDEX, ref);
-		mh_strnptr_del(h, k, NULL);
+		if (k != mh_end(h)) {
+			int ref = (intptr_t)mh_strnptr_node(h, k)->val;
+			luaL_unref(L, LUA_REGISTRYINDEX, ref);
+			mh_strnptr_del(h, k, NULL);
+		}
 		return 0;
 	}
 	if (h == NULL) {
diff --git a/test/box/net.box_fiber-async_gh-3107.result b/test/box/net.box_fiber-async_gh-3107.result
index e44db726c8cb..1c48a115e6b5 100644
--- a/test/box/net.box_fiber-async_gh-3107.result
+++ b/test/box/net.box_fiber-async_gh-3107.result
@@ -109,6 +109,13 @@ future.abc -- nil
 ---
 - null
 ...
+future.abc = nil
+---
+...
+future.abc -- nil
+---
+- null
+...
 future.abc = 'abc'
 ---
 ...
@@ -123,6 +130,13 @@ future.abc -- nil
 ---
 - null
 ...
+future.abc = nil
+---
+...
+future.abc -- nil
+---
+- null
+...
 future[{}] -- error
 ---
 - error: '[string "return future[{}] -- error "]:1: invalid index'
diff --git a/test/box/net.box_fiber-async_gh-3107.test.lua b/test/box/net.box_fiber-async_gh-3107.test.lua
index 5571aeb935e0..e08bad056cba 100644
--- a/test/box/net.box_fiber-async_gh-3107.test.lua
+++ b/test/box/net.box_fiber-async_gh-3107.test.lua
@@ -38,10 +38,14 @@ err:find('Usage') ~= nil
 -- Storing user data in future object.
 future = c:eval('return 123', {}, {is_async = true})
 future.abc -- nil
+future.abc = nil
+future.abc -- nil
 future.abc = 'abc'
 future.abc -- abc
 future.abc = nil
 future.abc -- nil
+future.abc = nil
+future.abc -- nil
 future[{}] -- error
 future[{}] = 'abc' -- error
 future:wait_result() -- 123


More information about the Tarantool-patches mailing list