summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2002-09-09 05:17:24 +0000
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2002-09-09 05:17:24 +0000
commitc472c61f649a0c291a72bd18d742d4e48c69be95 (patch)
tree57535c51f4d8193c5f44a74291628ddaaacf60c6
parentc8d5c206ce66a0315e65161911a3328d9e3f40dd (diff)
downloadgnutls-c472c61f649a0c291a72bd18d742d4e48c69be95.tar.gz
Some fixes for the used realloc() function. Now we have gnutls_realloc_fast() which frees the given pointer if the new allocation failed.
-rw-r--r--lib/auth_dhe.c2
-rw-r--r--lib/auth_rsa_export.c2
-rw-r--r--lib/gnutls_algorithms.c2
-rw-r--r--lib/gnutls_compress_int.c2
-rw-r--r--lib/gnutls_extensions.c2
-rw-r--r--lib/gnutls_handshake.c6
-rw-r--r--lib/gnutls_mem.c18
-rw-r--r--lib/gnutls_mem.h5
-rw-r--r--lib/gnutls_str.c4
-rw-r--r--lib/gnutls_x509.c12
-rw-r--r--lib/x509_xml.c1
-rw-r--r--libextra/gnutls_openpgp.c15
-rw-r--r--libextra/gnutls_srp.c9
13 files changed, 53 insertions, 27 deletions
diff --git a/lib/auth_dhe.c b/lib/auth_dhe.c
index 5f61104e81..d169469c7f 100644
--- a/lib/auth_dhe.c
+++ b/lib/auth_dhe.c
@@ -200,7 +200,7 @@ static int gen_dhe_server_kx(gnutls_session session, opaque ** data)
return data_size; /* do not put a signature - ILLEGAL! */
}
- *data = gnutls_realloc(*data, data_size + signature.size + 2);
+ *data = gnutls_realloc_fast(*data, data_size + signature.size + 2);
if (*data == NULL) {
gnutls_free_datum(&signature);
gnutls_assert();
diff --git a/lib/auth_rsa_export.c b/lib/auth_rsa_export.c
index 5967c9c7b3..52bda95ae2 100644
--- a/lib/auth_rsa_export.c
+++ b/lib/auth_rsa_export.c
@@ -165,7 +165,7 @@ static int gen_rsa_export_server_kx(gnutls_session session, opaque ** data)
return data_size; /* do not put a signature - ILLEGAL! */
}
- *data = gnutls_realloc(*data, data_size + signature.size + 2);
+ *data = gnutls_realloc_fast(*data, data_size + signature.size + 2);
if (*data == NULL) {
gnutls_free_datum(&signature);
gnutls_assert();
diff --git a/lib/gnutls_algorithms.c b/lib/gnutls_algorithms.c
index 54588f8d15..39dfa93328 100644
--- a/lib/gnutls_algorithms.c
+++ b/lib/gnutls_algorithms.c
@@ -1130,7 +1130,7 @@ _gnutls_supported_ciphersuites(gnutls_session session,
#if 0 /* expensive */
if (ret_count > 0 && ret_count != count) {
ciphers =
- gnutls_realloc(ciphers,
+ gnutls_realloc_fast(ciphers,
ret_count * sizeof(GNUTLS_CipherSuite));
} else {
if (ret_count != count) {
diff --git a/lib/gnutls_compress_int.c b/lib/gnutls_compress_int.c
index c40e4ac2d5..3d28775ad7 100644
--- a/lib/gnutls_compress_int.c
+++ b/lib/gnutls_compress_int.c
@@ -184,7 +184,7 @@ int cur_pos;
do {
out_size += 128;
- *plain = gnutls_realloc( *plain, out_size);
+ *plain = gnutls_realloc_fast( *plain, out_size);
if (*plain==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
diff --git a/lib/gnutls_extensions.c b/lib/gnutls_extensions.c
index 68fe3844d3..b2f775f371 100644
--- a/lib/gnutls_extensions.c
+++ b/lib/gnutls_extensions.c
@@ -194,7 +194,7 @@ int (*ext_func_send)( gnutls_session, opaque*, int);
size = ext_func_send( session, sdata, sdata_size);
if (size > 0) {
- (*data) = gnutls_realloc( (*data), pos+size+4);
+ (*data) = gnutls_realloc_fast( (*data), pos+size+4);
if ((*data)==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
diff --git a/lib/gnutls_handshake.c b/lib/gnutls_handshake.c
index 5dcc83dbf6..aaa053e806 100644
--- a/lib/gnutls_handshake.c
+++ b/lib/gnutls_handshake.c
@@ -1511,7 +1511,7 @@ static int _gnutls_send_client_hello(gnutls_session session, int again)
extdatalen = _gnutls_copy_ciphersuites(session, &extdata);
if (extdatalen > 0) {
datalen += extdatalen;
- data = gnutls_realloc(data, datalen);
+ data = gnutls_realloc_fast(data, datalen);
if (data == NULL) {
gnutls_assert();
gnutls_free(extdata);
@@ -1536,7 +1536,7 @@ static int _gnutls_send_client_hello(gnutls_session session, int again)
extdatalen = _gnutls_copy_comp_methods(session, &extdata);
if (extdatalen > 0) {
datalen += extdatalen;
- data = gnutls_realloc(data, datalen);
+ data = gnutls_realloc_fast(data, datalen);
if (data == NULL) {
gnutls_assert();
gnutls_free(extdata);
@@ -1561,7 +1561,7 @@ static int _gnutls_send_client_hello(gnutls_session session, int again)
extdatalen = _gnutls_gen_extensions(session, &extdata);
if (extdatalen > 0) {
datalen += extdatalen;
- data = gnutls_realloc(data, datalen);
+ data = gnutls_realloc_fast(data, datalen);
if (data == NULL) {
gnutls_assert();
gnutls_free(extdata);
diff --git a/lib/gnutls_mem.c b/lib/gnutls_mem.c
index f4a7481855..340e82f19e 100644
--- a/lib/gnutls_mem.c
+++ b/lib/gnutls_mem.c
@@ -60,6 +60,24 @@ svoid *gnutls_secure_calloc(size_t nmemb, size_t size)
return ret;
}
+/* This realloc will free ptr in case realloc
+ * fails.
+ */
+void* gnutls_realloc_fast( void* ptr, size_t size)
+{
+void *ret;
+
+ if (size == 0) return ptr;
+
+ ret = gnutls_realloc( ptr, size);
+ if ( ret == NULL) {
+ gnutls_free( ptr);
+ return NULL;
+ }
+
+ return ret;
+}
+
char* _gnutls_strdup( const char* str) {
int siz = strlen( str);
char * ret;
diff --git a/lib/gnutls_mem.h b/lib/gnutls_mem.h
index 5beba83b05..aedf27efb0 100644
--- a/lib/gnutls_mem.h
+++ b/lib/gnutls_mem.h
@@ -33,7 +33,10 @@ extern REALLOC_FUNC gnutls_realloc;
extern void* (*gnutls_calloc)(size_t, size_t);
extern char* (*gnutls_strdup)( const char*);
-#define gnutls_realloc_fast(x, y) (y==0?x:realloc(x, y))
+/* this realloc function will return ptr if size==0, and
+ * will free the ptr if the new allocation failed.
+ */
+void* gnutls_realloc_fast( void* ptr, size_t size);
svoid* gnutls_secure_calloc( size_t nmemb, size_t size);
void* _gnutls_calloc( size_t nmemb, size_t size);
diff --git a/lib/gnutls_str.c b/lib/gnutls_str.c
index 145d7041d2..71313d96f6 100644
--- a/lib/gnutls_str.c
+++ b/lib/gnutls_str.c
@@ -137,7 +137,7 @@ int _gnutls_string_append_str( gnutls_string* dest, const char * src)
return tot_len;
} else {
- size_t new_len = GMAX( src_len) + dest->max_length;
+ size_t new_len = GMAX( src_len, MIN_CHUNK) + dest->max_length;
dest->string = dest->realloc_func( dest->string, new_len);
if (dest->string == NULL) {
@@ -163,7 +163,7 @@ int _gnutls_string_append_data( gnutls_string* dest, const void * data, size_t d
return tot_len;
} else {
- size_t new_len = GMAX( src_len) + dest->max_length;
+ size_t new_len = GMAX( data_size, MIN_CHUNK) + dest->max_length;
dest->string = dest->realloc_func( dest->string, new_len);
if (dest->string == NULL) {
diff --git a/lib/gnutls_x509.c b/lib/gnutls_x509.c
index 3309053cf7..3c9c79ad60 100644
--- a/lib/gnutls_x509.c
+++ b/lib/gnutls_x509.c
@@ -1095,7 +1095,7 @@ static int parse_der_cert_mem( gnutls_cert** cert_list, int* ncerts,
i = *ncerts + 1;
*cert_list =
- (gnutls_cert *) gnutls_realloc( *cert_list,
+ (gnutls_cert *) gnutls_realloc_fast( *cert_list,
i *
sizeof(gnutls_cert));
@@ -1168,7 +1168,7 @@ static int parse_pkcs7_cert_mem( gnutls_cert** cert_list, int* ncerts,
if (ret >= 0) {
*cert_list =
- (gnutls_cert *) gnutls_realloc( *cert_list,
+ (gnutls_cert *) gnutls_realloc_fast( *cert_list,
i * sizeof(gnutls_cert));
if ( *cert_list == NULL) {
@@ -1253,7 +1253,7 @@ static int parse_pem_cert_mem( gnutls_cert** cert_list, int* ncerts,
}
*cert_list =
- (gnutls_cert *) gnutls_realloc( *cert_list,
+ (gnutls_cert *) gnutls_realloc_fast( *cert_list,
i *
sizeof(gnutls_cert));
@@ -1305,13 +1305,13 @@ static int read_cert_mem(gnutls_certificate_credentials res, const char *cert, i
/* allocate space for the certificate to add
*/
- res->cert_list = gnutls_realloc( res->cert_list, (1+ res->ncerts)*sizeof(gnutls_cert*));
+ res->cert_list = gnutls_realloc_fast( res->cert_list, (1+ res->ncerts)*sizeof(gnutls_cert*));
if ( res->cert_list==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
- res->cert_list_length = gnutls_realloc( res->cert_list_length,
+ res->cert_list_length = gnutls_realloc_fast( res->cert_list_length,
(1+ res->ncerts)*sizeof(int));
if (res->cert_list_length==NULL) {
gnutls_assert();
@@ -1406,7 +1406,7 @@ static int read_key_mem(gnutls_certificate_credentials res, const char *key, int
/* allocate space for the pkey list
*/
- res->pkey = gnutls_realloc( res->pkey, (res->ncerts+1)*sizeof(gnutls_private_key));
+ res->pkey = gnutls_realloc_fast( res->pkey, (res->ncerts+1)*sizeof(gnutls_private_key));
if (res->pkey==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
diff --git a/lib/x509_xml.c b/lib/x509_xml.c
index 2a051f49dd..2fef8c728e 100644
--- a/lib/x509_xml.c
+++ b/lib/x509_xml.c
@@ -558,6 +558,7 @@ _gnutls_asn1_get_structure_xml(ASN1_TYPE structure, const char *name,
APPEND( "\n\0", 2);
*res = _gnutls_string2datum( &str);
+ res->size -= 1; /* null is not included in size */
return 0;
}
diff --git a/libextra/gnutls_openpgp.c b/libextra/gnutls_openpgp.c
index 84c769ab3e..fecfefc6cc 100644
--- a/libextra/gnutls_openpgp.c
+++ b/libextra/gnutls_openpgp.c
@@ -43,7 +43,7 @@
#define OPENPGP_NAME_SIZE GNUTLS_X509_CN_SIZE
-#define APPEND_DATUM(x, y, z) _gnutls_datum_append_m( x, y, z, realloc)
+#define APPEND_DATUM(x, y, z) _gnutls_datum_append_m( x, y, z, gnutls_realloc)
#define RC(x) ((x) < 0)
typedef struct {
@@ -603,14 +603,14 @@ gnutls_certificate_set_openpgp_key_mem( gnutls_certificate_credentials res,
goto leave;
/* fixme: too much duplicated code from (set_openpgp_key_file) */
- res->cert_list = gnutls_realloc(res->cert_list,
+ res->cert_list = gnutls_realloc_fast(res->cert_list,
(1+res->ncerts)*sizeof(gnutls_cert*));
if (res->cert_list == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
- res->cert_list_length = gnutls_realloc(res->cert_list_length,
+ res->cert_list_length = gnutls_realloc_fast(res->cert_list_length,
(1+res->ncerts)*sizeof(int));
if (res->cert_list_length == NULL) {
gnutls_assert();
@@ -643,7 +643,7 @@ gnutls_certificate_set_openpgp_key_mem( gnutls_certificate_credentials res,
}
res->ncerts++;
- res->pkey = gnutls_realloc(res->pkey,
+ res->pkey = gnutls_realloc_fast(res->pkey,
(res->ncerts)*sizeof(gnutls_private_key));
if (res->pkey == NULL) {
gnutls_assert();
@@ -710,14 +710,14 @@ gnutls_certificate_set_openpgp_key_file(gnutls_certificate_credentials res,
/*cdk_iobuf_close( inp );*/
}
- res->cert_list = gnutls_realloc(res->cert_list,
+ res->cert_list = gnutls_realloc_fast(res->cert_list,
(1+res->ncerts)*sizeof(gnutls_cert*));
if (res->cert_list == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
- res->cert_list_length = gnutls_realloc(res->cert_list_length,
+ res->cert_list_length = gnutls_realloc_fast(res->cert_list_length,
(1+res->ncerts)*sizeof(int));
if (res->cert_list_length == NULL) {
gnutls_assert();
@@ -782,7 +782,7 @@ gnutls_certificate_set_openpgp_key_file(gnutls_certificate_credentials res,
iobuf_to_datum( inp, &raw );
cdk_iobuf_close( inp );
- res->pkey = gnutls_realloc(res->pkey,
+ res->pkey = gnutls_realloc_fast(res->pkey,
(res->ncerts+1)*sizeof(gnutls_private_key));
if (res->pkey == NULL) {
gnutls_assert();
@@ -1902,6 +1902,7 @@ gnutls_openpgp_key_to_xml( const gnutls_datum *cert,
if ( rc < 0 ) return rc;
*xmlkey = _gnutls_string2datum( &string_xml_key);
+ xmlkey->size--;
return rc;
}
diff --git a/libextra/gnutls_srp.c b/libextra/gnutls_srp.c
index b05558eda9..b6d3439635 100644
--- a/libextra/gnutls_srp.c
+++ b/libextra/gnutls_srp.c
@@ -435,21 +435,21 @@ int i;
return GNUTLS_E_FILE_ERROR;
}
- res->password_file = gnutls_realloc( res->password_file,
+ res->password_file = gnutls_realloc_fast( res->password_file,
sizeof(char*)*(res->password_files+1));
if (res->password_file==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
- res->password_conf_file = gnutls_realloc( res->password_conf_file,
+ res->password_conf_file = gnutls_realloc_fast( res->password_conf_file,
sizeof(char*)*(res->password_files+1));
if (res->password_conf_file==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
- i = res->password_files++;
+ i = res->password_files;
res->password_file[i] = gnutls_strdup( password_file);
if (res->password_file[i]==NULL) {
@@ -461,9 +461,12 @@ int i;
if (res->password_conf_file[i]==NULL) {
gnutls_assert();
gnutls_free(res->password_file[i]);
+ res->password_file[i] = NULL;
return GNUTLS_E_MEMORY_ERROR;
}
+ res->password_files++;
+
return 0;
}