Skip to content

Commit d63583f

Browse files
authored
Merge pull request ceph#63819 from tchaikov/wip-lua-fix-leak
rgw/rgw_lua_utils: fix memory leak in luaL_error() formatting Reviewed-by: Yuval Lifshitz <[email protected]>
2 parents 3fb436a + d8adc49 commit d63583f

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/rgw/rgw_lua_utils.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,16 @@ void lua_state_guard::runtime_hook(lua_State* L, lua_Debug* ar) {
241241
std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
242242

243243
if (elapsed > max_runtime) {
244-
std::string err = "Lua runtime limit exceeded: total elapsed time is " +
245-
std::to_string(elapsed.count()) + " ms";
246-
luaL_error(L, "%s", err.c_str());
244+
// +1 for max digit, +1 for null terminator
245+
constexpr size_t max_digits = std::numeric_limits<decltype(elapsed)::rep>::digits10 + 2;
246+
char elapsed_str[max_digits] = {};
247+
auto [ptr, ec] = std::to_chars(std::begin(elapsed_str), std::end(elapsed_str), elapsed.count());
248+
// buffer too small, should never happen though
249+
assert(ec == std::errc{});
250+
*ptr = '\0';
251+
// luaL_error() never returns, so we have to format the number in the hard way instead of
252+
// using std::to_string or fmt::to_string
253+
luaL_error(L, "Lua runtime limit exceeded: total elapsed time is %s ms", elapsed_str);
247254
}
248255
}
249256

0 commit comments

Comments
 (0)