Skip to content

Fix undefined behavior: incorrect usage of strlcpy #35

Description

@VVD

Discussed in discord.

If the src and dst strings overlap, the behavior is undefined.

--- src/fs.c.orig       2025-02-16 14:10:58 UTC
+++ src/fs.c
@@ -119,6 +119,7 @@ void FS_StripPathAndExtension(char *filepath)
        size_t lastslash = (size_t) -1;
        size_t lastdot   = (size_t) -1;
        size_t i                 = 0;
+       char temp_filepath[MAX_QPATH];

        for ( ; filepath[i]; i++)
        {
@@ -131,7 +132,8 @@ void FS_StripPathAndExtension(char *filepath)
        if (lastdot == (size_t) -1 || lastdot < lastslash)
                lastdot = i;

-       strlcpy(filepath, filepath + lastslash + 1, lastdot - lastslash);
+       strlcpy(temp_filepath, filepath + lastslash + 1, lastdot - lastslash);
+       strlcpy(filepath, temp_filepath, sizeof(temp_filepath));
 }

 // return file extension with dot, or empty string if dot not found at all
--- src/net_utils.c.orig        2025-02-16 14:10:58 UTC
+++ src/net_utils.c
@@ -60,11 +60,13 @@ char *Net_BaseAdrToString (struct sockaddr_in *a, char
        // Windows have inet_ntop only starting from Vista. Sigh.
 #ifdef _WIN32
        const char *result = inet_ntoa(a->sin_addr);
+       strlcpy(buf, result ? result : "", bufsize);
 #else
        const char *result = inet_ntop(a->sin_family, &a->sin_addr, buf, bufsize);
+       if (!result) {
+               strlcpy(buf, "", bufsize);
+       }
 #endif
-
-       strlcpy(buf, result ? result : "", bufsize);

        return buf;
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions