From ebb74f71ddc104e219b76f91c3307bfb6da48cd9 Mon Sep 17 00:00:00 2001 From: Simon Josefsson Date: Mon, 21 Apr 2008 12:46:41 +0200 Subject: [PATCH] Replace use of alloca with malloc. --- NEWS | 1 + configure.in | 6 +----- examples/CertificateExample.c | 2 +- examples/CrlExample.c | 4 ++-- lib/ASN1.y | 4 ++-- lib/Makefile.am | 7 +++---- lib/coding.c | 22 +++++++++++----------- lib/decoding.c | 22 +++++++++++----------- lib/element.c | 38 +++++++++++++++++++------------------- lib/int.h | 6 +++++- lib/mem.h | 27 --------------------------- 11 files changed, 56 insertions(+), 83 deletions(-) delete mode 100644 lib/mem.h diff --git a/NEWS b/NEWS index 3fe2d0b..25c0f80 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ Version 1.4 (unreleased) - Update gnulib files. +- Replace uses of alloca with malloc. Version 1.3 (released 2008-02-01) - Handle 'INTEGER { ... } (a..b)' regression. diff --git a/configure.in b/configure.in index ba0de0b..658a6f8 100644 --- a/configure.in +++ b/configure.in @@ -61,11 +61,6 @@ if test "$GCC" = "yes"; then fi fi - -AC_MSG_NOTICE([Detecting C library capabilities]) - -AC_FUNC_ALLOCA - AC_MSG_NOTICE([Detecting system's parameters]) # Run self-tests under valgrind? @@ -107,6 +102,7 @@ AC_ARG_ENABLE([ld-version-script], AM_CONDITIONAL(HAVE_LD_VERSION_SCRIPT, test "$have_ld_version_script" = "yes") # For gnulib compatibility modules in gl/. +AC_MSG_NOTICE([Running gnulib checks]) gl_INIT # For libtasn1-config. diff --git a/examples/CertificateExample.c b/examples/CertificateExample.c index f4b047e..5af525e 100644 --- a/examples/CertificateExample.c +++ b/examples/CertificateExample.c @@ -109,7 +109,7 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer) &value); len = sizeof(str) - 1; asn1_read_value(cert,name3,str,&len); - asn1_der_decoding(&value,str,len,errorDescription); + asn1_der_decoding(&value,str,len,errorDescription); len = sizeof(str) - 1; asn1_read_value(value,"",str,&len); /* CHOICE */ strcpy(name3,str); diff --git a/examples/CrlExample.c b/examples/CrlExample.c index f239cac..fbc200b 100644 --- a/examples/CrlExample.c +++ b/examples/CrlExample.c @@ -114,10 +114,10 @@ get_Name_type(node_asn *cert_def,node_asn *cert,char *root, char *answer) &value); len = sizeof(str)-1; asn1_read_value(cert,name3,str,&len); - result=asn1_der_decoding(&value,str,len,errorDescription); + result=asn1_der_decoding(&value,str,len,errorDescription); len = sizeof(str)-1; - asn1_read_value(value,"",str,&len); /* CHOICE */ + asn1_read_value(value,"",str,&len); /* CHOICE */ strcpy(name3,str); diff --git a/lib/ASN1.y b/lib/ASN1.y index 33e7bb3..7fac39e 100644 --- a/lib/ASN1.y +++ b/lib/ASN1.y @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. + * Copyright (C) 2004, 2005, 2006, 2008 Free Software Foundation, Inc. * Copyright (C) 2001, 2002 Fabio Fiorina * * This file is part of LIBTASN1. @@ -238,7 +238,7 @@ size_def: size_def2 {$$=$1;} generalstring_def: GeneralString {$$=_asn1_add_node(TYPE_GENERALSTRING);} | GeneralString size_def {$$=_asn1_add_node(TYPE_GENERALSTRING|CONST_SIZE); - _asn1_set_down($$,$2);} + _asn1_set_down($$,$2);} ; octet_string_def : OCTET STRING {$$=_asn1_add_node(TYPE_OCTET_STRING);} diff --git a/lib/Makefile.am b/lib/Makefile.am index a6dce8f..d3f5e77 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -28,10 +28,9 @@ pkgconfig_DATA = libtasn1.pc lib_LTLIBRARIES = libtasn1.la -libtasn1_la_SOURCES = libtasn1.h mem.h gstr.h errors.h \ - int.h parser_aux.h structure.h element.h ASN1.y \ - decoding.c gstr.c errors.c parser_aux.c structure.c element.c \ - coding.c libtasn1.vers +libtasn1_la_SOURCES = libtasn1.h gstr.h errors.h int.h parser_aux.h \ + structure.h element.h ASN1.y decoding.c gstr.c errors.c \ + parser_aux.c structure.c element.c coding.c libtasn1.vers libtasn1_la_LDFLAGS = -no-undefined \ -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) diff --git a/lib/coding.c b/lib/coding.c index cd646c7..74c8390 100644 --- a/lib/coding.c +++ b/lib/coding.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004, 2006 Free Software Foundation + * Copyright (C) 2004, 2006, 2008 Free Software Foundation * Copyright (C) 2002 Fabio Fiorina * * This file is part of LIBTASN1. @@ -260,7 +260,7 @@ _asn1_objectid_der (unsigned char *str, unsigned char *der, int *der_len) max_len = *der_len; - temp = (char *) _asn1_alloca (strlen (str) + 2); + temp = (char *) _asn1_malloc (strlen (str) + 2); if (temp == NULL) return ASN1_MEM_ALLOC_ERROR; @@ -312,7 +312,7 @@ _asn1_objectid_der (unsigned char *str, unsigned char *der, int *der_len) } *der_len += len_len; - _asn1_afree (temp); + _asn1_free (temp); if (max_len < (*der_len)) return ASN1_MEM_ERROR; @@ -636,7 +636,7 @@ _asn1_ordering_set (unsigned char *der, int der_len, node_asn * node) first = last = NULL; while (p) { - p_vet = (struct vet *) _asn1_alloca (sizeof (struct vet)); + p_vet = (struct vet *) _asn1_malloc (sizeof (struct vet)); if (p_vet == NULL) return; @@ -677,7 +677,7 @@ _asn1_ordering_set (unsigned char *der, int der_len, node_asn * node) if (p_vet->value > p2_vet->value) { /* change position */ - temp = (unsigned char *) _asn1_alloca (p_vet->end - counter); + temp = (unsigned char *) _asn1_malloc (p_vet->end - counter); if (temp == NULL) return; @@ -686,7 +686,7 @@ _asn1_ordering_set (unsigned char *der, int der_len, node_asn * node) p2_vet->end - p_vet->end); memcpy (der + counter + p2_vet->end - p_vet->end, temp, p_vet->end - counter); - _asn1_afree (temp); + _asn1_free (temp); tag = p_vet->value; p_vet->value = p2_vet->value; @@ -704,7 +704,7 @@ _asn1_ordering_set (unsigned char *der, int der_len, node_asn * node) p_vet->prev->next = NULL; else first = NULL; - _asn1_afree (p_vet); + _asn1_free (p_vet); p_vet = first; } } @@ -750,7 +750,7 @@ _asn1_ordering_set_of (unsigned char *der, int der_len, node_asn * node) first = last = NULL; while (p) { - p_vet = (struct vet *) _asn1_alloca (sizeof (struct vet)); + p_vet = (struct vet *) _asn1_malloc (sizeof (struct vet)); if (p_vet == NULL) return; @@ -815,7 +815,7 @@ _asn1_ordering_set_of (unsigned char *der, int der_len, node_asn * node) if (change == 1) { /* change position */ - temp = (unsigned char *) _asn1_alloca (p_vet->end - counter); + temp = (unsigned char *) _asn1_malloc (p_vet->end - counter); if (temp == NULL) return; @@ -824,7 +824,7 @@ _asn1_ordering_set_of (unsigned char *der, int der_len, node_asn * node) (p2_vet->end) - (p_vet->end)); memcpy (der + counter + (p2_vet->end) - (p_vet->end), temp, (p_vet->end) - counter); - _asn1_afree (temp); + _asn1_free (temp); p_vet->end = counter + (p2_vet->end - p_vet->end); } @@ -838,7 +838,7 @@ _asn1_ordering_set_of (unsigned char *der, int der_len, node_asn * node) p_vet->prev->next = NULL; else first = NULL; - _asn1_afree (p_vet); + _asn1_free (p_vet); p_vet = first; } } diff --git a/lib/decoding.c b/lib/decoding.c index 5d51a4e..c1de9e6 100644 --- a/lib/decoding.c +++ b/lib/decoding.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004, 2006 Free Software Foundation + * Copyright (C) 2004, 2006, 2008 Free Software Foundation * Copyright (C) 2002 Fabio Fiorina * * This file is part of LIBTASN1. @@ -618,7 +618,7 @@ _asn1_get_octet_string (const unsigned char *der, node_asn * node, int *len) if (node) { asn1_length_der (tot_len, NULL, &len2); - temp = _asn1_alloca (len2 + tot_len); + temp = _asn1_malloc (len2 + tot_len); if (temp == NULL) { return ASN1_MEM_ALLOC_ERROR; @@ -653,7 +653,7 @@ _asn1_get_octet_string (const unsigned char *der, node_asn * node, int *len) } _asn1_set_value (node, temp, tot_len); - _asn1_afree (temp); + _asn1_free (temp); } } else @@ -1193,7 +1193,7 @@ asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len, { len2 += len4; asn1_length_der (len2 + len3, NULL, &len4); - temp2 = (unsigned char *) _asn1_alloca (len2 + len3 + len4); + temp2 = (unsigned char *) _asn1_malloc (len2 + len3 + len4); if (temp2 == NULL) { asn1_delete_structure (element); @@ -1202,7 +1202,7 @@ asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len, asn1_octet_der (der + counter, len2 + len3, temp2, &len4); _asn1_set_value (p, temp2, len4); - _asn1_afree (temp2); + _asn1_free (temp2); counter += len2 + len3; } else @@ -1222,7 +1222,7 @@ asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len, return ris; } asn1_length_der (len2, NULL, &len4); - temp2 = (unsigned char *) _asn1_alloca (len2 + len4); + temp2 = (unsigned char *) _asn1_malloc (len2 + len4); if (temp2 == NULL) { asn1_delete_structure (element); @@ -1231,7 +1231,7 @@ asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len, asn1_octet_der (der + counter, len2, temp2, &len4); _asn1_set_value (p, temp2, len4); - _asn1_afree (temp2); + _asn1_free (temp2); counter += len2; /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with @@ -1879,7 +1879,7 @@ asn1_der_decoding_element (ASN1_TYPE * structure, const char *elementName, { asn1_length_der (len2 + len3, NULL, &len4); temp2 = - (unsigned char *) _asn1_alloca (len2 + len3 + len4); + (unsigned char *) _asn1_malloc (len2 + len3 + len4); if (temp2 == NULL) { asn1_delete_structure (structure); @@ -1889,7 +1889,7 @@ asn1_der_decoding_element (ASN1_TYPE * structure, const char *elementName, asn1_octet_der (der + counter, len2 + len3, temp2, &len4); _asn1_set_value (p, temp2, len4); - _asn1_afree (temp2); + _asn1_free (temp2); if (p == nodeFound) state = EXIT; @@ -1916,7 +1916,7 @@ asn1_der_decoding_element (ASN1_TYPE * structure, const char *elementName, if (state == FOUND) { asn1_length_der (len2, NULL, &len4); - temp2 = (unsigned char *) _asn1_alloca (len2 + len4); + temp2 = (unsigned char *) _asn1_malloc (len2 + len4); if (temp2 == NULL) { asn1_delete_structure (structure); @@ -1925,7 +1925,7 @@ asn1_der_decoding_element (ASN1_TYPE * structure, const char *elementName, asn1_octet_der (der + counter, len2, temp2, &len4); _asn1_set_value (p, temp2, len4); - _asn1_afree (temp2); + _asn1_free (temp2); if (p == nodeFound) state = EXIT; diff --git a/lib/element.c b/lib/element.c index fd66f24..752b40d 100644 --- a/lib/element.c +++ b/lib/element.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004, 2006 Free Software Foundation + * Copyright (C) 2004, 2006, 2008 Free Software Foundation * Copyright (C) 2000, 2001, 2002, 2003 Fabio Fiorina * * This file is part of LIBTASN1. @@ -350,7 +350,7 @@ asn1_write_value (ASN1_TYPE node_root, const char *name, if ((isdigit (value[0])) || (value[0] == '-')) { value_temp = - (unsigned char *) _asn1_alloca (SIZEOF_UNSIGNED_LONG_INT); + (unsigned char *) _asn1_malloc (SIZEOF_UNSIGNED_LONG_INT); if (value_temp == NULL) return ASN1_MEM_ALLOC_ERROR; @@ -370,7 +370,7 @@ asn1_write_value (ASN1_TYPE node_root, const char *name, { value_temp = (unsigned char *) - _asn1_alloca (SIZEOF_UNSIGNED_LONG_INT); + _asn1_malloc (SIZEOF_UNSIGNED_LONG_INT); if (value_temp == NULL) return ASN1_MEM_ALLOC_ERROR; @@ -389,7 +389,7 @@ asn1_write_value (ASN1_TYPE node_root, const char *name, } else { /* len != 0 */ - value_temp = (unsigned char *) _asn1_alloca (len); + value_temp = (unsigned char *) _asn1_malloc (len); if (value_temp == NULL) return ASN1_MEM_ALLOC_ERROR; memcpy (value_temp, value, len); @@ -403,7 +403,7 @@ asn1_write_value (ASN1_TYPE node_root, const char *name, if (negative && (type_field (node->type) == TYPE_ENUMERATED)) { - _asn1_afree (value_temp); + _asn1_free (value_temp); return ASN1_VALUE_NOT_VALID; } @@ -418,14 +418,14 @@ asn1_write_value (ASN1_TYPE node_root, const char *name, k--; asn1_length_der (len - k, NULL, &len2); - temp = (unsigned char *) _asn1_alloca (len - k + len2); + temp = (unsigned char *) _asn1_malloc (len - k + len2); if (temp == NULL) return ASN1_MEM_ALLOC_ERROR; asn1_octet_der (value_temp + k, len - k, temp, &len2); _asn1_set_value (node, temp, len2); - _asn1_afree (temp); + _asn1_free (temp); if (node->type & CONST_DEFAULT) @@ -436,7 +436,7 @@ asn1_write_value (ASN1_TYPE node_root, const char *name, if ((isdigit (p->value[0])) || (p->value[0] == '-')) { default_temp = - (unsigned char *) _asn1_alloca (SIZEOF_UNSIGNED_LONG_INT); + (unsigned char *) _asn1_malloc (SIZEOF_UNSIGNED_LONG_INT); if (default_temp == NULL) return ASN1_MEM_ALLOC_ERROR; @@ -456,7 +456,7 @@ asn1_write_value (ASN1_TYPE node_root, const char *name, { default_temp = (unsigned char *) - _asn1_alloca (SIZEOF_UNSIGNED_LONG_INT); + _asn1_malloc (SIZEOF_UNSIGNED_LONG_INT); if (default_temp == NULL) return ASN1_MEM_ALLOC_ERROR; @@ -484,9 +484,9 @@ asn1_write_value (ASN1_TYPE node_root, const char *name, if (k2 == len2) _asn1_set_value (node, NULL, 0); } - _asn1_afree (default_temp); + _asn1_free (default_temp); } - _asn1_afree (value_temp); + _asn1_free (value_temp); break; case TYPE_OBJECT_ID: for (k = 0; k < strlen (value); k++) @@ -555,37 +555,37 @@ asn1_write_value (ASN1_TYPE node_root, const char *name, if (len == 0) len = strlen (value); asn1_length_der (len, NULL, &len2); - temp = (unsigned char *) _asn1_alloca (len + len2); + temp = (unsigned char *) _asn1_malloc (len + len2); if (temp == NULL) return ASN1_MEM_ALLOC_ERROR; asn1_octet_der (value, len, temp, &len2); _asn1_set_value (node, temp, len2); - _asn1_afree (temp); + _asn1_free (temp); break; case TYPE_GENERALSTRING: if (len == 0) len = strlen (value); asn1_length_der (len, NULL, &len2); - temp = (unsigned char *) _asn1_alloca (len + len2); + temp = (unsigned char *) _asn1_malloc (len + len2); if (temp == NULL) return ASN1_MEM_ALLOC_ERROR; asn1_octet_der (value, len, temp, &len2); _asn1_set_value (node, temp, len2); - _asn1_afree (temp); + _asn1_free (temp); break; case TYPE_BIT_STRING: if (len == 0) len = strlen (value); asn1_length_der ((len >> 3) + 2, NULL, &len2); - temp = (unsigned char *) _asn1_alloca ((len >> 3) + 2 + len2); + temp = (unsigned char *) _asn1_malloc ((len >> 3) + 2 + len2); if (temp == NULL) return ASN1_MEM_ALLOC_ERROR; asn1_bit_der (value, len, temp, &len2); _asn1_set_value (node, temp, len2); - _asn1_afree (temp); + _asn1_free (temp); break; case TYPE_CHOICE: p = node->down; @@ -613,13 +613,13 @@ asn1_write_value (ASN1_TYPE node_root, const char *name, break; case TYPE_ANY: asn1_length_der (len, NULL, &len2); - temp = (unsigned char *) _asn1_alloca (len + len2); + temp = (unsigned char *) _asn1_malloc (len + len2); if (temp == NULL) return ASN1_MEM_ALLOC_ERROR; asn1_octet_der (value, len, temp, &len2); _asn1_set_value (node, temp, len2); - _asn1_afree (temp); + _asn1_free (temp); break; case TYPE_SEQUENCE_OF: case TYPE_SET_OF: diff --git a/lib/int.h b/lib/int.h index 5c89715..0c9c06f 100644 --- a/lib/int.h +++ b/lib/int.h @@ -45,7 +45,11 @@ #define LIBTASN1_DEBUG_INTEGER */ -#include +#define _asn1_malloc malloc +#define _asn1_free free +#define _asn1_calloc calloc +#define _asn1_realloc realloc +#define _asn1_strdup strdup #define MAX_LOG_SIZE 1024 /* maximum number of characters of a log message */ diff --git a/lib/mem.h b/lib/mem.h deleted file mode 100644 index 267f62f..0000000 --- a/lib/mem.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef MEM_H -# define MEM_H - -/* Use _asn1_afree() when calling alloca, or - * memory leaks may occur in systems which do not - * support alloca. - */ -#ifdef HAVE_ALLOCA -# ifdef HAVE_ALLOCA_H -# include -# endif -# define _asn1_alloca alloca -# define _asn1_afree(x) -#else -# define _asn1_alloca _asn1_malloc -# define _asn1_afree _asn1_free -#endif /* HAVE_ALLOCA */ - -#define _asn1_malloc malloc -#define _asn1_free free -#define _asn1_calloc calloc -#define _asn1_realloc realloc -#define _asn1_strdup strdup - -#endif /* MEM_H */ - - -- 2.7.4