[Outreachy PATCH v2 2/2] gpg-interface: use string_list_split() instead of strbuf_split_max()
- From
Olamide Caleb Bello <belkid98@gmail.com>
- Date
- Oct 16, 2025, 01:03 UTC
- Message-ID
- <024a44a242394f5a067f17ec97a99c61232898fe.1760571220.git.belkid98@gmail.com>
- In-Reply-To
- <cover.1760571220.git.belkid98@gmail.com>
In get_default_ssh_signing_key(), the default ssh signing key is retrieved in `key_stdout`, which is then split using strbuf_split_max() into two tokens
The string in `key_stdout` is then split using strbuf_split_max() into two tokens at a new line and the first token is returned as a `char *` and not a strbuf. This makes the function lack the use of strbuf API as no edits are performed on the split tokens.
Replace strbuf_split_max() with string_list_split() for simplicity.
Note that strbuf_split_max() uses `2` to indicate the number of tokens to extract from the string, while string_list_split() uses `1` to specify the number of times the split will be done on the string, so 1 gives 2 tokens as it is in the original instance.
Signed-off-by: Olamide Caleb Bello <belkid98@gmail.com> --- Changes in v2 - Reword the commit message for clarity - Replace the comment removed in v1 - Change the test to ensure the number of tokens added to the list by string_list_split() equals 2 - Add () to function names
gpg-interface.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/gpg-interface.c b/gpg-interface.c index 989dca7d14..ab7b8b46e5 100644 --- a/gpg-interface.c +++ b/gpg-interface.c @@ -862,7 +862,7 @@ static char *get_default_ssh_signing_key(void) struct child_process ssh_default_key = CHILD_PROCESS_INIT; int ret = -1; struct strbuf key_stdout = STRBUF_INIT, key_stderr = STRBUF_INIT; - struct strbuf **keys; + struct string_list keys = STRING_LIST_INIT_DUP; char *key_command = NULL; const char **argv; int n; @@ -884,19 +884,19 @@ static char *get_default_ssh_signing_key(void) &key_stderr, 0); if (!ret) { - keys = strbuf_split_max(&key_stdout, '\n', 2); - if (keys[0] && is_literal_ssh_key(keys[0]->buf, &literal_key)) { + if (string_list_split(&keys, key_stdout.buf, "\n", 1) == 2 && + is_literal_ssh_key(keys.items[0].string, &literal_key)) { /* * We only use `is_literal_ssh_key` here to check validity * The prefix will be stripped when the key is used. */ - default_key = strbuf_detach(keys[0], NULL); + default_key = xstrdup(keys.items[0].string); } else { warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"), key_stderr.buf, key_stdout.buf); } - strbuf_list_free(keys); + string_list_clear(&keys, 0); } else { warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"), key_stderr.buf, key_stdout.buf);
-- 2.51.0.463.g79cf913ea9