Skip to content

Commit c43d4cf

Browse files
committed
Merge branch 'ob/gpg-interface-cleanup'
strbuf_split*() to split a string into multiple strbufs is often a wrong API to use. A few uses of it have been removed by simplifying the code. * ob/gpg-interface-cleanup: gpg-interface: do not use misdesigned strbuf_split*() gpg-interface: do not use misdesigned strbuf_split*()
2 parents 48d0b65 + 2ab72a1 commit c43d4cf

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

gpg-interface.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,7 @@ static char *get_ssh_key_fingerprint(const char *signing_key)
821821
struct child_process ssh_keygen = CHILD_PROCESS_INIT;
822822
int ret = -1;
823823
struct strbuf fingerprint_stdout = STRBUF_INIT;
824-
struct strbuf **fingerprint;
825-
char *fingerprint_ret;
824+
char *fingerprint_ret, *begin, *delim;
826825
const char *literal_key = NULL;
827826

828827
/*
@@ -845,13 +844,17 @@ static char *get_ssh_key_fingerprint(const char *signing_key)
845844
die_errno(_("failed to get the ssh fingerprint for key '%s'"),
846845
signing_key);
847846

848-
fingerprint = strbuf_split_max(&fingerprint_stdout, ' ', 3);
849-
if (!fingerprint[1])
850-
die_errno(_("failed to get the ssh fingerprint for key '%s'"),
847+
begin = fingerprint_stdout.buf;
848+
delim = strchr(begin, ' ');
849+
if (!delim)
850+
die(_("failed to get the ssh fingerprint for key %s"),
851851
signing_key);
852-
853-
fingerprint_ret = strbuf_detach(fingerprint[1], NULL);
854-
strbuf_list_free(fingerprint);
852+
begin = delim + 1;
853+
delim = strchr(begin, ' ');
854+
if (!delim)
855+
die(_("failed to get the ssh fingerprint for key %s"),
856+
signing_key);
857+
fingerprint_ret = xmemdupz(begin, delim - begin);
855858
strbuf_release(&fingerprint_stdout);
856859
return fingerprint_ret;
857860
}
@@ -862,12 +865,12 @@ static char *get_default_ssh_signing_key(void)
862865
struct child_process ssh_default_key = CHILD_PROCESS_INIT;
863866
int ret = -1;
864867
struct strbuf key_stdout = STRBUF_INIT, key_stderr = STRBUF_INIT;
865-
struct strbuf **keys;
866868
char *key_command = NULL;
867869
const char **argv;
868870
int n;
869871
char *default_key = NULL;
870872
const char *literal_key = NULL;
873+
char *begin, *new_line, *first_line;
871874

872875
if (!ssh_default_key_command)
873876
die(_("either user.signingkey or gpg.ssh.defaultKeyCommand needs to be configured"));
@@ -884,19 +887,24 @@ static char *get_default_ssh_signing_key(void)
884887
&key_stderr, 0);
885888

886889
if (!ret) {
887-
keys = strbuf_split_max(&key_stdout, '\n', 2);
888-
if (keys[0] && is_literal_ssh_key(keys[0]->buf, &literal_key)) {
890+
begin = key_stdout.buf;
891+
new_line = strchr(begin, '\n');
892+
if (new_line)
893+
first_line = xmemdupz(begin, new_line - begin);
894+
else
895+
first_line = xstrdup(begin);
896+
if (is_literal_ssh_key(first_line, &literal_key)) {
889897
/*
890898
* We only use `is_literal_ssh_key` here to check validity
891899
* The prefix will be stripped when the key is used.
892900
*/
893-
default_key = strbuf_detach(keys[0], NULL);
901+
default_key = first_line;
894902
} else {
903+
free(first_line);
895904
warning(_("gpg.ssh.defaultKeyCommand succeeded but returned no keys: %s %s"),
896905
key_stderr.buf, key_stdout.buf);
897906
}
898907

899-
strbuf_list_free(keys);
900908
} else {
901909
warning(_("gpg.ssh.defaultKeyCommand failed: %s %s"),
902910
key_stderr.buf, key_stdout.buf);

0 commit comments

Comments
 (0)