summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2016-04-04 14:36:07 +0200
committerNikos Mavrogiannopoulos <nmav@redhat.com>2016-04-04 14:36:10 +0200
commit0301945a0f1e33ff9e02175c7848fc7c0a91b6db (patch)
treee1eeda121169010731ae6cb7facd36895528061c
parent4633168cd5ab694a053e4ee433f999f8e82c1f47 (diff)
downloadlibtasn1-0301945a0f1e33ff9e02175c7848fc7c0a91b6db.tar.gz
use a safer variant of realloc
This variant does not create memory leaks if allocation fails. Report and initial patch by Pascal Cuoq.
-rw-r--r--lib/decoding.c4
-rw-r--r--lib/int.h16
-rw-r--r--lib/parser_aux.c4
3 files changed, 20 insertions, 4 deletions
diff --git a/lib/decoding.c b/lib/decoding.c
index 3f459f2..4fa045c 100644
--- a/lib/decoding.c
+++ b/lib/decoding.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2002-2014 Free Software Foundation, Inc.
+ * Copyright (C) 2002-2016 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
@@ -2193,7 +2193,7 @@ asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
static int append(uint8_t **dst, unsigned *dst_size, const unsigned char *src, unsigned src_size)
{
- *dst = realloc(*dst, *dst_size+src_size);
+ *dst = _asn1_realloc(*dst, *dst_size+src_size);
if (*dst == NULL)
return ASN1_MEM_ERROR;
memcpy(*dst + *dst_size, src, src_size);
diff --git a/lib/int.h b/lib/int.h
index 322b80f..a3e890d 100644
--- a/lib/int.h
+++ b/lib/int.h
@@ -197,4 +197,20 @@ convert_old_type (unsigned int ntype)
return ntype;
}
+static inline
+void *_asn1_realloc(void *ptr, size_t size)
+{
+ void *ret;
+
+ if (size == 0)
+ return ptr;
+
+ ret = realloc(ptr, size);
+ if (ret == NULL)
+ {
+ free(ptr);
+ }
+ return ret;
+}
+
#endif /* INT_H */
diff --git a/lib/parser_aux.c b/lib/parser_aux.c
index 8e85bf8..0a1f645 100644
--- a/lib/parser_aux.c
+++ b/lib/parser_aux.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2000-2014 Free Software Foundation, Inc.
+ * Copyright (C) 2000-2016 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
@@ -321,7 +321,7 @@ _asn1_append_value (asn1_node node, const void *value, unsigned int len)
/* value is allocated */
int prev_len = node->value_len;
node->value_len += len;
- node->value = realloc (node->value, node->value_len);
+ node->value = _asn1_realloc (node->value, node->value_len);
if (node->value == NULL)
{
node->value_len = 0;