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

creds-util: make read_credential_strings_many behave the same as comment

The comment states "If the specified buffers are already non-NULL
frees them if a credential is found".

Also return 1 if all credentials are found.
This commit is contained in:
Mike Yuan 2024-01-09 15:00:53 +08:00 committed by Lennart Poettering
parent ff85480458
commit 95bcaa4e81
2 changed files with 17 additions and 20 deletions

View File

@ -219,6 +219,7 @@ int read_credential_strings_many_internal(
...) {
_cleanup_free_ void *b = NULL;
bool all = true;
int r, ret = 0;
/* Reads a bunch of credentials into the specified buffers. If the specified buffers are already
@ -234,10 +235,11 @@ int read_credential_strings_many_internal(
r = read_credential(first_name, &b, NULL);
if (r == -ENXIO) /* No creds passed at all? Bail immediately. */
return 0;
if (r < 0) {
if (r != -ENOENT)
ret = r;
} else
if (r == -ENOENT)
all = false;
else if (r < 0)
RET_GATHER(ret, r);
else
free_and_replace(*first_value, b);
va_list ap;
@ -252,20 +254,19 @@ int read_credential_strings_many_internal(
if (!name)
break;
value = va_arg(ap, char **);
if (*value)
continue;
value = ASSERT_PTR(va_arg(ap, char **));
r = read_credential(name, &bb, NULL);
if (r < 0) {
if (ret >= 0 && r != -ENOENT)
ret = r;
} else
if (r == -ENOENT)
all = false;
else if (r < 0)
RET_GATHER(ret, r);
else
free_and_replace(*value, bb);
}
va_end(ap);
return ret;
return ret < 0 ? ret : all;
}
int read_credential_bool(const char *name) {

View File

@ -42,18 +42,12 @@ TEST(read_credential_strings) {
assert_se(read_credential_strings_many("foo", &x, "bar", &y) == 0);
assert_se(x == NULL);
assert_se(streq(y, "piff"));
assert_se(streq(y, "paff"));
p = mfree(p);
assert_se(p = path_join(tmp, "foo"));
assert_se(write_string_file(p, "knurz", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_AVOID_NEWLINE) >= 0);
assert_se(read_credential_strings_many("foo", &x, "bar", &y) >= 0);
assert_se(streq(x, "knurz"));
assert_se(streq(y, "piff"));
y = mfree(y);
assert_se(read_credential_strings_many("foo", &x, "bar", &y) >= 0);
assert_se(streq(x, "knurz"));
assert_se(streq(y, "paff"));
@ -64,7 +58,9 @@ TEST(read_credential_strings) {
assert_se(fwrite("x\0y", 1, 3, f) == 3); /* embedded NUL byte should result in EBADMSG when reading back with read_credential_strings_many() */
f = safe_fclose(f);
assert_se(read_credential_strings_many("bazz", &x, "foo", &y) == -EBADMSG);
y = mfree(y);
assert_se(read_credential_strings_many("bazz", &x, "bar", &y) == -EBADMSG);
assert_se(streq(x, "knurz"));
assert_se(streq(y, "paff"));