Skip to content

eBPF-based security platform

util: format the last values with strsprintf()

Daniel Borkmann9711585295d5
diff --git a/util/config_image.c b/util/config_image.c
index 64603bf8..303575ce 100644
--- a/util/config_image.c
+++ b/util/config_image.c
@@ -2,6 +2,7 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include <endian.h>
#include <errno.h>
@@ -153,10 +154,9 @@ int shirudo_config_image_check_files(int data_fd, int hash_fd,
static int open_part(const char *prefix, const char *suffix)
{
char path[PATH_MAX];
- int n, fd;
+ int fd;
- n = snprintf(path, sizeof(path), "%s%s", prefix, suffix);
- if (n < 0 || (size_t)n >= sizeof(path))
+ if (strsprintf(path, sizeof(path), "%s%s", prefix, suffix) < 0)
return -ENAMETOOLONG;
fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK);
return fd < 0 ? -errno : fd;
diff --git a/util/dm_verity.c b/util/dm_verity.c
index 1d649326..958f7700 100644
--- a/util/dm_verity.c
+++ b/util/dm_verity.c
@@ -174,7 +174,8 @@ static int loop_attach(int file_fd, __u64 bytes, int *out)
close(control);
if (rc)
return rc;
- snprintf(path, sizeof(path), "/dev/loop%d", n);
+ if (strsprintf(path, sizeof(path), "/dev/loop%d", n) < 0)
+ return -ENAMETOOLONG;
loop = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
if (loop < 0)
return -errno;
@@ -194,12 +195,10 @@ static int loop_attach(int file_fd, __u64 bytes, int *out)
static int device_path(struct shirudo_dm_verity *device, __u64 dev)
{
struct stat st;
- int n;
device->device = (dev_t)dev;
- n = snprintf(device->path, sizeof(device->path), "/dev/dm-%u",
- minor(device->device));
- if (n < 0 || (size_t)n >= sizeof(device->path))
+ if (strsprintf(device->path, sizeof(device->path), "/dev/dm-%u",
+ minor(device->device)) < 0)
return -ENAMETOOLONG;
if (lstat(device->path, &st)) {
if (errno != ENOENT)
@@ -406,7 +405,8 @@ static key_serial_t signature_key(int fd, char description[64])
done += n;
}
hex_encode(random, sizeof(random), suffix);
- snprintf(description, 64, "shirudo-verity-%s", suffix);
+ if (strsprintf(description, 64, "shirudo-verity-%s", suffix) < 0)
+ return -ENAMETOOLONG;
key = syscall(__NR_add_key, "user", description, signature,
(size_t)st.st_size, KEY_SPEC_THREAD_KEYRING);
if (key < 0)
@@ -465,12 +465,12 @@ int shirudo_dm_verity_create(const struct shirudo_config_image_open *image,
}
hex_encode(image->image.root_hash, 32, root);
hex_encode(image->image.salt, 32, salt);
- n = snprintf(params, sizeof(params),
- "1 %u:%u %u:%u 4096 4096 %llu 1 sha256 %s %s",
- major(data_st.st_rdev), minor(data_st.st_rdev),
- major(hash_st.st_rdev), minor(hash_st.st_rdev),
- (__u64)image->image.data_blocks, root, salt);
- if (n < 0 || (size_t)n >= sizeof(params)) {
+ n = strsprintf(params, sizeof(params),
+ "1 %u:%u %u:%u 4096 4096 %llu 1 sha256 %s %s",
+ major(data_st.st_rdev), minor(data_st.st_rdev),
+ major(hash_st.st_rdev), minor(hash_st.st_rdev),
+ (__u64)image->image.data_blocks, root, salt);
+ if (n < 0) {
rc = -EOVERFLOW;
goto out;
}
@@ -483,9 +483,9 @@ int shirudo_dm_verity_create(const struct shirudo_config_image_open *image,
rc = key;
goto out;
}
- written = snprintf(params + n, remaining,
- " 2 root_hash_sig_key_desc %s", signature);
- if (written < 0 || written >= remaining) {
+ written = strsprintf(params + n, remaining,
+ " 2 root_hash_sig_key_desc %s", signature);
+ if (written < 0) {
rc = -EOVERFLOW;
goto out;
}
@@ -559,7 +559,8 @@ static int loop_open(dev_t device)
if (major(device) != 7)
return -ENODEV;
- snprintf(path, sizeof(path), "/dev/loop%u", minor(device));
+ if (strsprintf(path, sizeof(path), "/dev/loop%u", minor(device)) < 0)
+ return -ENAMETOOLONG;
fd = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);
if (fd < 0)
return -errno;
diff --git a/util/format.c b/util/format.c
index f0285965..d3e42cbc 100644
--- a/util/format.c
+++ b/util/format.c
@@ -5,16 +5,19 @@
* Human-readable formatters.
*/
+#include <errno.h>
#include <stdio.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "format.h"
-void human_size(__u64 bytes, char *out, size_t out_cap)
+int human_size(__u64 bytes, char *out, size_t out_cap)
{
double v = (double)bytes;
const char *suf = "B";
+ int n;
if (bytes >= (1ULL << 30)) {
v /= (double)(1ULL << 30);
@@ -26,14 +29,14 @@ void human_size(__u64 bytes, char *out, size_t out_cap)
v /= (double)(1ULL << 10);
suf = "K";
} else {
- snprintf(out, out_cap, "%llu%s",
- (__u64)bytes, suf);
- return;
+ return strsprintf(out, out_cap, "%llu%s", (__u64)bytes,
+ suf) < 0 ? -E2BIG : 0;
}
if (v >= 100.0)
- snprintf(out, out_cap, "%.0f%s", v, suf);
+ n = strsprintf(out, out_cap, "%.0f%s", v, suf);
else if (v >= 10.0)
- snprintf(out, out_cap, "%.1f%s", v, suf);
+ n = strsprintf(out, out_cap, "%.1f%s", v, suf);
else
- snprintf(out, out_cap, "%.2f%s", v, suf);
+ n = strsprintf(out, out_cap, "%.2f%s", v, suf);
+ return n < 0 ? -E2BIG : 0;
}
diff --git a/util/format.h b/util/format.h
index 2a968484..0ef5d29e 100644
--- a/util/format.h
+++ b/util/format.h
@@ -17,6 +17,6 @@
* picked so the rendered value stays within four significant digits
* regardless of magnitude (16K / 1.8M / 5.57G / 1.23G / 124G).
*/
-void human_size(__u64 bytes, char *out, size_t out_cap);
+int human_size(__u64 bytes, char *out, size_t out_cap);
#endif /* SHIRUDO_UTIL_FORMAT_H */
diff --git a/util/image_cache.c b/util/image_cache.c
index 12565d3e..3a29c120 100644
--- a/util/image_cache.c
+++ b/util/image_cache.c
@@ -2,6 +2,7 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include <errno.h>
#include <fcntl.h>
@@ -146,8 +147,8 @@ static int verify_incoming(const struct shirudo_config_image_open *opened,
int rc, cleanup;
char name[128];
- snprintf(name, sizeof(name), "shirudo-verify-%s",
- strrchr(staging, '/') + 1);
+ if (strsprintf(name, sizeof(name), "shirudo-verify-%s", strrchr(staging, '/') + 1) < 0)
+ return -ENAMETOOLONG;
rc = shirudo_dm_verity_create(opened, name, &device);
if (rc)
return rc;
@@ -184,13 +185,13 @@ int shirudo_image_cache_fetch(const char *source,
rc = seal_label(staging);
if (rc)
goto out;
- snprintf(incoming, sizeof(incoming), "%s/image", staging);
+ if (strsprintf(incoming, sizeof(incoming), "%s/image", staging) < 0) {
+ rc = -ENAMETOOLONG;
+ goto out;
+ }
for (i = 0; i < ARRAY_SIZE(files); i++) {
- if (snprintf(from, sizeof(from), "%s%s", source,
- files[i].suffix) >= (int)sizeof(from) ||
- snprintf(to, sizeof(to), "%s%s", incoming,
- files[i].suffix) >=
- (int)sizeof(to)) {
+ if (strsprintf(from, sizeof(from), "%s%s", source, files[i].suffix) < 0 ||
+ strsprintf(to, sizeof(to), "%s%s", incoming, files[i].suffix) < 0) {
rc = -ENAMETOOLONG;
goto out;
}
@@ -205,16 +206,19 @@ int shirudo_image_cache_fetch(const char *source,
if (!rc) {
memcpy(target->root_hash, opened.image.root_hash, 32);
hex_encode(target->root_hash, 32, hex);
- snprintf(target->prefix, sizeof(target->prefix), "%s/%s",
- SHIRUDO_IMAGE_CACHE_DIR, hex);
+ if (strsprintf(target->prefix, sizeof(target->prefix), "%s/%s",
+ SHIRUDO_IMAGE_CACHE_DIR, hex) < 0)
+ rc = -ENAMETOOLONG;
}
shirudo_config_image_close(&opened);
if (rc)
goto out;
for (i = 0; i < ARRAY_SIZE(files); i++) {
- snprintf(from, sizeof(from), "%s%s", incoming, files[i].suffix);
- snprintf(to, sizeof(to), "%s%s", target->prefix,
- files[i].suffix);
+ if (strsprintf(from, sizeof(from), "%s%s", incoming, files[i].suffix) < 0 ||
+ strsprintf(to, sizeof(to), "%s%s", target->prefix, files[i].suffix) < 0) {
+ rc = -ENAMETOOLONG;
+ goto out;
+ }
if (link(from, to) && errno != EEXIST) {
rc = -errno;
goto out;
@@ -236,9 +240,8 @@ out:
if (dir >= 0)
close(dir);
for (i = 0; i < ARRAY_SIZE(files); i++) {
- snprintf(to, sizeof(to), "%s/image%s", staging,
- files[i].suffix);
- unlink(to);
+ if (strsprintf(to, sizeof(to), "%s/image%s", staging, files[i].suffix) >= 0)
+ unlink(to);
}
rmdir(staging);
return rc;
diff --git a/util/image_config.c b/util/image_config.c
index 5b6c374c..1b64d900 100644
--- a/util/image_config.c
+++ b/util/image_config.c
@@ -60,12 +60,10 @@ const char *shirudo_image_path(enum shirudo_image_path which)
int shirudo_image_path_relative(char *out, __u64 cap, const char *path)
{
- int n;
-
if (config_fd < 0 || !path || path[0] == '/' || strstr(path, ".."))
return -EINVAL;
- n = snprintf(out, cap, "/proc/self/fd/%d/%s", config_fd, path);
- return n < 0 || (__u64)n >= cap ? -ENAMETOOLONG : 0;
+ return strsprintf(out, cap, "/proc/self/fd/%d/%s", config_fd, path) < 0 ?
+ -ENAMETOOLONG : 0;
}
static int validate_tree(int root_fd, dev_t device, __u32 depth,
@@ -206,9 +204,11 @@ int shirudo_image_config_validate(int root_fd)
rc = validate_tree(root_fd, st.st_dev, 0, &entries);
if (!rc)
rc = validate_settings(root_fd);
+ if (!rc && strsprintf(path, sizeof(path),
+ "/proc/self/fd/%d/etc/shirudo/shirudo.conf",
+ root_fd) < 0)
+ rc = -ENAMETOOLONG;
if (!rc) {
- snprintf(path, sizeof(path),
- "/proc/self/fd/%d/etc/shirudo/shirudo.conf", root_fd);
rc = config_load_path(&candidate, path);
if (!rc && !candidate.map_export_valid)
rc = -EINVAL;
@@ -233,8 +233,11 @@ int shirudo_image_config_set(const struct shirudo_config *bootstrap,
fd = fcntl(root_fd, F_DUPFD_CLOEXEC, 3);
if (fd < 0)
return -errno;
- snprintf(path, sizeof(path),
- "/proc/self/fd/%d/etc/shirudo/shirudo.conf", fd);
+ if (strsprintf(path, sizeof(path),
+ "/proc/self/fd/%d/etc/shirudo/shirudo.conf", fd) < 0) {
+ close(fd);
+ return -ENAMETOOLONG;
+ }
rc = config_load_path(&candidate, path);
if (rc || !candidate.map_export_valid) {
close(fd);
@@ -251,9 +254,13 @@ int shirudo_image_config_set(const struct shirudo_config *bootstrap,
config_fd = fd;
effective = candidate;
active = true;
- for (i = 0; i < SHIRUDO_IMAGE_PATH_COUNT; i++)
- snprintf(paths[i], sizeof(paths[i]), "/proc/self/fd/%d/%s",
- config_fd, relative[i]);
+ for (i = 0; i < SHIRUDO_IMAGE_PATH_COUNT; i++) {
+ if (strsprintf(paths[i], sizeof(paths[i]), "/proc/self/fd/%d/%s",
+ config_fd, relative[i]) < 0) {
+ shirudo_image_config_clear();
+ return -ENAMETOOLONG;
+ }
+ }
return 0;
}
diff --git a/util/image_guards.c b/util/image_guards.c
index 70b9d849..f8be3604 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: BSD-2-Clause
/* Copyright (c) 2026 Authors of shirudo */
-#include <shirudo/misc.h>
-
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
@@ -16,6 +14,9 @@
#include <bpf/btf.h>
#include <bpf/libbpf.h>
+#include <shirudo/misc.h>
+#include <shirudo/string.h>
+
#include "identity.h"
#include "image_guards.h"
#include "log.h"
@@ -363,8 +364,7 @@ static int guard_link(const struct shirudo_image_guard *guard,
return -EPROTO;
suffix = !strncmp(guard->prog, "shirudo_lsm_", 12) ?
guard->prog + 12 : guard->prog + 13;
- if (snprintf(function, sizeof(function), "bpf_lsm_%s", suffix) >=
- (int)sizeof(function))
+ if (strsprintf(function, sizeof(function), "bpf_lsm_%s", suffix) < 0)
return -ENAMETOOLONG;
type_id = btf__find_by_name_kind(*kernel, function, BTF_KIND_FUNC);
if (type_id <= 0 || (__u32)type_id != program.attach_btf_id)
diff --git a/util/image_keys.c b/util/image_keys.c
index 7244670c..c2ae1c4a 100644
--- a/util/image_keys.c
+++ b/util/image_keys.c
@@ -2,6 +2,7 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include <dirent.h>
#include <errno.h>
@@ -81,7 +82,8 @@ static int revocations(int root_fd, bool apply, bool boot)
st.st_size <= 0 ||
st.st_size > SHIRUDO_KEYS_DER_MAX))
rc = -EINVAL;
- snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
+ if (!rc && strsprintf(path, sizeof(path), "/proc/self/fd/%d", fd) < 0)
+ rc = -ENAMETOOLONG;
if (!rc)
rc = keys_read_der(path, &der, &size);
close(fd);
@@ -114,8 +116,9 @@ int shirudo_keys_validate_image_set(const char *primary, int root_fd,
rc = revocations(root_fd, false, false);
if (rc)
return rc;
- snprintf(keys, sizeof(keys), "/proc/self/fd/%d/keys", root_fd);
- snprintf(revoked, sizeof(revoked), "%s/revoked", keys);
+ if (strsprintf(keys, sizeof(keys), "/proc/self/fd/%d/keys", root_fd) < 0 ||
+ strsprintf(revoked, sizeof(revoked), "%s/revoked", keys) < 0)
+ return -ENAMETOOLONG;
rc = shirudo_keys_load_image(primary, keys, revoked, true, set);
if (!rc && (!set->n || set->n_unvouched))
rc = -EKEYREJECTED;
diff --git a/util/image_runtime.c b/util/image_runtime.c
index 35632f0a..e580e07f 100644
--- a/util/image_runtime.c
+++ b/util/image_runtime.c
@@ -162,12 +162,12 @@ static int mkdir_owned(const char *path)
return created ? seal_label(path) : 0;
}
-static void mapping_name(const __u8 root[32], char out[128])
+static int mapping_name(const __u8 root[32], char out[128])
{
char hex[65];
hex_encode(root, 32, hex);
- snprintf(out, 128, "shirudo-config-%s", hex);
+ return strsprintf(out, 128, "shirudo-config-%s", hex) < 0 ? -ENAMETOOLONG : 0;
}
static int device_observed(dev_t device, const __u8 root[32], __u32 kind)
@@ -179,7 +179,8 @@ static int device_observed(dev_t device, const __u8 root[32], __u32 kind)
char path[64];
int fd, block, rc = -EACCES;
- snprintf(path, sizeof(path), "/dev/dm-%u", minor(device));
+ if (strsprintf(path, sizeof(path), "/dev/dm-%u", minor(device)) < 0)
+ return -ENAMETOOLONG;
block = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
if (block < 0)
return -errno;
@@ -273,8 +274,10 @@ static int load_runtime_config(bool trusted_boot)
fd = open_runtime_file(SHIRUDO_RUNTIME_CONF_PATH, root.st_dev);
if (fd < 0)
return fd;
- snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
- rc = shirudo_image_bootstrap_read(path, &image.bootstrap, false);
+ if (strsprintf(path, sizeof(path), "/proc/self/fd/%d", fd) < 0)
+ rc = -ENAMETOOLONG;
+ else
+ rc = shirudo_image_bootstrap_read(path, &image.bootstrap, false);
if (!rc)
rc = config_load_path(&image.bootstrap_config, path);
close(fd);
@@ -303,9 +306,10 @@ static int load_runtime_config(bool trusted_boot)
return rc;
/* Keep the verified config certificate open through key enrollment. */
- snprintf(image.bootstrap.certificate,
- sizeof(image.bootstrap.certificate), "/proc/self/fd/%d",
- image.certificate_fd);
+ if (strsprintf(image.bootstrap.certificate,
+ sizeof(image.bootstrap.certificate), "/proc/self/fd/%d",
+ image.certificate_fd) < 0)
+ return -ENAMETOOLONG;
return 0;
}
@@ -525,7 +529,9 @@ static int prepare_config(struct image_context *current,
return rc;
if (memcmp(target->root_hash, current->candidate.image.root_hash, 32))
return -EKEYREJECTED;
- mapping_name(target->root_hash, name);
+ rc = mapping_name(target->root_hash, name);
+ if (rc)
+ return rc;
rc = shirudo_dm_verity_inspect(name, &current->candidate.image,
device);
if (rc == -ENXIO || rc == -ENODEV || rc == -ENOENT) {
@@ -812,7 +818,8 @@ static int config_mount_check(const struct shirudo_config_update_record *record)
return rc;
if (memcmp(record->target.root_hash, opened.image.root_hash, 32))
rc = -EKEYREJECTED;
- mapping_name(record->target.root_hash, name);
+ if (!rc)
+ rc = mapping_name(record->target.root_hash, name);
if (!rc)
rc = shirudo_dm_verity_inspect(name, &opened.image, &device);
shirudo_config_image_close(&opened);
diff --git a/util/json.c b/util/json.c
index 4268a94a..c2c9847f 100644
--- a/util/json.c
+++ b/util/json.c
@@ -63,7 +63,10 @@ static void put_string(struct shirudo_json_emit *e, const char *s)
} else if (c < 0x20) {
char u[7];
- snprintf(u, sizeof(u), "\\u%04x", c);
+ if (strsprintf(u, sizeof(u), "\\u%04x", c) < 0) {
+ e->err = -E2BIG;
+ break;
+ }
put_raw(e, u);
p++;
} else if (c < 0x80) {
@@ -175,10 +178,13 @@ void json_kv_int(struct shirudo_json_emit *e,
{
char num[24];
+ if (strsprintf(num, sizeof(num), "%lld", (__s64)val) < 0) {
+ e->err = -E2BIG;
+ return;
+ }
sep(e);
put_string(e, key);
put(e, ':');
- snprintf(num, sizeof(num), "%lld", (__s64)val);
put_raw(e, num);
}
diff --git a/util/keys.c b/util/keys.c
index a570c02b..ea387f38 100644
--- a/util/keys.c
+++ b/util/keys.c
@@ -446,8 +446,7 @@ int keys_load(const char *keys_dir, const char *revoked_dir,
return shirudo_keys_load_image(SHIRUDO_RUNTIME_CERT_PATH, keys,
SHIRUDO_KEYS_REVOKED_DIR, with_trusted, set);
}
- if (snprintf(primary, sizeof(primary), "%s/%s", keys_dir,
- SHIRUDO_CERT_FILE) >= (int)sizeof(primary))
+ if (strsprintf(primary, sizeof(primary), "%s/%s", keys_dir, SHIRUDO_CERT_FILE) < 0)
return -ENAMETOOLONG;
return shirudo_keys_load_split(primary, keys_dir, revoked_dir,
with_trusted, set);
diff --git a/util/kmodules.c b/util/kmodules.c
index 4bcdb067..b9adecfd 100644
--- a/util/kmodules.c
+++ b/util/kmodules.c
@@ -302,8 +302,8 @@ int kmodules_read(struct shirudo_kmod_set *set)
char path[64 + sizeof("/sys/module/")];
struct stat st;
- snprintf(path, sizeof(path), "/sys/module/%s", m->name);
- if (stat(path, &st) == 0) {
+ if (strsprintf(path, sizeof(path), "/sys/module/%s",
+ m->name) >= 0 && stat(path, &st) == 0) {
m->loaded_at_sec = (__s64)st.st_mtim.tv_sec;
m->loaded_at_nsec = (__s32)st.st_mtim.tv_nsec;
}
@@ -324,8 +324,9 @@ static void print_subtree(const struct shirudo_kmod_set *set, size_t i,
const struct shirudo_kmod *m = &set->mods[i];
const char *branch = is_last ? "└── " : "├── ";
int already = (seen[i >> 3] >> (i & 7)) & 1;
- char size_s[16];
+ const char *child = prefix;
char child_prefix[256];
+ char size_s[16];
human_size(m->size, size_s, sizeof(size_s));
@@ -338,14 +339,15 @@ static void print_subtree(const struct shirudo_kmod_set *set, size_t i,
printf("%s%s%s (%s, refs=%u)\n", prefix, branch, m->name,
size_s, (__u32)m->refcount);
- snprintf(child_prefix, sizeof(child_prefix), "%s%s", prefix,
- is_last ? " " : "│ ");
+ if (strsprintf(child_prefix, sizeof(child_prefix), "%s%s", prefix,
+ is_last ? " " : "│ ") >= 0)
+ child = child_prefix;
for (__u16 k = 0; k < m->n_deps; k++) {
size_t dep = set->deps[m->deps_off + k];
int last_child = (k == m->n_deps - 1);
- print_subtree(set, dep, child_prefix, last_child, seen);
+ print_subtree(set, dep, child, last_child, seen);
}
}
diff --git a/util/preflight.c b/util/preflight.c
index 74c19496..e04b69d6 100644
--- a/util/preflight.c
+++ b/util/preflight.c
@@ -390,11 +390,10 @@ static void check_tpm(struct shirudo_preflight_result *r)
if (override) {
pass(c);
- snprintf(c->detail, sizeof(c->detail),
- "TCTI override: %.*s",
- (int)(sizeof(c->detail) -
- sizeof("TCTI override: ")),
- cfg.tpm_tcti);
+ if (strsprintf(c->detail, sizeof(c->detail),
+ "TCTI override: %s", cfg.tpm_tcti) < 0)
+ strscpy(c->detail, "TCTI override",
+ sizeof(c->detail));
}
explicit_bzero(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */
if (override)
diff --git a/util/timefmt.c b/util/timefmt.c
index a204bf4f..2c017f22 100644
--- a/util/timefmt.c
+++ b/util/timefmt.c
@@ -12,18 +12,21 @@
#include "timefmt.h"
-void format_duration(long seconds, char *buf, size_t cap)
+int format_duration(long seconds, char *buf, size_t cap)
{
+ int n;
+
if (seconds < 0)
- strscpy(buf, "?", cap);
- else if (seconds < 60)
- snprintf(buf, cap, "%lds", seconds);
+ return strscpy(buf, "?", cap) < 0 ? -E2BIG : 0;
+ if (seconds < 60)
+ n = strsprintf(buf, cap, "%lds", seconds);
else if (seconds < 3600)
- snprintf(buf, cap, "%ldm", seconds / 60);
+ n = strsprintf(buf, cap, "%ldm", seconds / 60);
else if (seconds < 86400)
- snprintf(buf, cap, "%ldh", seconds / 3600);
+ n = strsprintf(buf, cap, "%ldh", seconds / 3600);
else
- snprintf(buf, cap, "%ldd", seconds / 86400);
+ n = strsprintf(buf, cap, "%ldd", seconds / 86400);
+ return n < 0 ? -E2BIG : 0;
}
int iso8601(time_t t, char *out, size_t cap)
diff --git a/util/timefmt.h b/util/timefmt.h
index 9f86cc23..c3c76576 100644
--- a/util/timefmt.h
+++ b/util/timefmt.h
@@ -12,7 +12,7 @@
#include <time.h>
/* A negative seconds writes "?"; cap must hold the worst-case %ldd. */
-void format_duration(long seconds, char *buf, size_t cap);
+int format_duration(long seconds, char *buf, size_t cap);
/* UTC to the second; "" and -EINVAL when t cannot be rendered. */
int iso8601(time_t t, char *out, size_t cap);