]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: voidify unchecked snprintf calls 20494/head
authorLuca Boccassi <luca.boccassi@microsoft.com>
Fri, 20 Aug 2021 13:57:02 +0000 (14:57 +0100)
committerLuca Boccassi <luca.boccassi@microsoft.com>
Fri, 20 Aug 2021 17:29:40 +0000 (18:29 +0100)
According to Coverity, 194 ouf of 227 times we check for snprintf return code.
Voidify the rest.

CID#1461512
CID#1461513
CID#1461514
CID#1461515
CID#1461516
CID#1461518
CID#1461519
CID#1461520
CID#1461522

src/basic/format-util.c
src/basic/time-util.c
src/cgtop/cgtop.c
src/core/cgroup.c
src/core/selinux-access.c
src/resolve/resolved-dns-rr.c
src/test/nss-test-util.c
src/test/test-nss-hosts.c
src/udev/udev-builtin-hwdb.c

index 8174eaa81b88ee403776bfa751e3bd74590604d3..e2c7b134d01855c3156096c62aa424f6fbb6c02c 100644 (file)
@@ -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;
index c3b175a192bffd942e6610e4fecb871a1d3af8e9..f4022f7c86984a1f59fb3a074e8451b4a334c362 100644 (file)
@@ -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;
index 0104b6a543b80baeb61afc3282a617f867443b29..e5ab904c4fc0d4ad185705e047a59ce7f1b9aa81 100644 (file)
@@ -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);
index 4b6fd525bb2844e9f2d75cae3cacbc60d6f28b9a..52bdf54b5fb690159326b1b94134dda8c7474227 100644 (file)
@@ -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;
 }
index d077d5dea751c6e936c5f99ce8c5648549825038..984e324d74aa7103d0f9853f11ce157e92930506 100644 (file)
@@ -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;
 }
index d98914e8b817ea94e31182100f468d86b6b87a6d..720b4c58d762eae88e79288aa832482d46aecee9 100644 (file)
@@ -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;
 }
index fc1d724a2f26ee685679db46b0fc9267a5b873c8..20643f8aa5a2cabaafb94169d3e9261aa5fba057 100644 (file)
@@ -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;
         }
 };
index eddb04795133bc5682fbc653fa20a2e2c274941a..f9c0bd6ff9e8e21dfb054bfaeacf124dc22606ea 100644 (file)
@@ -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;
 }
 
index 78835185b07345d12c4509a30f4a25b2438c1490..87535dbd005469c15acd2ee9f2efab9373a6f381 100644 (file)
@@ -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;
 }