Merge pull request #20494 from bluca/snprintf_voidify

tree-wide: voidify unchecked snprintf calls or use snprintf_ok
This commit is contained in:
Yu Watanabe 2021-08-21 06:45:58 +09:00 committed by GitHub
commit cc97ef5627
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 74 additions and 78 deletions

View file

@ -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)

View file

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

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

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

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