various: use strdup_to() in various obvious cases

strdup_to() returns 0 on success and here we convert obvious blocks
which either return -ENOMEM or 0.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2024-03-20 15:13:44 +01:00
parent 7e850da3d6
commit 454318d3e8
17 changed files with 31 additions and 124 deletions
+1 -6
View File
@@ -741,12 +741,7 @@ int chase_extract_filename(const char *path, const char *root, char **ret) {
return r;
}
char *fname = strdup(".");
if (!fname)
return -ENOMEM;
*ret = fname;
return 0;
return strdup_to(ret, ".");
}
int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, int open_flags, char **ret_path) {
+4 -7
View File
@@ -58,21 +58,18 @@ int device_path_make_major_minor(mode_t mode, dev_t devnum, char **ret) {
}
int device_path_make_inaccessible(mode_t mode, char **ret) {
char *s;
const char *s;
assert(ret);
if (S_ISCHR(mode))
s = strdup("/run/systemd/inaccessible/chr");
s = "/run/systemd/inaccessible/chr";
else if (S_ISBLK(mode))
s = strdup("/run/systemd/inaccessible/blk");
s = "/run/systemd/inaccessible/blk";
else
return -ENODEV;
if (!s)
return -ENOMEM;
*ret = s;
return 0;
return strdup_to(ret, s);
}
int device_path_make_canonical(mode_t mode, dev_t devnum, char **ret) {
+2 -7
View File
@@ -25,7 +25,7 @@ int format_ifname_full(int ifindex, FormatIfnameFlag flag, char buf[static IF_NA
}
int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret) {
char buf[IF_NAMESIZE], *copy;
char buf[IF_NAMESIZE];
int r;
assert(ret);
@@ -34,12 +34,7 @@ int format_ifname_full_alloc(int ifindex, FormatIfnameFlag flag, char **ret) {
if (r < 0)
return r;
copy = strdup(buf);
if (!copy)
return -ENOMEM;
*ret = copy;
return 0;
return strdup_to(ret, buf);
}
char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) {
+2 -7
View File
@@ -1094,7 +1094,6 @@ int path_extract_filename(const char *path, char **ret) {
}
int path_extract_directory(const char *path, char **ret) {
_cleanup_free_ char *a = NULL;
const char *c, *next = NULL;
int r;
@@ -1118,14 +1117,10 @@ int path_extract_directory(const char *path, char **ret) {
if (*path != '/') /* filename only */
return -EDESTADDRREQ;
a = strdup("/");
if (!a)
return -ENOMEM;
*ret = TAKE_PTR(a);
return 0;
return strdup_to(ret, "/");
}
a = strndup(path, next - path);
_cleanup_free_ char *a = strndup(path, next - path);
if (!a)
return -ENOMEM;
+5 -18
View File
@@ -697,18 +697,10 @@ int vtnr_from_tty(const char *tty) {
tty = active;
}
if (tty == active)
*ret = TAKE_PTR(active);
else {
char *tmp;
tmp = strdup(tty);
if (!tmp)
return -ENOMEM;
*ret = tmp;
}
if (tty != active)
return strdup_to(ret, tty);
*ret = TAKE_PTR(active);
return 0;
}
@@ -988,7 +980,7 @@ bool on_tty(void) {
}
int getttyname_malloc(int fd, char **ret) {
char path[PATH_MAX], *c; /* PATH_MAX is counted *with* the trailing NUL byte */
char path[PATH_MAX]; /* PATH_MAX is counted *with* the trailing NUL byte */
int r;
assert(fd >= 0);
@@ -1001,12 +993,7 @@ int getttyname_malloc(int fd, char **ret) {
if (r > 0)
return -r;
c = strdup(skip_dev_prefix(path));
if (!c)
return -ENOMEM;
*ret = c;
return 0;
return strdup_to(ret, skip_dev_prefix(path));
}
int getttyname_harder(int fd, char **ret) {
+1 -6
View File
@@ -793,12 +793,7 @@ int unit_name_mangle_with_suffix(
return 1;
good:
s = strdup(name);
if (!s)
return -ENOMEM;
*ret = TAKE_PTR(s);
return 0;
return strdup_to(ret, name);
}
int slice_build_parent_slice(const char *slice, char **ret) {