cursor: check return value of snprintf()

Fixes a new warning in GCC 7:

    FAILED: cursor/libwayland-cursor.so.0.22.90.p/xcursor.c.o
    cc -Icursor/libwayland-cursor.so.0.22.90.p -Icursor -I../cursor -I. -I.. -Isrc -I../src -fdiagnostics-color=always -pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -Werror -std=c99 -O3 -D_POSIX_C_SOURCE=200809L -Wno-unused-parameter -Wstrict-prototypes -Wmissing-prototypes -fvisibility=hidden -fPIC '-DICONDIR="/usr/share/X11/icons"' -MD -MQ cursor/libwayland-cursor.so.0.22.90.p/xcursor.c.o -MF cursor/libwayland-cursor.so.0.22.90.p/xcursor.c.o.d -o cursor/libwayland-cursor.so.0.22.90.p/xcursor.c.o -c ../cursor/xcursor.c
    ../cursor/xcursor.c: In function 'xcursor_load_theme':
    ../cursor/xcursor.c:596:39: error: '%s' directive output between 7 and 7 bytes may cause result to exceed 'INT_MAX' [-Werror=format-truncation=]
      596 |         snprintf(full, full_size, "%s/%s/%s", dir, subdir, file);
          |                                       ^~
    ......
      764 |                 full = xcursor_build_fullname(dir, "cursors", "");
          |                                                    ~~~~~~~~~
    ../cursor/xcursor.c:596:41: error: '/' directive output between 1 and 1 bytes may cause result to exceed 'INT_MAX' [-Werror=format-truncation=]
      596 |         snprintf(full, full_size, "%s/%s/%s", dir, subdir, file);
          |                                         ^
    cc1: all warnings being treated as errors

Signed-off-by: Simon Ser <contact@emersion.fr>
This commit is contained in:
Simon Ser 2023-08-02 16:46:11 +02:00
parent 72da004b3e
commit 7b27881cd1

View File

@ -585,6 +585,7 @@ xcursor_build_fullname(const char *dir, const char *subdir, const char *file)
{ {
char *full; char *full;
size_t full_size; size_t full_size;
int ret;
if (!dir || !subdir || !file) if (!dir || !subdir || !file)
return NULL; return NULL;
@ -593,7 +594,11 @@ xcursor_build_fullname(const char *dir, const char *subdir, const char *file)
full = malloc(full_size); full = malloc(full_size);
if (!full) if (!full)
return NULL; return NULL;
snprintf(full, full_size, "%s/%s/%s", dir, subdir, file); ret = snprintf(full, full_size, "%s/%s/%s", dir, subdir, file);
if (ret < 0) {
free(full);
return NULL;
}
return full; return full;
} }