1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

sd-json: use sd_json_dispatch_const_string() in sd_json_dispatch_string()

Also, do similar for json_dispatch_user_group_name().
This commit is contained in:
Yu Watanabe 2024-10-07 13:27:37 +09:00 committed by Daan De Meyer
parent f769518c9a
commit 14dde316c7
2 changed files with 8 additions and 22 deletions

View File

@ -73,17 +73,9 @@ int json_dispatch_user_group_name(const char *name, sd_json_variant *variant, sd
const char *n;
int r;
if (sd_json_variant_is_null(variant)) {
*s = mfree(*s);
return 0;
}
if (!sd_json_variant_is_string(variant))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
n = sd_json_variant_string(variant);
if (!valid_user_group_name(n, FLAGS_SET(flags, SD_JSON_RELAX) ? VALID_USER_RELAX : 0))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid user/group name.", strna(name));
r = json_dispatch_const_user_group_name(name, variant, flags, &n);
if (r < 0)
return r;
r = free_and_strdup(s, n);
if (r < 0)

View File

@ -5376,22 +5376,16 @@ _public_ int sd_json_dispatch_double(const char *name, sd_json_variant *variant,
_public_ int sd_json_dispatch_string(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
char **s = ASSERT_PTR(userdata);
const char *n;
int r;
assert_return(variant, -EINVAL);
if (sd_json_variant_is_null(variant)) {
*s = mfree(*s);
return 0;
}
r = sd_json_dispatch_const_string(name, variant, flags, &n);
if (r < 0)
return r;
if (!sd_json_variant_is_string(variant))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name));
if ((flags & SD_JSON_STRICT) && !string_is_safe(sd_json_variant_string(variant)))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' contains unsafe characters, refusing.", strna(name));
r = free_and_strdup(s, sd_json_variant_string(variant));
r = free_and_strdup(s, n);
if (r < 0)
return json_log(variant, flags, r, "Failed to allocate string: %m");