commit 7a9d2d10b39753fed7f1fd1b80cf66cf7be18a82 parent ca62ca8a590238ece80108e0aa280a1ebcd389ab Author: Michael Savage <mikejsavage@gmail.com> Date: Sat Nov 5 17:11:13 +0200 Slightly more robust file_get_contents Diffstat:
intrinsics.h | | | 8 | ++++---- |
diff --git a/intrinsics.h b/intrinsics.h @@ -132,17 +132,17 @@ To checked_cast( const From & from ) { // TODO: this sucks inline u8 * file_get_contents( const char * path, size_t * out_len = NULL ) { FILE * file = fopen( path, "rb" ); - assert( file != NULL ); + ASSERT( file != NULL ); fseek( file, 0, SEEK_END ); - size_t len = ftell( file ); + size_t len = checked_cast< size_t >( ftell( file ) ); + ASSERT( len < SIZE_MAX ); fseek( file, 0, SEEK_SET ); - // TODO: oh-no-verflow u8 * contents = ( u8 * ) malloc( len + 1 ); size_t bytes_read = fread( contents, 1, len, file ); contents[ len ] = '\0'; - assert( bytes_read == len ); + ASSERT( bytes_read == len ); if( out_len ) *out_len = len;