Commit 5c078e17 authored by Manuel Pégourié-Gonnard's avatar Manuel Pégourié-Gonnard
Browse files

Fix memory leak on bad arguments in ssl_server2

Not a big deal, but was annoying in coverity results.
parent 6fdc4cae
......@@ -386,7 +386,7 @@ static int my_send( void *ctx, const unsigned char *buf, size_t len )
dst = p; \
while( *p != ',' ) \
if( ++p > end ) \
return( NULL ); \
goto error; \
*p++ = '\0';
#if defined(POLARSSL_SNI)
......@@ -399,12 +399,29 @@ struct _sni_entry {
sni_entry *next;
};
void sni_free( sni_entry *head )
{
sni_entry *cur = head, *next;
while( cur != NULL )
{
x509_crt_free( cur->cert );
polarssl_free( cur->cert );
pk_free( cur->key );
polarssl_free( cur->key );
next = cur->next;
polarssl_free( cur );
cur = next;
}
}
/*
* Parse a string of triplets name1,crt1,key1[,name2,crt2,key2[,...]]
* into a usable sni_entry list.
*
* Modifies the input string! This is not production quality!
* (leaks memory if parsing fails, no error reporting, ...)
*/
sni_entry *sni_parse( char *sni_string )
{
......@@ -420,13 +437,21 @@ sni_entry *sni_parse( char *sni_string )
while( p <= end )
{
if( ( new = polarssl_malloc( sizeof( sni_entry ) ) ) == NULL )
{
sni_free( cur );
return( NULL );
}
memset( new, 0, sizeof( sni_entry ) );
if( ( new->cert = polarssl_malloc( sizeof( x509_crt ) ) ) == NULL ||
( new->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
{
polarssl_free( new->cert );
polarssl_free( new );
sni_free( cur );
return( NULL );
}
x509_crt_init( new->cert );
pk_init( new->key );
......@@ -437,31 +462,20 @@ sni_entry *sni_parse( char *sni_string )
if( x509_crt_parse_file( new->cert, crt_file ) != 0 ||
pk_parse_keyfile( new->key, key_file, "" ) != 0 )
return( NULL );
{
goto error;
}
new->next = cur;
cur = new;
}
return( cur );
}
void sni_free( sni_entry *head )
{
sni_entry *cur = head, *next;
while( cur != NULL )
{
x509_crt_free( cur->cert );
polarssl_free( cur->cert );
pk_free( cur->key );
polarssl_free( cur->key );
next = cur->next;
polarssl_free( cur );
cur = next;
}
error:
sni_free( new );
sni_free( cur );
return( NULL );
}
/*
......@@ -538,12 +552,26 @@ struct _psk_entry
psk_entry *next;
};
/*
* Free a list of psk_entry's
*/
void psk_free( psk_entry *head )
{
psk_entry *next;
while( head != NULL )
{
next = head->next;
polarssl_free( head );
head = next;
}
}
/*
* Parse a string of pairs name1,key1[,name2,key2[,...]]
* into a usable psk_entry list.
*
* Modifies the input string! This is not production quality!
* (leaks memory if parsing fails, no error reporting, ...)
*/
psk_entry *psk_parse( char *psk_string )
{
......@@ -567,28 +595,18 @@ psk_entry *psk_parse( char *psk_string )
GET_ITEM( key_hex );
if( unhexify( new->key, key_hex, &new->key_len ) != 0 )
return( NULL );
goto error;
new->next = cur;
cur = new;
}
return( cur );
}
/*
* Free a list of psk_entry's
*/
void psk_free( psk_entry *head )
{
psk_entry *next;
while( head != NULL )
{
next = head->next;
polarssl_free( head );
head = next;
}
error:
psk_free( new );
psk_free( cur );
return( 0 );
}
/*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment