From 9f851256f13ed2f1103464e35162bf58499db7ca Mon Sep 17 00:00:00 2001 From: rapperskull Date: Mon, 6 Mar 2023 09:46:50 +0100 Subject: [PATCH] Check before free --- extract-xiso.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/extract-xiso.c b/extract-xiso.c index 0f22e88..7c6142d 100644 --- a/extract-xiso.c +++ b/extract-xiso.c @@ -812,7 +812,7 @@ int main( int argc, char **argv ) { if ( ! err && stat( buf, &sb ) != -1 ) misc_err( "%s already exists, cannot rewrite %s", buf, argv[ i ] ); if ( ! err && rename( argv[ i ], buf ) == -1 ) misc_err( "cannot rename %s to %s", argv[ i ], buf ); - if ( err ) { err = 0; free( buf ); continue; } + if ( err ) { err = 0; if ( buf ) free( buf ); continue; } } if ( ! err ) err = decode_xiso( buf, path, k_rewrite, &new_iso_path ); if ( ! err && delete && unlink( buf ) == -1 ) log_err( __FILE__, __LINE__, "unable to delete %s", buf ); @@ -1022,8 +1022,11 @@ int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_av if ( ! err && ( buf = (char *) malloc( n = max( READWRITE_BUFFER_SIZE, XISO_HEADER_OFFSET ) ) ) == nil ) mem_err(); if ( ! err ) { if ( ( xiso = open( xiso_path, WRITEFLAGS, 0644 ) ) == -1 ) open_err( xiso_path ); - if ( out_iso_path ) *out_iso_path = xiso_path; - else free( xiso_path ); + if (out_iso_path) *out_iso_path = xiso_path; + else { + free(xiso_path); + xiso_path = nil; + } } if ( ! err ) { memset( buf, 0, n ); @@ -1087,7 +1090,7 @@ int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_av if ( xiso != -1 ) { close( xiso ); - if ( err ) unlink( xiso_path ); + if (err && xiso_path) unlink(xiso_path); } if ( root.filename ) free( root.filename ); @@ -1216,7 +1219,7 @@ int traverse_xiso(int in_xiso, xoff_t in_dir_start, uint16_t entry_offset, char* } // Read node - if (!err) if (!(node = calloc(1, sizeof(dir_node)))) mem_err(); + if (!err) if ((node = calloc(1, sizeof(dir_node))) == nil) mem_err(); if (!err && read(in_xiso, &r_offset, XISO_TABLE_OFFSET_SIZE) != XISO_TABLE_OFFSET_SIZE) read_err(); if (!err && read(in_xiso, &node->start_sector, XISO_SECTOR_OFFSET_SIZE) != XISO_SECTOR_OFFSET_SIZE) read_err(); if (!err && read(in_xiso, &node->file_size, XISO_FILESIZE_SIZE) != XISO_FILESIZE_SIZE) read_err(); @@ -1260,8 +1263,10 @@ int traverse_xiso(int in_xiso, xoff_t in_dir_start, uint16_t entry_offset, char* if (!err) err = process_node(in_xiso, node, in_path, in_mode, (in_mode == k_generate_avl) ? &avl->subdirectory : nil); // Free memory before recurring - if (node->filename) free(node->filename); - if (node) free(node); + if (node) { + if (node->filename) free(node->filename); + free(node); + } // Repeat on left node if (!err && l_offset) {