cursor/os-compatibility: remove strcpy/strcat usage
authorSimon Ser <contact@emersion.fr>
Sat, 4 Jun 2022 20:56:36 +0000 (22:56 +0200)
committerSimon Ser <contact@emersion.fr>
Sat, 4 Jun 2022 20:58:47 +0000 (22:58 +0200)
These functions don't perform bounds checking, so they are easy to
misuse and complicate audits.

Signed-off-by: Simon Ser <contact@emersion.fr>
cursor/os-compatibility.c

index 8d51e52..9c84d68 100644 (file)
@@ -32,6 +32,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
+#include <stdio.h>
 #include <stdlib.h>
 
 #ifdef HAVE_MEMFD_CREATE
@@ -118,6 +119,7 @@ os_create_anonymous_file(off_t size)
        static const char template[] = "/wayland-cursor-shared-XXXXXX";
        const char *path;
        char *name;
+       size_t name_size;
        int fd;
 
 #ifdef HAVE_MEMFD_CREATE
@@ -139,12 +141,12 @@ os_create_anonymous_file(off_t size)
                        return -1;
                }
 
-               name = malloc(strlen(path) + sizeof(template));
+               name_size = strlen(path) + sizeof(template);
+               name = malloc(name_size);
                if (!name)
                        return -1;
 
-               strcpy(name, path);
-               strcat(name, template);
+               snprintf(name, name_size, "%s%s", path, template);
 
                fd = create_tmpfile_cloexec(name);