Merge memory leak fix into branch 'mbedtls-1.3'
Merge of fix for memory leak in RSA-SSA signing - #372
diff --git a/library/asn1write.c b/library/asn1write.c
index 6a7c9d3..bb60830 100644
--- a/library/asn1write.c
+++ b/library/asn1write.c
@@ -342,19 +342,18 @@
}
else if( cur->val.len < val_len )
{
- // Enlarge existing value buffer if needed
- //
- polarssl_free( cur->val.p );
- cur->val.p = NULL;
-
- cur->val.len = val_len;
- cur->val.p = polarssl_malloc( val_len );
- if( cur->val.p == NULL )
- {
- polarssl_free( cur->oid.p );
- polarssl_free( cur );
+ /*
+ * Enlarge existing value buffer if needed
+ * Preserve old data until the allocation succeeded, to leave list in
+ * a consistent state in case allocation fails.
+ */
+ void *p = polarssl_malloc( val_len );
+ if( p == NULL )
return( NULL );
- }
+
+ polarssl_free( cur->val.p );
+ cur->val.p = p;
+ cur->val.len = val_len;
}
if( val != NULL )