various: use asprintf_safe in more places

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-03-06 17:46:59 +01:00
parent 1dc6445bbb
commit dc0b438836
6 changed files with 75 additions and 90 deletions
+6 -8
View File
@@ -559,20 +559,18 @@ int in_addr_port_ifindex_name_to_string(int family, const union in_addr_union *u
if (port > 0) {
if (family == AF_INET6) {
if (ifindex > 0)
r = asprintf(&x, "[%s]:%"PRIu16"%%%i%s%s", ip_str, port, ifindex, separator, server_name);
x = asprintf_safe("[%s]:%"PRIu16"%%%i%s%s", ip_str, port, ifindex, separator, server_name);
else
r = asprintf(&x, "[%s]:%"PRIu16"%s%s", ip_str, port, separator, server_name);
x = asprintf_safe("[%s]:%"PRIu16"%s%s", ip_str, port, separator, server_name);
} else
r = asprintf(&x, "%s:%"PRIu16"%s%s", ip_str, port, separator, server_name);
x = asprintf_safe("%s:%"PRIu16"%s%s", ip_str, port, separator, server_name);
} else {
if (ifindex > 0)
r = asprintf(&x, "%s%%%i%s%s", ip_str, ifindex, separator, server_name);
else {
x = asprintf_safe("%s%%%i%s%s", ip_str, ifindex, separator, server_name);
else
x = strjoin(ip_str, separator, server_name);
r = x ? 0 : -ENOMEM;
}
}
if (r < 0)
if (!x)
return -ENOMEM;
*ret = TAKE_PTR(x);
+8 -9
View File
@@ -289,26 +289,25 @@ int rlimit_parse(int resource, const char *val, struct rlimit *ret) {
}
int rlimit_format(const struct rlimit *rl, char **ret) {
_cleanup_free_ char *s = NULL;
int r;
char *s;
assert(rl);
assert(ret);
if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY)
r = free_and_strdup(&s, "infinity");
s = strdup("infinity");
else if (rl->rlim_cur >= RLIM_INFINITY)
r = asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max);
s = asprintf_safe("infinity:" RLIM_FMT, rl->rlim_max);
else if (rl->rlim_max >= RLIM_INFINITY)
r = asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur);
s = asprintf_safe(RLIM_FMT ":infinity", rl->rlim_cur);
else if (rl->rlim_cur == rl->rlim_max)
r = asprintf(&s, RLIM_FMT, rl->rlim_cur);
s = asprintf_safe(RLIM_FMT, rl->rlim_cur);
else
r = asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
if (r < 0)
s = asprintf_safe(RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
if (!s)
return -ENOMEM;
*ret = TAKE_PTR(s);
*ret = s;
return 0;
}
+5 -4
View File
@@ -3,6 +3,7 @@
#include <stdio.h>
#include "parse-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
@@ -43,11 +44,11 @@ int string_table_lookup_to_string_fallback(const char * const *table, size_t len
if (i < 0 || i > (ssize_t) max)
return -ERANGE;
if (i < (ssize_t) len && table[i]) {
if (i < (ssize_t) len && table[i])
s = strdup(table[i]);
if (!s)
return -ENOMEM;
} else if (asprintf(&s, "%zd", i) < 0)
else
s = asprintf_safe("%zd", i);
if (!s)
return -ENOMEM;
*ret = s;
+8 -9
View File
@@ -15,6 +15,7 @@
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "sync-util.h"
@@ -299,7 +300,7 @@ static int make_good(const char *prefix, const char *suffix, char **ret) {
}
static int make_bad(const char *prefix, uint64_t done, const char *suffix, char **ret) {
_cleanup_free_ char *bad = NULL;
char *bad;
assert(prefix);
assert(suffix);
@@ -308,16 +309,14 @@ static int make_bad(const char *prefix, uint64_t done, const char *suffix, char
/* Generate the path we'd use on bad boots. Let's simply set the 'left' counter to zero, and keep the 'done'
* counter. The information might be interesting to boot loaders, after all. */
if (done == 0) {
if (done == 0)
bad = strjoin(prefix, "+0", suffix);
if (!bad)
return -ENOMEM;
} else {
if (asprintf(&bad, "%s+0-%" PRIu64 "%s", prefix, done, suffix) < 0)
return -ENOMEM;
}
else
bad = asprintf_safe("%s+0-%" PRIu64 "%s", prefix, done, suffix);
if (!bad)
return -ENOMEM;
*ret = TAKE_PTR(bad);
*ret = bad;
return 0;
}
+5 -7
View File
@@ -1930,14 +1930,12 @@ char** exec_context_get_syscall_filter(const ExecContext *c) {
if (num >= 0) {
e = seccomp_errno_or_action_to_string(num);
if (e) {
if (e)
s = strjoin(name, ":", e);
if (!s)
return NULL;
} else {
if (asprintf(&s, "%s:%d", name, num) < 0)
return NULL;
}
else
s = asprintf_safe("%s:%d", name, num);
if (!s)
return NULL;
} else
s = TAKE_PTR(name);
+43 -53
View File
@@ -882,16 +882,14 @@ static int instance_from_socket(
a = be32toh(local.in.sin_addr.s_addr),
b = be32toh(remote.in.sin_addr.s_addr);
if (asprintf(&s,
"%u-%" PRIu64 "-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
nr,
cookie,
a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
be16toh(local.in.sin_port),
b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF,
be16toh(remote.in.sin_port)) < 0)
return -ENOMEM;
s = asprintf_safe(
"%u-%" PRIu64 "-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
nr,
cookie,
a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
be16toh(local.in.sin_port),
b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF,
be16toh(remote.in.sin_port));
break;
}
@@ -906,27 +904,23 @@ static int instance_from_socket(
*a = local.in6.sin6_addr.s6_addr+12,
*b = remote.in6.sin6_addr.s6_addr+12;
if (asprintf(&s,
"%u-%" PRIu64 "-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
nr,
cookie,
a[0], a[1], a[2], a[3],
be16toh(local.in6.sin6_port),
b[0], b[1], b[2], b[3],
be16toh(remote.in6.sin6_port)) < 0)
return -ENOMEM;
} else {
if (asprintf(&s,
"%u-%" PRIu64 "-%s:%u-%s:%u",
nr,
cookie,
IN6_ADDR_TO_STRING(&local.in6.sin6_addr),
be16toh(local.in6.sin6_port),
IN6_ADDR_TO_STRING(&remote.in6.sin6_addr),
be16toh(remote.in6.sin6_port)) < 0)
return -ENOMEM;
}
s = asprintf_safe(
"%u-%" PRIu64 "-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
nr,
cookie,
a[0], a[1], a[2], a[3],
be16toh(local.in6.sin6_port),
b[0], b[1], b[2], b[3],
be16toh(remote.in6.sin6_port));
} else
s = asprintf_safe(
"%u-%" PRIu64 "-%s:%u-%s:%u",
nr,
cookie,
IN6_ADDR_TO_STRING(&local.in6.sin6_addr),
be16toh(local.in6.sin6_port),
IN6_ADDR_TO_STRING(&remote.in6.sin6_addr),
be16toh(remote.in6.sin6_port));
break;
}
@@ -939,42 +933,38 @@ static int instance_from_socket(
uint64_t pidfd_id;
if (pidfd >= 0 && pidfd_get_inode_id(pidfd, &pidfd_id) >= 0)
r = asprintf(&s, "%u-%" PRIu64 "-" PID_FMT "_%" PRIu64 "-" UID_FMT,
nr, cookie, ucred.pid, pidfd_id, ucred.uid);
s = asprintf_safe(
"%u-%" PRIu64 "-" PID_FMT "_%" PRIu64 "-" UID_FMT,
nr, cookie, ucred.pid, pidfd_id, ucred.uid);
else
r = asprintf(&s, "%u-%" PRIu64 "-" PID_FMT "-" UID_FMT,
nr, cookie, ucred.pid, ucred.uid);
if (r < 0)
return -ENOMEM;
} else if (r == -ENODATA) {
s = asprintf_safe(
"%u-%" PRIu64 "-" PID_FMT "-" UID_FMT,
nr, cookie, ucred.pid, ucred.uid);
} else if (r == -ENODATA)
/* This handles the case where somebody is connecting from another pid/uid namespace
* (e.g. from outside of our container). */
if (asprintf(&s,
"%u-%" PRIu64 "-unknown",
nr,
cookie) < 0)
return -ENOMEM;
} else
s = asprintf_safe("%u-%" PRIu64 "-unknown", nr, cookie);
else
return r;
break;
}
case AF_VSOCK:
if (asprintf(&s,
"%u-%" PRIu64 "-%u:%u-%u:%u",
nr,
cookie,
local.vm.svm_cid, local.vm.svm_port,
remote.vm.svm_cid, remote.vm.svm_port) < 0)
return -ENOMEM;
s = asprintf_safe(
"%u-%" PRIu64 "-%u:%u-%u:%u",
nr,
cookie,
local.vm.svm_cid, local.vm.svm_port,
remote.vm.svm_cid, remote.vm.svm_port);
break;
default:
assert_not_reached();
}
if (!s)
return -ENOMEM;
*ret = s;
return 0;
}