From 7b27881cd1cbe55ec66c1455867eda9606eab8c7 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 2 Aug 2023 16:46:11 +0200 Subject: [PATCH] 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 --- cursor/xcursor.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cursor/xcursor.c b/cursor/xcursor.c index 43a5292c..6766c564 100644 --- a/cursor/xcursor.c +++ b/cursor/xcursor.c @@ -585,6 +585,7 @@ xcursor_build_fullname(const char *dir, const char *subdir, const char *file) { char *full; size_t full_size; + int ret; if (!dir || !subdir || !file) return NULL; @@ -593,7 +594,11 @@ xcursor_build_fullname(const char *dir, const char *subdir, const char *file) full = malloc(full_size); if (!full) 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; }