From 5d77d1c012a47507b7652b8cf42b74790acf6d76 Mon Sep 17 00:00:00 2001 From: George Zhao Date: Thu, 10 Oct 2024 10:44:45 +0800 Subject: [PATCH] f --- src/misc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/misc.c b/src/misc.c index 668bbcb9..f7891ee7 100644 --- a/src/misc.c +++ b/src/misc.c @@ -808,11 +808,11 @@ static int luv_utf16_to_wtf8(lua_State *L) { const uint16_t *utf16 = (const uint16_t *)luaL_checklstring(L, 1, &sz); ssize_t utf16_len = luaL_optinteger(L, 2, sz/2); /* pad NUL terminator */ - uint16_t *ws = malloc(sz+2); - if (ws== NULL) return luaL_error(L, "failed to allocate %zu bytes", sz + 2); - memcpy(ws, utf16, sz); - ws[sz/2] = 0; - sz = uv_utf16_length_as_wtf8(utf16, utf16_len); + uint16_t *ws = malloc(2*(utf16_len+1)); + if (ws== NULL) return luaL_error(L, "failed to allocate %zu bytes", 2*(utf16_len+1)); + memcpy(ws, utf16, 2*utf16_len); + ws[utf16_len] = 0; + sz = uv_utf16_length_as_wtf8(utf16, utf16_len+1); wtf8 = malloc(sz + 1); if (wtf8 == NULL) return luaL_error(L, "failed to allocate %zu bytes", sz + 1); ret = uv_utf16_to_wtf8(ws, utf16_len+1, &wtf8, &sz);