diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-11-22 11:53:45 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2023-11-22 15:17:18 +0100 |
commit | d66c4d101e9d0da1c3b4a13fade2ec9e5f4b0ed8 (patch) | |
tree | d8a24bf1847b4de3c7e0f610925902a00c4d0c5f /src | |
parent | machine-credential: fix error logging (diff) | |
download | systemd-d66c4d101e9d0da1c3b4a13fade2ec9e5f4b0ed8.tar.xz systemd-d66c4d101e9d0da1c3b4a13fade2ec9e5f4b0ed8.zip |
machine-credential: fix GREEDY_REALLOC error handling
We must check the return value of GREEDY_REALLOC for OOM, and the
pointer are updated already on success, hence it's a bad idea to make a
copy of the pointer beforehand.
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/machine-credential.c | 33 |
1 files changed, 12 insertions, 21 deletions
diff --git a/src/shared/machine-credential.c b/src/shared/machine-credential.c index 9c3a352e94..0e6db381b6 100644 --- a/src/shared/machine-credential.c +++ b/src/shared/machine-credential.c @@ -30,13 +30,12 @@ void machine_credential_free_all(MachineCredential *creds, size_t n) { int machine_credential_set(MachineCredential **credentials, size_t *n_credentials, const char *cred_string) { _cleanup_free_ char *word = NULL, *data = NULL; - MachineCredential *creds = *ASSERT_PTR(credentials); ssize_t l; - size_t n_creds = *ASSERT_PTR(n_credentials); int r; const char *p = ASSERT_PTR(cred_string); - assert(creds || n_creds == 0); + assert(credentials && n_credentials); + assert(*credentials || *n_credentials == 0); r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS); if (r < 0) @@ -47,7 +46,7 @@ int machine_credential_set(MachineCredential **credentials, size_t *n_credential if (!credential_name_valid(word)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "MachineCredential name is not valid: %s", word); - FOREACH_ARRAY(cred, creds, n_creds) + FOREACH_ARRAY(cred, *credentials, *n_credentials) if (streq(cred->id, word)) return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Duplicate credential '%s', refusing.", word); @@ -55,19 +54,15 @@ int machine_credential_set(MachineCredential **credentials, size_t *n_credential if (l < 0) return log_error_errno(l, "Failed to unescape credential data: %s", p); - GREEDY_REALLOC(creds, n_creds + 1); - if (!creds) + if (!GREEDY_REALLOC(*credentials, *n_credentials + 1)) return log_oom(); - creds[n_creds++] = (MachineCredential) { + (*credentials)[(*n_credentials)++] = (MachineCredential) { .id = TAKE_PTR(word), .data = TAKE_PTR(data), .size = l, }; - *credentials = creds; - *n_credentials = n_creds; - return 0; } @@ -75,12 +70,12 @@ int machine_credential_load(MachineCredential **credentials, size_t *n_credentia ReadFullFileFlags flags = READ_FULL_FILE_SECURE; _cleanup_(erase_and_freep) char *data = NULL; _cleanup_free_ char *word = NULL, *j = NULL; - MachineCredential *creds = *ASSERT_PTR(credentials); - size_t size, n_creds = *ASSERT_PTR(n_credentials); - int r; const char *p = ASSERT_PTR(cred_path); + size_t size; + int r; - assert(creds || n_creds == 0); + assert(credentials && n_credentials); + assert(*credentials || *n_credentials == 0); r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS); if (r < 0) @@ -91,7 +86,7 @@ int machine_credential_load(MachineCredential **credentials, size_t *n_credentia if (!credential_name_valid(word)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "MachineCredential name is not valid: %s", word); - FOREACH_ARRAY(cred, creds, n_creds) + FOREACH_ARRAY(cred, *credentials, *n_credentials) if (streq(cred->id, word)) return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Duplicate credential '%s', refusing.", word); @@ -116,18 +111,14 @@ int machine_credential_load(MachineCredential **credentials, size_t *n_credentia if (r < 0) return log_error_errno(r, "Failed to read credential '%s': %m", j ?: p); - GREEDY_REALLOC(creds, n_creds + 1); - if (!creds) + if (!GREEDY_REALLOC(*credentials, *n_credentials + 1)) return log_oom(); - creds[n_creds++] = (MachineCredential) { + (*credentials)[(*n_credentials)++] = (MachineCredential) { .id = TAKE_PTR(word), .data = TAKE_PTR(data), .size = size, }; - *credentials = creds; - *n_credentials = n_creds; - return 0; } |