BUG/MINOR: lua: remove incorrect usage of strncat()

As every time strncat() is used, it's wrong, and this one is no exception.
Users often think that the length applies to the destination except it
applies to the source and makes it hard to use correctly. The bug did not
have an impact because the length was preallocated from the sum of all
the individual lengths as measured by strlen() so there was no chance one
of them would change in between. But it could change in the future. Let's
fix it to use memcpy() instead for strings, or byte copies for delimiters.

No backport is needed, though it can be done if it helps to apply other
fixes.
This commit is contained in:
Willy Tarreau 2023-04-07 15:27:55 +02:00
parent ead43fe4f2
commit 22450af22a

View File

@ -10954,6 +10954,7 @@ __LJMP static int hlua_register_cli(lua_State *L)
const char *kw[5];
struct cli_kw *cli_kw;
const char *errmsg;
char *end;
MAY_LJMP(check_args(L, 3, "register_cli"));
@ -11055,12 +11056,21 @@ __LJMP static int hlua_register_cli(lua_State *L)
errmsg = "Lua out of memory error.";
goto error;
}
strncat((char *)fcn->name, "<lua.cli", len);
end = fcn->name;
len = 8;
memcpy(end, "<lua.cli", len);
end += len;
for (i = 0; i < index; i++) {
strncat((char *)fcn->name, ".", len);
strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
*(end++) = '.';
len = strlen(cli_kws->kw[0].str_kw[i]);
memcpy(end, cli_kws->kw[0].str_kw[i], len);
end += len;
}
strncat((char *)fcn->name, ">", len);
*(end++) = '>';
*(end++) = 0;
fcn->function_ref[hlua_state_id] = ref_io;
/* Fill last entries. */