]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: lua: remove incorrect usage of strncat()
authorWilly Tarreau <w@1wt.eu>
Fri, 7 Apr 2023 13:27:55 +0000 (15:27 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 7 Apr 2023 14:04:54 +0000 (16:04 +0200)
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.

src/hlua.c

index 561b5d1cd9dce3b0b20ea4139825e88a95585a63..84b606e8be4feded9bb5d30bbb034dcdfe23212a 100644 (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. */