diff --git a/src/shared/creds-util.c b/src/shared/creds-util.c index 08d915cb8da..434907c9986 100644 --- a/src/shared/creds-util.c +++ b/src/shared/creds-util.c @@ -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) { diff --git a/src/test/test-creds.c b/src/test/test-creds.c index 778bb6ab8fa..e56a2f38e38 100644 --- a/src/test/test-creds.c +++ b/src/test/test-creds.c @@ -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"));