summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-02-23 11:12:19 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-02-23 11:15:42 +0100
commit73fc96c8ac0aa95477e53838a22ce94918c9d06f (patch)
treeed6eb222b0ec6a5d13d5e45047e1e2af1857f728 /src/basic
parentcc5bbdb274d6941f74f9227781e6715951cf3dfb (diff)
downloadsystemd-73fc96c8ac0aa95477e53838a22ce94918c9d06f.tar.gz
udev/net-id: check all snprintf return values
gcc-8 throws an error if it knows snprintf might truncate output and the return value is ignored: ../src/udev/udev-builtin-net_id.c: In function 'dev_pci_slot': ../src/udev/udev-builtin-net_id.c:297:47: error: '%s' directive output may be truncated writing up to 255 bytes into a region of size between 0 and 4095 [-Werror=format-truncation=] snprintf(str, sizeof str, "%s/%s/address", slots, dent->d_name); ^~ ../src/udev/udev-builtin-net_id.c:297:17: note: 'snprintf' output between 10 and 4360 bytes into a destination of size 4096 snprintf(str, sizeof str, "%s/%s/address", slots, dent->d_name); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1: some warnings being treated as errors Let's check all return values. This actually makes the code better, because there's no point in trying to open a file when the name has been truncated, etc.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/stdio-util.h6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/basic/stdio-util.h b/src/basic/stdio-util.h
index dbfafba269..d3fed365d8 100644
--- a/src/basic/stdio-util.h
+++ b/src/basic/stdio-util.h
@@ -27,9 +27,11 @@
#include "macro.h"
-#define xsprintf(buf, fmt, ...) \
- assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), "xsprintf: " #buf "[] must be big enough")
+#define snprintf_ok(buf, len, fmt, ...) \
+ ((size_t) snprintf(buf, len, fmt, __VA_ARGS__) < (len))
+#define xsprintf(buf, fmt, ...) \
+ assert_message_se(snprintf_ok(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__), "xsprintf: " #buf "[] must be big enough")
#define VA_FORMAT_ADVANCE(format, ap) \
do { \