1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-11 05:17:44 +03:00

tree-wide: voidify unchecked snprintf calls

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
This commit is contained in:
Luca Boccassi 2021-08-20 14:57:02 +01:00
parent 272fff7d84
commit 121ed16caf
9 changed files with 66 additions and 66 deletions

View 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;

View 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;

View 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);

View 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;
}

View 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;
}

View 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;
}

View 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;
}
};

View 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;
}

View 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;
}