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;