diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-08-21 06:45:58 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-21 06:45:58 +0900 |
commit | cc97ef56272b3af82ca991a861b86aaa65c2bc8b (patch) | |
tree | 112aa4c784b68a76c1400e307f41f23b068dcc16 | |
parent | 0e0e57a484f97e6bfb6f4bf92b59cbe33ebab008 (diff) | |
parent | 121ed16cafadc209602ca45dc6215bf5a7d85632 (diff) | |
download | systemd-cc97ef56272b3af82ca991a861b86aaa65c2bc8b.tar.gz |
Merge pull request #20494 from bluca/snprintf_voidify
tree-wide: voidify unchecked snprintf calls or use snprintf_ok
-rw-r--r-- | man/hwdb-usb-device.c | 4 | ||||
-rw-r--r-- | src/basic/format-util.c | 28 | ||||
-rw-r--r-- | src/basic/time-util.c | 76 | ||||
-rw-r--r-- | src/cgtop/cgtop.c | 4 | ||||
-rw-r--r-- | src/core/cgroup.c | 4 | ||||
-rw-r--r-- | src/core/selinux-access.c | 10 | ||||
-rw-r--r-- | src/libsystemd/sd-bus/bus-match.c | 12 | ||||
-rw-r--r-- | src/resolve/resolved-dns-rr.c | 8 | ||||
-rw-r--r-- | src/test/nss-test-util.c | 2 | ||||
-rw-r--r-- | src/test/test-nss-hosts.c | 2 | ||||
-rw-r--r-- | src/udev/udev-builtin-hwdb.c | 2 |
11 files changed, 74 insertions, 78 deletions
diff --git a/man/hwdb-usb-device.c b/man/hwdb-usb-device.c index 8a4b86e7bd..a85c3bcf10 100644 --- a/man/hwdb-usb-device.c +++ b/man/hwdb-usb-device.c @@ -3,13 +3,13 @@ #include <sd-hwdb.h> int print_usb_properties(uint16_t vid, uint16_t pid) { - char match[15]; + char match[STRLEN("usb:vp") + DECIMAL_STR_MAX(uint16_t) * 2]; sd_hwdb *hwdb; const char *key, *value; int r; /* Match this USB vendor and product ID combination */ - snprintf(match, sizeof match, "usb:v%04Xp%04X", vid, pid); + xsprintf(match, "usb:v%04Xp%04X", vid, pid); r = sd_hwdb_new(&hwdb); if (r < 0) diff --git a/src/basic/format-util.c b/src/basic/format-util.c index 9920604f3a..e2c7b134d0 100644 --- a/src/basic/format-util.c +++ b/src/basic/format-util.c @@ -15,9 +15,9 @@ char *format_ifname_full(int ifindex, char buf[static IF_NAMESIZE + 1], FormatIf return NULL; if (FLAGS_SET(flag, FORMAT_IFNAME_IFINDEX_WITH_PERCENT)) - snprintf(buf, IF_NAMESIZE + 1, "%%%d", ifindex); + assert(snprintf_ok(buf, IF_NAMESIZE + 1, "%%%d", ifindex)); else - snprintf(buf, IF_NAMESIZE + 1, "%d", ifindex); + assert(snprintf_ok(buf, IF_NAMESIZE + 1, "%d", ifindex)); return buf; } @@ -56,23 +56,23 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) { for (size_t i = 0; i < n; i++) if (t >= table[i].factor) { if (flag & FORMAT_BYTES_BELOW_POINT) { - snprintf(buf, l, - "%" PRIu64 ".%" PRIu64 "%s", - t / table[i].factor, - i != n - 1 ? - (t / table[i + 1].factor * UINT64_C(10) / table[n - 1].factor) % UINT64_C(10): - (t * UINT64_C(10) / table[i].factor) % UINT64_C(10), - table[i].suffix); + (void) snprintf(buf, l, + "%" PRIu64 ".%" PRIu64 "%s", + t / table[i].factor, + i != n - 1 ? + (t / table[i + 1].factor * UINT64_C(10) / table[n - 1].factor) % UINT64_C(10): + (t * UINT64_C(10) / table[i].factor) % UINT64_C(10), + table[i].suffix); } else - snprintf(buf, l, - "%" PRIu64 "%s", - t / table[i].factor, - table[i].suffix); + (void) snprintf(buf, l, + "%" PRIu64 "%s", + t / table[i].factor, + table[i].suffix); goto finish; } - snprintf(buf, l, "%" PRIu64 "%s", t, flag & FORMAT_BYTES_TRAILING_B ? "B" : ""); + (void) snprintf(buf, l, "%" PRIu64 "%s", t, flag & FORMAT_BYTES_TRAILING_B ? "B" : ""); finish: buf[l-1] = 0; diff --git a/src/basic/time-util.c b/src/basic/time-util.c index c3b175a192..f4022f7c86 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -433,62 +433,62 @@ char *format_timestamp_relative(char *buf, size_t l, usec_t t) { usec_t years = d / USEC_PER_YEAR; usec_t months = (d % USEC_PER_YEAR) / USEC_PER_MONTH; - snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s %s", - years, - years == 1 ? "year" : "years", - months, - months == 1 ? "month" : "months", - s); + (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s %s", + years, + years == 1 ? "year" : "years", + months, + months == 1 ? "month" : "months", + s); } else if (d >= USEC_PER_MONTH) { usec_t months = d / USEC_PER_MONTH; usec_t days = (d % USEC_PER_MONTH) / USEC_PER_DAY; - snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s %s", - months, - months == 1 ? "month" : "months", - days, - days == 1 ? "day" : "days", - s); + (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s %s", + months, + months == 1 ? "month" : "months", + days, + days == 1 ? "day" : "days", + s); } else if (d >= USEC_PER_WEEK) { usec_t weeks = d / USEC_PER_WEEK; usec_t days = (d % USEC_PER_WEEK) / USEC_PER_DAY; - snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s %s", - weeks, - weeks == 1 ? "week" : "weeks", - days, - days == 1 ? "day" : "days", - s); + (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s %s", + weeks, + weeks == 1 ? "week" : "weeks", + days, + days == 1 ? "day" : "days", + s); } else if (d >= 2*USEC_PER_DAY) - snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s); + (void) snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s); else if (d >= 25*USEC_PER_HOUR) - snprintf(buf, l, "1 day " USEC_FMT "h %s", - (d - USEC_PER_DAY) / USEC_PER_HOUR, s); + (void) snprintf(buf, l, "1 day " USEC_FMT "h %s", + (d - USEC_PER_DAY) / USEC_PER_HOUR, s); else if (d >= 6*USEC_PER_HOUR) - snprintf(buf, l, USEC_FMT "h %s", - d / USEC_PER_HOUR, s); + (void) snprintf(buf, l, USEC_FMT "h %s", + d / USEC_PER_HOUR, s); else if (d >= USEC_PER_HOUR) - snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s", - d / USEC_PER_HOUR, - (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s); + (void) snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s", + d / USEC_PER_HOUR, + (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s); else if (d >= 5*USEC_PER_MINUTE) - snprintf(buf, l, USEC_FMT "min %s", - d / USEC_PER_MINUTE, s); + (void) snprintf(buf, l, USEC_FMT "min %s", + d / USEC_PER_MINUTE, s); else if (d >= USEC_PER_MINUTE) - snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s", - d / USEC_PER_MINUTE, - (d % USEC_PER_MINUTE) / USEC_PER_SEC, s); + (void) snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s", + d / USEC_PER_MINUTE, + (d % USEC_PER_MINUTE) / USEC_PER_SEC, s); else if (d >= USEC_PER_SEC) - snprintf(buf, l, USEC_FMT "s %s", - d / USEC_PER_SEC, s); + (void) snprintf(buf, l, USEC_FMT "s %s", + d / USEC_PER_SEC, s); else if (d >= USEC_PER_MSEC) - snprintf(buf, l, USEC_FMT "ms %s", - d / USEC_PER_MSEC, s); + (void) snprintf(buf, l, USEC_FMT "ms %s", + d / USEC_PER_MSEC, s); else if (d > 0) - snprintf(buf, l, USEC_FMT"us %s", - d, s); + (void) snprintf(buf, l, USEC_FMT"us %s", + d, s); else - snprintf(buf, l, "now"); + (void) snprintf(buf, l, "now"); buf[l-1] = 0; return buf; diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index 0104b6a543..e5ab904c4f 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -95,7 +95,7 @@ static Group *group_free(Group *g) { static const char *maybe_format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { if (arg_raw) { - snprintf(buf, l, USEC_FMT, t); + (void) snprintf(buf, l, USEC_FMT, t); return buf; } return format_timespan(buf, l, t, accuracy); @@ -109,7 +109,7 @@ static const char *maybe_format_bytes(char *buf, size_t l, bool is_valid, uint64 if (!is_valid) return "-"; if (arg_raw) { - snprintf(buf, l, "%" PRIu64, t); + (void) snprintf(buf, l, "%" PRIu64, t); return buf; } return format_bytes(buf, l, t); diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 4b6fd525bb..52bdf54b5f 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -375,11 +375,11 @@ static char *format_cgroup_memory_limit_comparison(char *buf, size_t l, Unit *u, } if (r < 0) { - snprintf(buf, l, " (error getting kernel value: %s)", strerror_safe(r)); + (void) snprintf(buf, l, " (error getting kernel value: %s)", strerror_safe(r)); return buf; } - snprintf(buf, l, " (different value in kernel: %" PRIu64 ")", kval); + (void) snprintf(buf, l, " (different value in kernel: %" PRIu64 ")", kval); return buf; } diff --git a/src/core/selinux-access.c b/src/core/selinux-access.c index d077d5dea7..984e324d74 100644 --- a/src/core/selinux-access.c +++ b/src/core/selinux-access.c @@ -57,11 +57,11 @@ static int audit_callback( if (sd_bus_creds_get_egid(audit->creds, &gid) >= 0) xsprintf(gid_buf, GID_FMT, gid); - snprintf(msgbuf, msgbufsize, - "auid=%s uid=%s gid=%s%s%s%s%s%s%s", - login_uid_buf, uid_buf, gid_buf, - audit->path ? " path=\"" : "", strempty(audit->path), audit->path ? "\"" : "", - audit->cmdline ? " cmdline=\"" : "", strempty(audit->cmdline), audit->cmdline ? "\"" : ""); + (void) snprintf(msgbuf, msgbufsize, + "auid=%s uid=%s gid=%s%s%s%s%s%s%s", + login_uid_buf, uid_buf, gid_buf, + audit->path ? " path=\"" : "", strempty(audit->path), audit->path ? "\"" : "", + audit->cmdline ? " cmdline=\"" : "", strempty(audit->cmdline), audit->cmdline ? "\"" : ""); return 0; } diff --git a/src/libsystemd/sd-bus/bus-match.c b/src/libsystemd/sd-bus/bus-match.c index ff8cf4ee0e..12a50845db 100644 --- a/src/libsystemd/sd-bus/bus-match.c +++ b/src/libsystemd/sd-bus/bus-match.c @@ -997,20 +997,16 @@ const char* bus_match_node_type_to_string(enum bus_match_node_type t, char buf[] return "path_namespace"; case BUS_MATCH_ARG ... BUS_MATCH_ARG_LAST: - snprintf(buf, l, "arg%i", t - BUS_MATCH_ARG); - return buf; + return snprintf_ok(buf, l, "arg%i", t - BUS_MATCH_ARG); case BUS_MATCH_ARG_PATH ... BUS_MATCH_ARG_PATH_LAST: - snprintf(buf, l, "arg%ipath", t - BUS_MATCH_ARG_PATH); - return buf; + return snprintf_ok(buf, l, "arg%ipath", t - BUS_MATCH_ARG_PATH); case BUS_MATCH_ARG_NAMESPACE ... BUS_MATCH_ARG_NAMESPACE_LAST: - snprintf(buf, l, "arg%inamespace", t - BUS_MATCH_ARG_NAMESPACE); - return buf; + return snprintf_ok(buf, l, "arg%inamespace", t - BUS_MATCH_ARG_NAMESPACE); case BUS_MATCH_ARG_HAS ... BUS_MATCH_ARG_HAS_LAST: - snprintf(buf, l, "arg%ihas", t - BUS_MATCH_ARG_HAS); - return buf; + return snprintf_ok(buf, l, "arg%ihas", t - BUS_MATCH_ARG_HAS); default: return NULL; diff --git a/src/resolve/resolved-dns-rr.c b/src/resolve/resolved-dns-rr.c index d98914e8b8..720b4c58d7 100644 --- a/src/resolve/resolved-dns-rr.c +++ b/src/resolve/resolved-dns-rr.c @@ -323,10 +323,10 @@ char* dns_resource_key_to_string(const DnsResourceKey *key, char *buf, size_t bu c = dns_class_to_string(key->class); t = dns_type_to_string(key->type); - snprintf(buf, buf_size, "%s %s%s%.0u %s%s%.0u", - dns_resource_key_name(key), - strempty(c), c ? "" : "CLASS", c ? 0 : key->class, - strempty(t), t ? "" : "TYPE", t ? 0 : key->type); + (void) snprintf(buf, buf_size, "%s %s%s%.0u %s%s%.0u", + dns_resource_key_name(key), + strempty(c), c ? "" : "CLASS", c ? 0 : key->class, + strempty(t), t ? "" : "TYPE", t ? 0 : key->type); return ans; } diff --git a/src/test/nss-test-util.c b/src/test/nss-test-util.c index fc1d724a2f..20643f8aa5 100644 --- a/src/test/nss-test-util.c +++ b/src/test/nss-test-util.c @@ -20,7 +20,7 @@ const char* nss_status_to_string(enum nss_status status, char *buf, size_t buf_l case NSS_STATUS_RETURN: return "NSS_STATUS_RETURN"; default: - snprintf(buf, buf_len, "%i", status); + (void) snprintf(buf, buf_len, "%i", status); return buf; } }; diff --git a/src/test/test-nss-hosts.c b/src/test/test-nss-hosts.c index eddb047951..f9c0bd6ff9 100644 --- a/src/test/test-nss-hosts.c +++ b/src/test/test-nss-hosts.c @@ -36,7 +36,7 @@ static const char* af_to_string(int family, char *buf, size_t buf_len) { if (name) return name; - snprintf(buf, buf_len, "%i", family); + (void) snprintf(buf, buf_len, "%i", family); return buf; } diff --git a/src/udev/udev-builtin-hwdb.c b/src/udev/udev-builtin-hwdb.c index 78835185b0..87535dbd00 100644 --- a/src/udev/udev-builtin-hwdb.c +++ b/src/udev/udev-builtin-hwdb.c @@ -60,7 +60,7 @@ static const char *modalias_usb(sd_device *dev, char *s, size_t size) { return NULL; (void) sd_device_get_sysattr_value(dev, "product", &n); - snprintf(s, size, "usb:v%04Xp%04X:%s", vn, pn, strempty(n)); + (void) snprintf(s, size, "usb:v%04Xp%04X:%s", vn, pn, strempty(n)); return s; } |