summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2015-07-31 22:00:53 +0200
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2015-07-31 22:06:53 +0200
commit58bd9788dee5bd1858ec26391064bf453bb758a0 (patch)
tree4551c1b4fed60fc33943206f0166d8638d21a49b
parent9ae6c9a877495aa7b3d8e473c7957aaed4797af8 (diff)
downloadgnutls-58bd9788dee5bd1858ec26391064bf453bb758a0.tar.gz
made data2hex() safer, and eliminated mem leak
-rw-r--r--lib/x509/common.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/lib/x509/common.c b/lib/x509/common.c
index 5952b4b972..8e8ee68fb5 100644
--- a/lib/x509/common.c
+++ b/lib/x509/common.c
@@ -489,29 +489,33 @@ static int
data2hex(const void *data, size_t data_size,
gnutls_datum_t *out)
{
- gnutls_datum_t tmp;
+ gnutls_datum_t tmp, td;
int ret;
size_t size;
- out->size = hex_str_size(data_size) + 1; /* +1 for '#' */
- out->data = gnutls_malloc(out->size);
- if (out->data == NULL)
+ td.size = hex_str_size(data_size) + 1; /* +1 for '#' */
+ td.data = gnutls_malloc(td.size);
+ if (td.data == NULL)
return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
tmp.data = (void*)data;
tmp.size = data_size;
- out->data[0] = '#';
- size = out->size-1; /* don't include '#' */
+ td.data[0] = '#';
+ size = td.size-1; /* don't include '#' */
ret =
gnutls_hex_encode(&tmp,
- (char*)&out->data[1], &size);
+ (char*)&td.data[1], &size);
if (ret < 0) {
gnutls_assert();
+ gnutls_free(td.data);
return GNUTLS_E_SHORT_MEMORY_BUFFER;
}
- out->size--; /* don't include null */
+ td.size--; /* don't include null */
+
+ out->data = td.data;
+ out->size = td.size;
return 0;
}