From 42c3ccf36e86972bff3a74c1e74c11311e6a7af0 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Mon, 19 Aug 2013 14:29:31 +0200 Subject: [PATCH] Fixed potential negative value misinterpretation in load_file() --- library/x509parse.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/library/x509parse.c b/library/x509parse.c index 535b18075..0a3b6fea2 100644 --- a/library/x509parse.c +++ b/library/x509parse.c @@ -1917,14 +1917,21 @@ int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen ) static int load_file( const char *path, unsigned char **buf, size_t *n ) { FILE *f; + long size; if( ( f = fopen( path, "rb" ) ) == NULL ) return( POLARSSL_ERR_X509_FILE_IO_ERROR ); fseek( f, 0, SEEK_END ); - *n = (size_t) ftell( f ); + if( ( size = ftell( f ) ) == -1 ) + { + fclose( f ); + return( POLARSSL_ERR_X509_FILE_IO_ERROR ); + } fseek( f, 0, SEEK_SET ); + *n = (size_t) size; + if( *n + 1 == 0 || ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL ) {