]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
PRVM: fix a use of strlcpy on an unterminated source
authorbones_was_here <bones_was_here@xonotic.au>
Sun, 21 Jan 2024 18:03:51 +0000 (04:03 +1000)
committerbones_was_here <bones_was_here@xonotic.au>
Sun, 21 Jan 2024 18:37:17 +0000 (04:37 +1000)
This code is used when starting a QC program in a non-English language.
See also 3727057b879ccfeaa434537f41a302f047e8cfae

Also renames the dp_ustr2stp() parameter ssize to slen for clarity.

Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
common.c
common.h
prvm_edict.c

index 837c635374ce18719bd6f5728cf80d3645bdd0a3..06afb60fbda94023beffced85838aa80f6291c7d 100644 (file)
--- a/common.c
+++ b/common.c
@@ -1369,16 +1369,16 @@ char *dp_stpecpy(char *dst, char *end, const char *src)
  * Returns a pointer to the \0 terminator. Guarantees \0 termination.
  * Compared to ustr2stp(): truncates and warns on overflow.
  */
-char *dp_ustr2stp(char *dst, size_t dsize, const char *src, size_t ssize)
+char *dp_ustr2stp(char *dst, size_t dsize, const char *src, size_t slen)
 {
-       if (ssize >= dsize)
+       if (slen >= dsize)
        {
-               ssize = dsize - 1;
-               Con_Printf(CON_WARN "%s: src string truncated to %zu bytes: \"%.*s\"\n", __func__, ssize, (int)ssize, src);
+               slen = dsize - 1;
+               Con_Printf(CON_WARN "%s: src string truncated to %zu bytes: \"%.*s\"\n", __func__, slen, (int)slen, src);
        }
-       memcpy(dst, src, ssize);
-       dst[ssize] = '\0';
-       return &dst[ssize];
+       memcpy(dst, src, slen);
+       dst[slen] = '\0';
+       return &dst[slen];
 }
 
 /** Copies a string, like strlcpy() but with a better return: the number of bytes copied
index cb889439323d91039b06d0e5630ef12c06bb8c88..200e432b0ac8fdc425ea0690af00423f2a45bef9 100644 (file)
--- a/common.h
+++ b/common.h
@@ -297,7 +297,7 @@ size_t COM_StringDecolorize(const char *in, size_t size_in, char *out, size_t si
 size_t dp__strlcpy(char *dst, const char *src, size_t dsize, const char *func, unsigned line);
 size_t dp__strlcat(char *dst, const char *src, size_t dsize, const char *func, unsigned line);
 char *dp_stpecpy(char *dst, char *end, const char *src);
-char *dp_ustr2stp(char *dst, size_t dsize, const char *src, size_t ssize);
+char *dp_ustr2stp(char *dst, size_t dsize, const char *src, size_t slen);
 
 
 void FindFraction(double val, int *num, int *denom, int denomMax);
index aa05028fc13c7d43dea99c3bce30f80ebe2c9c91..c05aaa28559b6786be56f8614c15fe970ddcb488 100644 (file)
@@ -1854,7 +1854,8 @@ static po_t *PRVM_PO_Load(const char *filename, const char *filename2, mempool_t
                                        break;
                                if((size_t)(q - p) >= (size_t) sizeof(inbuf))
                                        break;
-                               dp_strlcpy(inbuf, p, q - p); // not - 1, because this adds a NUL
+                               memcpy(inbuf, p, q - p - 1);
+                               inbuf[q - p - 1] = '\0';
                                PRVM_PO_ParseString(decodedbuf + decodedpos, inbuf, sizeof(decodedbuf) - decodedpos);
                                decodedpos += strlen(decodedbuf + decodedpos);
                                if(*q == '\r')