[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 3/4] _asn1_ordering_*(): Fix memory leak in case of error
From: |
Kurt Roeckx |
Subject: |
[PATCH 3/4] _asn1_ordering_*(): Fix memory leak in case of error |
Date: |
Sun, 20 Apr 2014 16:50:02 +0200 |
---
lib/coding.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 52 insertions(+), 14 deletions(-)
diff --git a/lib/coding.c b/lib/coding.c
index 38824b3..6c0aafe 100644
--- a/lib/coding.c
+++ b/lib/coding.c
@@ -714,6 +714,7 @@ _asn1_ordering_set (unsigned char *der, int der_len,
asn1_node node)
asn1_node p;
unsigned char class, *temp;
unsigned long tag, t;
+ int err;
counter = 0;
@@ -733,7 +734,10 @@ _asn1_ordering_set (unsigned char *der, int der_len,
asn1_node node)
{
p_vet = malloc (sizeof (struct vet));
if (p_vet == NULL)
- return ASN1_MEM_ALLOC_ERROR;
+ {
+ err = ASN1_MEM_ALLOC_ERROR;
+ goto error;
+ }
p_vet->next = NULL;
p_vet->prev = last;
@@ -744,10 +748,10 @@ _asn1_ordering_set (unsigned char *der, int der_len,
asn1_node node)
last = p_vet;
/* tag value calculation */
- if (asn1_get_tag_der
- (der + counter, der_len - counter, &class, &len2,
- &tag) != ASN1_SUCCESS)
- return ASN1_DER_ERROR;
+ err = asn1_get_tag_der (der + counter, der_len - counter, &class, &len2,
+ &tag);
+ if (err != ASN1_SUCCESS)
+ goto error;
t = class << 24;
p_vet->value = t | tag;
@@ -756,7 +760,10 @@ _asn1_ordering_set (unsigned char *der, int der_len,
asn1_node node)
/* extraction and length */
len2 = asn1_get_length_der (der + counter, der_len - counter, &len);
if (len2 < 0)
- return ASN1_DER_ERROR;
+ {
+ err = ASN1_DER_ERROR;
+ goto error;
+ }
counter += len + len2;
p_vet->end = counter;
@@ -776,7 +783,10 @@ _asn1_ordering_set (unsigned char *der, int der_len,
asn1_node node)
/* change position */
temp = malloc (p_vet->end - counter);
if (temp == NULL)
- return ASN1_MEM_ALLOC_ERROR;
+ {
+ err = ASN1_MEM_ALLOC_ERROR;
+ goto error;
+ }
memcpy (temp, der + counter, p_vet->end - counter);
memcpy (der + counter, der + p_vet->end,
@@ -805,6 +815,15 @@ _asn1_ordering_set (unsigned char *der, int der_len,
asn1_node node)
p_vet = first;
}
return ASN1_SUCCESS;
+
+error:
+ while (first != NULL)
+ {
+ p_vet = first;
+ first = first->next;
+ free(p_vet);
+ }
+ return err;
}
/******************************************************/
@@ -832,6 +851,7 @@ _asn1_ordering_set_of (unsigned char *der, int der_len,
asn1_node node)
asn1_node p;
unsigned char *temp, class;
unsigned long k, max;
+ int err;
counter = 0;
@@ -852,7 +872,10 @@ _asn1_ordering_set_of (unsigned char *der, int der_len,
asn1_node node)
{
p_vet = malloc (sizeof (struct vet));
if (p_vet == NULL)
- return ASN1_MEM_ALLOC_ERROR;
+ {
+ err = ASN1_MEM_ALLOC_ERROR;
+ goto error;
+ }
p_vet->next = NULL;
p_vet->prev = last;
@@ -866,15 +889,18 @@ _asn1_ordering_set_of (unsigned char *der, int der_len,
asn1_node node)
if (der_len - counter > 0)
{
- if (asn1_get_tag_der
- (der + counter, der_len - counter, &class, &len,
- NULL) != ASN1_SUCCESS)
- return ASN1_DER_ERROR;
+ err = asn1_get_tag_der (der + counter, der_len - counter, &class,
+ &len, NULL);
+ if (err != ASN1_SUCCESS)
+ goto error;
counter += len;
len2 = asn1_get_length_der (der + counter, der_len - counter, &len);
if (len2 < 0)
- return ASN1_DER_ERROR;
+ {
+ err = ASN1_DER_ERROR;
+ goto error;
+ }
counter += len + len2;
}
@@ -917,7 +943,10 @@ _asn1_ordering_set_of (unsigned char *der, int der_len,
asn1_node node)
/* change position */
temp = malloc (p_vet->end - counter);
if (temp == NULL)
- return ASN1_MEM_ALLOC_ERROR;
+ {
+ err = ASN1_MEM_ALLOC_ERROR;
+ goto error;
+ }
memcpy (temp, der + counter, (p_vet->end) - counter);
memcpy (der + counter, der + (p_vet->end),
@@ -942,6 +971,15 @@ _asn1_ordering_set_of (unsigned char *der, int der_len,
asn1_node node)
p_vet = first;
}
return ASN1_SUCCESS;
+
+error:
+ while (first != NULL)
+ {
+ p_vet = first;
+ first = first->next;
+ free(p_vet);
+ }
+ return err;
}
/**
--
1.9.1
- [PATCH 1/4] Call ordering functions with the right length, Kurt Roeckx, 2014/04/20
- [PATCH 4/4] Fix memory leak., Kurt Roeckx, 2014/04/20
- [PATCH 3/4] _asn1_ordering_*(): Fix memory leak in case of error,
Kurt Roeckx <=
- [PATCH 2/4] Make _asn1_ordering_* return error values and check them., Kurt Roeckx, 2014/04/20
- Re: [PATCH 2/4] Make _asn1_ordering_* return error values and check them., Nikos Mavrogiannopoulos, 2014/04/21
- Re: [PATCH 2/4] Make _asn1_ordering_* return error values and check them., Kurt Roeckx, 2014/04/24
- Re: [PATCH 2/4] Make _asn1_ordering_* return error values and check them., Nikos Mavrogiannopoulos, 2014/04/25
- Re: [PATCH 2/4] Make _asn1_ordering_* return error values and check them., Kurt Roeckx, 2014/04/24
- Re: [PATCH 2/4] Make _asn1_ordering_* return error values and check them., Kurt Roeckx, 2014/04/24
- Re: [PATCH 2/4] Make _asn1_ordering_* return error values and check them., Kurt Roeckx, 2014/04/25
- Re: [PATCH 2/4] Make _asn1_ordering_* return error values and check them., Nikos Mavrogiannopoulos, 2014/04/26
- Re: [PATCH 2/4] Make _asn1_ordering_* return error values and check them., Kurt Roeckx, 2014/04/26
- Re: [PATCH 2/4] Make _asn1_ordering_* return error values and check them., Nikos Mavrogiannopoulos, 2014/04/26