Skip to content

Commit 86bda64

Browse files
committed
Replace all malloc+sprintf with asprintf
1 parent eee30c5 commit 86bda64

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

extract-xiso.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -805,11 +805,10 @@ int main( int argc, char **argv ) {
805805
exiso_log( "%s is already optimized, skipping...\n", argv[ i ] );
806806
continue;
807807
}
808-
809-
if ( ! err && ( buf = (char *) malloc( strlen( argv[ i ] ) + 5 ) ) == nil ) mem_err(); // + 5 magic number is for ".old\0"
808+
810809
if ( ! err ) {
811-
sprintf( buf, "%s.old", argv[ i ] );
812-
if ( stat( buf, &sb ) != -1 ) misc_err( "%s already exists, cannot rewrite %s\n", buf, argv[ i ], 0 );
810+
if (asprintf(&buf, "%s.old", argv[i]) == -1) mem_err();
811+
if ( ! err && stat( buf, &sb ) != -1 ) misc_err( "%s already exists, cannot rewrite %s\n", buf, argv[ i ], 0 );
813812
if ( ! err && rename( argv[ i ], buf ) == -1 ) misc_err( "cannot rename %s to %s\n", argv[ i ], buf, 0 );
814813

815814
if ( err ) { err = 0; free( buf ); continue; }
@@ -1151,10 +1150,8 @@ int decode_xiso( char *in_xiso, char *in_path, modes in_mode, char **out_iso_pat
11511150
if ( in_path[ path_len - 1 ] != PATH_CHAR ) ++add_slash;
11521151
}
11531152

1154-
if ( ( buf = (char *) malloc( path_len + add_slash + strlen( iso_name ) + 2 ) ) == nil ) mem_err();
1155-
1156-
if ( ! err ) {
1157-
sprintf( buf, "%s%s%s%c", in_path ? in_path : "", add_slash && ( ! in_path ) ? PATH_CHAR_STR : "", in_mode != k_list && ( ! in_path ) ? iso_name : "", PATH_CHAR );
1153+
if (!err) {
1154+
if (asprintf(&buf, "%s%s%s%c", in_path ? in_path : "", add_slash && (!in_path) ? PATH_CHAR_STR : "", in_mode != k_list && (!in_path) ? iso_name : "", PATH_CHAR) == -1) mem_err()
11581155

11591156
if (!err && lseek(xiso, (xoff_t)root_dir_sect * XISO_SECTOR_SIZE + s_xbox_disc_lseek, SEEK_SET) == -1) seek_err();
11601157

@@ -1166,7 +1163,7 @@ int decode_xiso( char *in_xiso, char *in_path, modes in_mode, char **out_iso_pat
11661163
if (!err) err = traverse_xiso(xiso, (xoff_t)root_dir_sect * XISO_SECTOR_SIZE + s_xbox_disc_lseek, 0, buf, in_mode, nil);
11671164
}
11681165

1169-
free( buf );
1166+
if(buf) free(buf);
11701167
}
11711168
}
11721169

0 commit comments

Comments
 (0)