Merge pull request #31862 from keszybz/add-strdup_to-helper

Add strdup_to() helper
This commit is contained in:
Yu Watanabe
2024-03-21 14:47:05 +09:00
committed by GitHub
53 changed files with 304 additions and 701 deletions
+1 -9
View File
@@ -268,16 +268,8 @@ int probe_filesystem_full(
(void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
if (fstype) {
char *t;
log_debug("Probed fstype '%s' on partition %s.", fstype, path);
t = strdup(fstype);
if (!t)
return -ENOMEM;
*ret_fstype = t;
return 1;
return strdup_to_full(ret_fstype, fstype);
}
not_found:
+1 -7
View File
@@ -182,7 +182,6 @@ int ethtool_get_driver(int *ethtool_fd, const char *ifname, char **ret) {
struct ifreq ifr = {
.ifr_data = (void*) &ecmd,
};
char *d;
int r;
assert(ethtool_fd);
@@ -201,12 +200,7 @@ int ethtool_get_driver(int *ethtool_fd, const char *ifname, char **ret) {
if (isempty(ecmd.driver))
return -ENODATA;
d = strdup(ecmd.driver);
if (!d)
return -ENOMEM;
*ret = d;
return 0;
return strdup_to(ret, ecmd.driver);
}
int ethtool_get_link_info(
+4 -13
View File
@@ -37,16 +37,11 @@ static int specifier_prefix_and_instance(char specifier, const void *data, const
static int specifier_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
const InstallInfo *i = ASSERT_PTR(userdata);
char *ans;
if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) && i->default_instance)
return unit_name_replace_instance(i->name, i->default_instance, ret);
ans = strdup(i->name);
if (!ans)
return -ENOMEM;
*ret = ans;
return 0;
return strdup_to(ret, i->name);
}
static int specifier_prefix(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
@@ -86,14 +81,10 @@ static int specifier_last_component(char specifier, const void *data, const char
return r;
dash = strrchr(prefix, '-');
if (dash) {
dash = strdup(dash + 1);
if (!dash)
return -ENOMEM;
*ret = dash;
} else
*ret = TAKE_PTR(prefix);
if (dash)
return strdup_to(ret, dash + 1);
*ret = TAKE_PTR(prefix);
return 0;
}
+6 -14
View File
@@ -3042,17 +3042,16 @@ int unit_file_set_default(
int unit_file_get_default(
RuntimeScope scope,
const char *root_dir,
char **name) {
char **ret) {
_cleanup_(lookup_paths_done) LookupPaths lp = {};
_cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
InstallInfo *info;
char *n;
int r;
assert(scope >= 0);
assert(scope < _RUNTIME_SCOPE_MAX);
assert(name);
assert(ret);
r = lookup_paths_init(&lp, scope, 0, root_dir);
if (r < 0)
@@ -3063,12 +3062,7 @@ int unit_file_get_default(
if (r < 0)
return r;
n = strdup(info->name);
if (!n)
return -ENOMEM;
*name = n;
return 0;
return strdup_to(ret, info->name);
}
int unit_file_lookup_state(
@@ -3221,11 +3215,9 @@ int unit_file_exists_full(RuntimeScope scope, const LookupPaths *lp, const char
if (ret_path) {
assert(info);
_cleanup_free_ char *p = strdup(info->path);
if (!p)
return -ENOMEM;
*ret_path = TAKE_PTR(p);
r = strdup_to(ret_path, info->path);
if (r < 0)
return r;
}
return 1;
+1 -1
View File
@@ -175,7 +175,7 @@ int unit_file_set_default(
int unit_file_get_default(
RuntimeScope scope,
const char *root_dir,
char **name);
char **ret);
int unit_file_add_dependency(
RuntimeScope scope,
UnitFileFlags flags,
+2 -7
View File
@@ -140,7 +140,7 @@ static char* systemd_crypt_ra(const char *phrase, const char *setting, void **da
int hash_password_full(const char *password, void **cd_data, int *cd_size, char **ret) {
_cleanup_free_ char *salt = NULL;
_cleanup_(erase_and_freep) void *_cd_data = NULL;
char *p;
const char *p;
int r, _cd_size = 0;
assert(!!cd_data == !!cd_size);
@@ -155,12 +155,7 @@ int hash_password_full(const char *password, void **cd_data, int *cd_size, char
return log_debug_errno(errno_or_else(SYNTHETIC_ERRNO(EINVAL)),
CRYPT_RA_NAME "() failed: %m");
p = strdup(p);
if (!p)
return -ENOMEM;
*ret = p;
return 0;
return strdup_to(ret, p);
}
bool looks_like_hashed_password(const char *s) {
+2 -8
View File
@@ -32,14 +32,8 @@ int net_get_type_string(sd_device *device, uint16_t iftype, char **ret) {
if (device &&
sd_device_get_devtype(device, &t) >= 0 &&
!isempty(t)) {
p = strdup(t);
if (!p)
return -ENOMEM;
*ret = p;
return 0;
}
!isempty(t))
return strdup_to(ret, t);
t = arphrd_to_name(iftype);
if (!t)
+5 -9
View File
@@ -101,7 +101,6 @@ int suggest_passwords(void) {
_cleanup_strv_free_erase_ char **suggestions = NULL;
_cleanup_(erase_and_freep) char *joined = NULL;
char buf[PWQ_MAX_ERROR_MESSAGE_LEN];
size_t i;
int r;
r = pwq_allocate_context(&pwq);
@@ -115,7 +114,7 @@ int suggest_passwords(void) {
if (!suggestions)
return log_oom();
for (i = 0; i < N_SUGGESTIONS; i++) {
for (size_t i = 0; i < N_SUGGESTIONS; i++) {
r = sym_pwquality_generate(pwq, 64, suggestions + i);
if (r < 0)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to generate password, ignoring: %s",
@@ -145,13 +144,10 @@ int check_password_quality(const char *password, const char *old, const char *us
r = sym_pwquality_check(pwq, password, old, username, &auxerror);
if (r < 0) {
if (ret_error) {
_cleanup_free_ char *e = NULL;
e = strdup(sym_pwquality_strerror(buf, sizeof(buf), r, auxerror));
if (!e)
return -ENOMEM;
*ret_error = TAKE_PTR(e);
r = strdup_to(ret_error,
sym_pwquality_strerror(buf, sizeof(buf), r, auxerror));
if (r < 0)
return r;
}
return 0; /* all bad */
+2 -10
View File
@@ -141,16 +141,8 @@ int terminal_urlify_path(const char *path, const char *text, char **ret) {
if (isempty(text))
text = path;
if (!urlify_enabled()) {
char *n;
n = strdup(text);
if (!n)
return -ENOMEM;
*ret = n;
return 0;
}
if (!urlify_enabled())
return strdup_to(ret, text);
r = file_url_from_path(path, &url);
if (r < 0)
+6 -41
View File
@@ -111,18 +111,7 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[
/* Generic handler for simple string replacements */
int specifier_string(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
char *n = NULL;
assert(ret);
if (!isempty(data)) {
n = strdup(data);
if (!n)
return -ENOMEM;
}
*ret = n;
return 0;
return strdup_to(ASSERT_PTR(ret), empty_to_null(data));
}
int specifier_real_path(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
@@ -249,32 +238,18 @@ int specifier_pretty_hostname(char specifier, const void *data, const char *root
int specifier_kernel_release(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
struct utsname uts;
char *n;
assert(ret);
if (uname(&uts) < 0)
return -errno;
n = strdup(uts.release);
if (!n)
return -ENOMEM;
*ret = n;
return 0;
return strdup_to(ret, uts.release);
}
int specifier_architecture(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
char *t;
assert(ret);
t = strdup(architecture_to_string(uname_architecture()));
if (!t)
return -ENOMEM;
*ret = t;
return 0;
return strdup_to(ASSERT_PTR(ret),
architecture_to_string(uname_architecture()));
}
/* Note: fields in /etc/os-release might quite possibly be missing, even if everything is entirely valid
@@ -420,7 +395,6 @@ int specifier_user_shell(char specifier, const void *data, const char *root, con
int specifier_tmp_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
const char *p;
char *copy;
int r;
assert(ret);
@@ -432,17 +406,12 @@ int specifier_tmp_dir(char specifier, const void *data, const char *root, const
if (r < 0)
return r;
}
copy = strdup(p);
if (!copy)
return -ENOMEM;
*ret = copy;
return 0;
return strdup_to(ret, p);
}
int specifier_var_tmp_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
const char *p;
char *copy;
int r;
assert(ret);
@@ -454,12 +423,8 @@ int specifier_var_tmp_dir(char specifier, const void *data, const char *root, co
if (r < 0)
return r;
}
copy = strdup(p);
if (!copy)
return -ENOMEM;
*ret = copy;
return 0;
return strdup_to(ret, p);
}
int specifier_escape_strv(char **l, char ***ret) {