mirror of
https://github.com/git/git.git
synced 2024-05-23 23:06:12 +02:00
gpg-interface: refactor the free-and-xmemdupz pattern
Introduce a static replace_cstring() function to simplify repeated pattern of free-and-xmemdupz() for GPG status line parsing. This also helps us avoid potential memleaks if parsing of new status lines are introduced in the future. Signed-off-by: Hans Jerry Illikainen <hji@dyntopia.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
75b2f01a0f
commit
392b862e9a
|
@ -105,6 +105,16 @@ static struct {
|
||||||
{ 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
|
{ 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void replace_cstring(char **field, const char *line, const char *next)
|
||||||
|
{
|
||||||
|
free(*field);
|
||||||
|
|
||||||
|
if (line && next)
|
||||||
|
*field = xmemdupz(line, next - line);
|
||||||
|
else
|
||||||
|
*field = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static void parse_gpg_output(struct signature_check *sigc)
|
static void parse_gpg_output(struct signature_check *sigc)
|
||||||
{
|
{
|
||||||
const char *buf = sigc->gpg_status;
|
const char *buf = sigc->gpg_status;
|
||||||
|
@ -136,21 +146,18 @@ static void parse_gpg_output(struct signature_check *sigc)
|
||||||
/* Do we have key information? */
|
/* Do we have key information? */
|
||||||
if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
|
if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
|
||||||
next = strchrnul(line, ' ');
|
next = strchrnul(line, ' ');
|
||||||
free(sigc->key);
|
replace_cstring(&sigc->key, line, next);
|
||||||
sigc->key = xmemdupz(line, next - line);
|
|
||||||
/* Do we have signer information? */
|
/* Do we have signer information? */
|
||||||
if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
|
if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
|
||||||
line = next + 1;
|
line = next + 1;
|
||||||
next = strchrnul(line, '\n');
|
next = strchrnul(line, '\n');
|
||||||
free(sigc->signer);
|
replace_cstring(&sigc->signer, line, next);
|
||||||
sigc->signer = xmemdupz(line, next - line);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Do we have fingerprint? */
|
/* Do we have fingerprint? */
|
||||||
if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
|
if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
|
||||||
next = strchrnul(line, ' ');
|
next = strchrnul(line, ' ');
|
||||||
free(sigc->fingerprint);
|
replace_cstring(&sigc->fingerprint, line, next);
|
||||||
sigc->fingerprint = xmemdupz(line, next - line);
|
|
||||||
|
|
||||||
/* Skip interim fields */
|
/* Skip interim fields */
|
||||||
for (j = 9; j > 0; j--) {
|
for (j = 9; j > 0; j--) {
|
||||||
|
@ -162,7 +169,8 @@ static void parse_gpg_output(struct signature_check *sigc)
|
||||||
|
|
||||||
next = strchrnul(line, '\n');
|
next = strchrnul(line, '\n');
|
||||||
free(sigc->primary_key_fingerprint);
|
free(sigc->primary_key_fingerprint);
|
||||||
sigc->primary_key_fingerprint = xmemdupz(line, next - line);
|
replace_cstring(&sigc->primary_key_fingerprint,
|
||||||
|
line, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue