From a16e7f24f074e77a701399b5faaa1bc5975055eb Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Wed, 9 Jul 2014 14:58:11 +0200 Subject: [PATCH] Proper initialization and checks for rare cases --- library/net.c | 4 ++++ programs/aes/crypt_and_hash.c | 6 +++++- programs/pkey/key_app_writer.c | 3 +++ programs/ssl/ssl_client1.c | 2 +- programs/ssl/ssl_fork_server.c | 2 +- programs/ssl/ssl_mail_client.c | 1 + programs/ssl/ssl_server2.c | 1 + 7 files changed, 16 insertions(+), 3 deletions(-) diff --git a/library/net.c b/library/net.c index 85f0a0ebd..cf4a77886 100644 --- a/library/net.c +++ b/library/net.c @@ -113,6 +113,8 @@ int net_connect( int *fd, const char *host, int port ) struct sockaddr_in server_addr; struct hostent *server_host; + memset( &server_addr, 0, sizeof( server_addr ) ); + #if defined(_WIN32) || defined(_WIN32_WCE) WSADATA wsaData; @@ -158,6 +160,8 @@ int net_bind( int *fd, const char *bind_ip, int port ) int n, c[4]; struct sockaddr_in server_addr; + memset( &server_addr, 0, sizeof( server_addr ) ); + #if defined(_WIN32) || defined(_WIN32_WCE) WSADATA wsaData; diff --git a/programs/aes/crypt_and_hash.c b/programs/aes/crypt_and_hash.c index fc86337b4..95a316058 100644 --- a/programs/aes/crypt_and_hash.c +++ b/programs/aes/crypt_and_hash.c @@ -502,7 +502,11 @@ int main( int argc, char *argv[] ) /* * Write the final block of data */ - cipher_finish( &cipher_ctx, output, &olen ); + if( cipher_finish( &cipher_ctx, output, &olen ) != 0 ) + { + fprintf( stderr, "cipher_finish() returned error\n" ); + goto exit; + } if( fwrite( output, 1, olen, fout ) != olen ) { diff --git a/programs/pkey/key_app_writer.c b/programs/pkey/key_app_writer.c index d8465ca75..00553194d 100644 --- a/programs/pkey/key_app_writer.c +++ b/programs/pkey/key_app_writer.c @@ -97,6 +97,9 @@ void write_public_key( rsa_context *rsa, const char *output_file ) c = base_buf; f = fopen( output_file, "w" ); + if( f == NULL ) + return; + fprintf(f, "-----BEGIN PUBLIC KEY-----\n"); while (olen) { diff --git a/programs/ssl/ssl_client1.c b/programs/ssl/ssl_client1.c index d629bd899..040532663 100644 --- a/programs/ssl/ssl_client1.c +++ b/programs/ssl/ssl_client1.c @@ -72,7 +72,7 @@ int main( int argc, char *argv[] ) #else int main( int argc, char *argv[] ) { - int ret, len, server_fd; + int ret, len, server_fd = -1; unsigned char buf[1024]; const char *pers = "ssl_client1"; diff --git a/programs/ssl/ssl_fork_server.c b/programs/ssl/ssl_fork_server.c index 5ea79b353..47dedb49d 100644 --- a/programs/ssl/ssl_fork_server.c +++ b/programs/ssl/ssl_fork_server.c @@ -97,7 +97,7 @@ int main( int argc, char *argv[] ) { int ret, len, cnt = 0, pid; int listen_fd; - int client_fd; + int client_fd = -1; unsigned char buf[1024]; const char *pers = "ssl_fork_server"; diff --git a/programs/ssl/ssl_mail_client.c b/programs/ssl/ssl_mail_client.c index 20ded2a01..31e22fc88 100644 --- a/programs/ssl/ssl_mail_client.c +++ b/programs/ssl/ssl_mail_client.c @@ -373,6 +373,7 @@ int main( int argc, char *argv[] ) memset( &cacert, 0, sizeof( x509_cert ) ); memset( &clicert, 0, sizeof( x509_cert ) ); memset( &rsa, 0, sizeof( rsa_context ) ); + memset( &ssl, 0, sizeof( ssl_context ) ); if( argc == 0 ) { diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index 5be6d6ca7..3cc3f8ab7 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -259,6 +259,7 @@ int main( int argc, char *argv[] ) memset( &cacert, 0, sizeof( x509_cert ) ); memset( &srvcert, 0, sizeof( x509_cert ) ); memset( &rsa, 0, sizeof( rsa_context ) ); + memset( &ssl, 0, sizeof( ssl_context ) ); #if defined(POLARSSL_SSL_CACHE_C) ssl_cache_init( &cache ); #endif