Skip to content

Commit

Permalink
src/signature: Fix leaking GString in get_pubkey_hash()
Browse files Browse the repository at this point in the history
The variable 'string' will not be freed on error.
Use g_autoptr to automatically free 'string' and use this to simplify
code.

Fixes coverity issue:
| CID 1445505 (#1 of 1): Resource leak (RESOURCE_LEAK)
| 7. leaked_storage: Variable string going out of scope leaks the storage it points to

Signed-off-by: Enrico Joerns <[email protected]>
  • Loading branch information
ejoerns committed Mar 2, 2022
1 parent 39a0c7a commit 87b0677
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/signature.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,7 @@ GBytes *cms_sign(GBytes *content, gboolean detached, const gchar *certfile, cons

gchar* get_pubkey_hash(X509 *cert)
{
gchar *data = NULL;
GString *string;
g_autoptr(GString) string = NULL;
g_autofree unsigned char *der_buf = NULL;
unsigned char *tmp_buf = NULL;
unsigned int len = 0;
Expand All @@ -620,7 +619,7 @@ gchar* get_pubkey_hash(X509 *cert)
len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), NULL);
if (len <= 0) {
g_warning("DER Encoding failed");
goto out;
return NULL;
}
/* As i2d_X509_PUBKEY() moves pointer after end of data,
* we must use a tmp pointer, here */
Expand All @@ -631,7 +630,7 @@ gchar* get_pubkey_hash(X509 *cert)

if (!EVP_Digest(der_buf, len, md, &n, EVP_sha256(), NULL)) {
g_warning("Error in EVP_Digest");
goto out;
return NULL;
}

g_assert_cmpint(n, ==, SHA256_DIGEST_LENGTH);
Expand All @@ -641,9 +640,7 @@ gchar* get_pubkey_hash(X509 *cert)
}
g_string_truncate(string, SHA256_DIGEST_LENGTH * 3 - 1);

data = g_string_free(string, FALSE);
out:
return data;
return g_string_free(g_steal_pointer(&string), FALSE);
}

gchar** get_pubkey_hashes(STACK_OF(X509) *verified_chain)
Expand Down

0 comments on commit 87b0677

Please sign in to comment.