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

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Sat Mar 20 03:42:34 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
---
 src/box/lua/space.cc | 6 ++----
 src/lua/fiber.c      | 2 +-
 src/lua/utf8.c       | 5 ++---
 3 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/src/box/lua/space.cc b/src/box/lua/space.cc
index 544a18f47..8d4d8cc23 100644
--- a/src/box/lua/space.cc
+++ b/src/box/lua/space.cc
@@ -568,8 +568,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);
@@ -586,8 +585,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/fiber.c b/src/lua/fiber.c
index edbd06ebc..02ec3d158 100644
--- a/src/lua/fiber.c
+++ b/src/lua/fiber.c
@@ -337,7 +337,7 @@ lbox_fiber_top_entry(struct fiber *f, void *cb_ctx)
 {
 	struct lua_State *L = (struct lua_State *) cb_ctx;
 
-	lua_pushstring(L, tt_sprintf("%u/%s", f->fid, f->name));
+	lua_pushfstring(L, "%f/%s", (lua_Number)f->fid, f->name);
 
 	lua_newtable(L);
 
diff --git a/src/lua/utf8.c b/src/lua/utf8.c
index bf9bb98f4..e060fec8f 100644
--- a/src/lua/utf8.c
+++ b/src/lua/utf8.c
@@ -81,9 +81,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