[Tarantool-patches] [PATCH 11/15] lua: use lua_pushfstring() instead of tt_sprintf()

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Thu Mar 25 00:24:23 MSK 2021


In a few places to push a formatted string was used 2 calls:
tt_sprintf() + lua_pushstring(). It wasn't necessary because Lua
API has lua_pushfstring() with a big enough subset of printf
format features.

But more importantly - it was a bug. lua_pushstring() is a GC
point. Before copying the passed string it tries to invoke Lua GC,
which might invoke a __gc handler for some cdata, where static
alloc might be used, and it can rewrite the string passed to
lua_pushstring() in the beginning of the stack.

Part of #5632

(cherry picked from commit b3872a38b1e4be6a84d0621c833a196baf70284f)
---
 src/box/lua/space.cc | 6 ++----
 src/lua/utf8.c       | 5 ++---
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/src/box/lua/space.cc b/src/box/lua/space.cc
index 9ea0d6f7e..962796e6a 100644
--- a/src/box/lua/space.cc
+++ b/src/box/lua/space.cc
@@ -480,8 +480,7 @@ lbox_space_frommap(struct lua_State *L)
 	space = space_by_id(id);
 	if (space == NULL) {
 		lua_pushnil(L);
-		lua_pushstring(L, tt_sprintf("Space with id '%d' "\
-					     "doesn't exist", id));
+		lua_pushfstring(L, "Space with id '%d' doesn't exist", id);
 		return 2;
 	}
 	assert(space->format != NULL);
@@ -498,8 +497,7 @@ lbox_space_frommap(struct lua_State *L)
 		if (tuple_fieldno_by_name(dict, key, key_len, key_hash,
 					  &fieldno)) {
 			lua_pushnil(L);
-			lua_pushstring(L, tt_sprintf("Unknown field '%s'",
-						     key));
+			lua_pushfstring(L, "Unknown field '%s'", key);
 			return 2;
 		}
 		lua_rawseti(L, -3, fieldno+1);
diff --git a/src/lua/utf8.c b/src/lua/utf8.c
index 0d9c49a8b..53cdcb023 100644
--- a/src/lua/utf8.c
+++ b/src/lua/utf8.c
@@ -83,9 +83,8 @@ utf8_str_to_case(struct lua_State *L, const char *src, int src_bsize,
 		} else {
 			cord_ibuf_put(ibuf);
 			lua_pushnil(L);
-			lua_pushstring(L, tt_sprintf("error during ICU case "\
-						     "transform: %s",
-						     u_errorName(err)));
+			lua_pushfstring(L, "error during ICU case "
+					"transform: %s", u_errorName(err));
 			return 2;
 		}
 		/*
-- 
2.24.3 (Apple Git-128)



More information about the Tarantool-patches mailing list