From 0301945a0f1e33ff9e02175c7848fc7c0a91b6db Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Mon, 4 Apr 2016 14:36:07 +0200 Subject: use a safer variant of realloc This variant does not create memory leaks if allocation fails. Report and initial patch by Pascal Cuoq. --- lib/decoding.c | 4 ++-- lib/int.h | 16 ++++++++++++++++ lib/parser_aux.c | 4 ++-- 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; -- cgit v1.2.1