diff --git a/src/disk/hdd_image.c b/src/disk/hdd_image.c index df473d7d9..db56d5b78 100644 --- a/src/disk/hdd_image.c +++ b/src/disk/hdd_image.c @@ -327,7 +327,7 @@ hdd_image_load(int id) hdd_images[id].type = HDD_IMAGE_HDX; } else if (is_vhd[0]) { fclose(hdd_images[id].file); - MVHDGeom geometry; + MVHDGeom geometry = { 0 }; geometry.cyl = hdd[id].tracks; geometry.heads = hdd[id].hpc; geometry.spt = hdd[id].spt; @@ -335,7 +335,7 @@ hdd_image_load(int id) hdd_images[id].last_sector = (full_size >> 9LL) - 1; if (hdd[id].vhd_blocksize || hdd[id].vhd_parent[0]) { - MVHDCreationOptions options; + MVHDCreationOptions options = { 0 }; retry_vhd: options.block_size_in_sectors = hdd[id].vhd_blocksize; options.path = fn; diff --git a/src/disk/minivhd/create.c b/src/disk/minivhd/create.c index d06382ef9..1fcfc8682 100644 --- a/src/disk/minivhd/create.c +++ b/src/disk/minivhd/create.c @@ -315,7 +315,7 @@ create_sparse_diff(const char* path, const char* par_path, uint64_t size_in_byte { uint8_t footer_buff[MVHD_FOOTER_SIZE] = {0}; uint8_t sparse_buff[MVHD_SPARSE_SIZE] = {0}; - uint8_t bat_sect[MVHD_SECTOR_SIZE]; + uint8_t bat_sect[MVHD_SECTOR_SIZE] = {0}; MVHDGeom par_geom = {0}; memset(bat_sect, 0xffffffff, sizeof bat_sect); MVHDMeta* vhdm = NULL; diff --git a/src/disk/minivhd/manage.c b/src/disk/minivhd/manage.c index 7ac3989e6..fdf3d8e79 100644 --- a/src/disk/minivhd/manage.c +++ b/src/disk/minivhd/manage.c @@ -92,7 +92,7 @@ read_footer(MVHDMeta* vhdm) static void read_sparse_header(MVHDMeta* vhdm) { - uint8_t buffer[MVHD_SPARSE_SIZE]; + uint8_t buffer[MVHD_SPARSE_SIZE] = { 0 }; mvhd_fseeko64(vhdm->f, vhdm->footer.data_offset, SEEK_SET); (void) !fread(buffer, sizeof buffer, 1, vhdm->f); @@ -438,17 +438,15 @@ mvhd_version_id(void) MVHDAPI int mvhd_file_is_vhd(FILE* f) { - uint8_t con_str[8]; + uint8_t con_str[8] = { 0 }; - if (f == NULL) { + if (f == NULL) return 0; - } mvhd_fseeko64(f, -MVHD_FOOTER_SIZE, SEEK_END); (void) !fread(con_str, sizeof con_str, 1, f); - if (mvhd_is_conectix_str(con_str)) { + if (mvhd_is_conectix_str(con_str)) return 1; - } return 0; } @@ -457,13 +455,12 @@ mvhd_file_is_vhd(FILE* f) MVHDAPI MVHDGeom mvhd_calculate_geometry(uint64_t size) { - MVHDGeom chs; + MVHDGeom chs = { 0 }; uint32_t ts = (uint32_t)(size / MVHD_SECTOR_SIZE); uint32_t spt, heads, cyl, cth; - if (ts > 65535 * 16 * 255) { + if (ts > 65535 * 16 * 255) ts = 65535 * 16 * 255; - } if (ts >= 65535 * 16 * 63) { spt = 255; @@ -473,9 +470,8 @@ mvhd_calculate_geometry(uint64_t size) spt = 17; cth = ts / spt; heads = (cth + 1023) / 1024; - if (heads < 4) { + if (heads < 4) heads = 4; - } if (cth >= (heads * 1024) || heads > 16) { spt = 31; heads = 16; @@ -500,7 +496,7 @@ mvhd_calculate_geometry(uint64_t size) MVHDAPI MVHDMeta* mvhd_open(const char* path, int readonly, int* err) { - MVHDError open_err; + MVHDError open_err = { 0 }; MVHDMeta *vhdm = calloc(sizeof *vhdm, 1); if (vhdm == NULL) { @@ -516,11 +512,10 @@ mvhd_open(const char* path, int readonly, int* err) //This is safe, as we've just checked for potential overflow above strcpy(vhdm->filename, path); - if (readonly) { + if (readonly) vhdm->f = mvhd_fopen((const char*)vhdm->filename, "rb", err); - } else { + else vhdm->f = mvhd_fopen((const char*)vhdm->filename, "rb+", err); - } if (vhdm->f == NULL) { /* note, mvhd_fopen sets err for us */ goto cleanup_vhdm; @@ -567,14 +562,12 @@ mvhd_open(const char* path, int readonly, int* err) vhdm->format_buffer.sector_count = 64; if (vhdm->footer.disk_type == MVHD_TYPE_DIFF) { char* par_path = get_diff_parent_path(vhdm, err); - if (par_path == NULL) { + if (par_path == NULL) goto cleanup_format_buff; - } uint32_t par_mod_ts = mvhd_file_mod_timestamp(par_path, err); - if (*err != 0) { + if (*err != 0) goto cleanup_format_buff; - } if (vhdm->sparse.par_timestamp != par_mod_ts) { /* The last-modified timestamp is to fragile to make this a fatal error. @@ -582,9 +575,8 @@ mvhd_open(const char* path, int readonly, int* err) *err = MVHD_ERR_TIMESTAMP; } vhdm->parent = mvhd_open(par_path, true, err); - if (vhdm->parent == NULL) { + if (vhdm->parent == NULL) goto cleanup_format_buff; - } if (memcmp(vhdm->sparse.par_uuid, vhdm->parent->footer.uuid, sizeof vhdm->sparse.par_uuid) != 0) { *err = MVHD_ERR_INVALID_PAR_UUID; @@ -629,9 +621,8 @@ mvhd_close(MVHDMeta* vhdm) if (vhdm == NULL) return; - if (vhdm->parent != NULL) { + if (vhdm->parent != NULL) mvhd_close(vhdm->parent); - } fclose(vhdm->f); @@ -655,7 +646,7 @@ mvhd_close(MVHDMeta* vhdm) MVHDAPI int mvhd_diff_update_par_timestamp(MVHDMeta* vhdm, int* err) { - uint8_t sparse_buff[1024]; + uint8_t sparse_buff[1024] = { 0 }; if (vhdm == NULL || err == NULL) { *err = MVHD_ERR_INVALID_PARAMS; diff --git a/src/disk/minivhd/minivhd_io.c b/src/disk/minivhd/minivhd_io.c index ff86a8337..2912cf5b2 100644 --- a/src/disk/minivhd/minivhd_io.c +++ b/src/disk/minivhd/minivhd_io.c @@ -46,7 +46,6 @@ #include "minivhd.h" #include "internal.h" - /* * The following bit array macros adapted from: * @@ -56,7 +55,6 @@ #define VHD_CLEARBIT(A,k) ( A[(k>>3)] &= ~(0x80 >> (k&7)) ) #define VHD_TESTBIT(A,k) ( A[(k>>3)] & (0x80 >> (k&7)) ) - /** * \brief Check that we will not be overflowing buffers * @@ -68,29 +66,26 @@ * \param [out] trunc_sectors The number of sectors truncated if transfer_sectors < num_sectors */ static inline void -check_sectors(uint32_t offset, int num_sectors, uint32_t total_sectors, int* transfer_sect, int* trunc_sect) +check_sectors(uint32_t offset, int num_sectors, uint32_t total_sectors, int *transfer_sect, int *trunc_sect) { *transfer_sect = num_sectors; *trunc_sect = 0; - if ((total_sectors - offset) < (uint32_t)*transfer_sect) { + if ((total_sectors - offset) < ((uint32_t) *transfer_sect)) { *transfer_sect = total_sectors - offset; *trunc_sect = num_sectors - *transfer_sect; } } - void -mvhd_write_empty_sectors(FILE* f, int sector_count) +mvhd_write_empty_sectors(FILE *f, int sector_count) { uint8_t zero_bytes[MVHD_SECTOR_SIZE] = {0}; - for (int i = 0; i < sector_count; i++) { + for (int i = 0; i < sector_count; i++) fwrite(zero_bytes, sizeof zero_bytes, 1, f); - } } - /** * \brief Read the sector bitmap for a block. * @@ -101,19 +96,17 @@ mvhd_write_empty_sectors(FILE* f, int sector_count) * \param [in] blk The block for which to read the sector bitmap from */ static void -read_sect_bitmap(MVHDMeta* vhdm, int blk) +read_sect_bitmap(MVHDMeta *vhdm, int blk) { if (vhdm->block_offset[blk] != MVHD_SPARSE_BLK) { mvhd_fseeko64(vhdm->f, (uint64_t)vhdm->block_offset[blk] * MVHD_SECTOR_SIZE, SEEK_SET); (void) !fread(vhdm->bitmap.curr_bitmap, vhdm->bitmap.sector_count * MVHD_SECTOR_SIZE, 1, vhdm->f); - } else { + } else memset(vhdm->bitmap.curr_bitmap, 0, vhdm->bitmap.sector_count * MVHD_SECTOR_SIZE); - } vhdm->bitmap.curr_block = blk; } - /** * \brief Write the current sector bitmap in memory to file * @@ -129,7 +122,6 @@ write_curr_sect_bitmap(MVHDMeta* vhdm) } } - /** * \brief Write block offset from memory into file * @@ -137,7 +129,7 @@ write_curr_sect_bitmap(MVHDMeta* vhdm) * \param [in] blk The block for which to write the offset for */ static void -write_bat_entry(MVHDMeta* vhdm, int blk) +write_bat_entry(MVHDMeta *vhdm, int blk) { uint64_t table_offset = vhdm->sparse.bat_offset + ((uint64_t)blk * sizeof *vhdm->block_offset); uint32_t offset = mvhd_to_be32(vhdm->block_offset[blk]); @@ -146,7 +138,6 @@ write_bat_entry(MVHDMeta* vhdm, int blk) fwrite(&offset, sizeof offset, 1, vhdm->f); } - /** * \brief Create an empty block in a sparse or differencing VHD image * @@ -162,9 +153,9 @@ write_bat_entry(MVHDMeta* vhdm, int blk) * \param [in] blk The block number to create */ static void -create_block(MVHDMeta* vhdm, int blk) +create_block(MVHDMeta *vhdm, int blk) { - uint8_t footer[MVHD_FOOTER_SIZE]; + uint8_t footer[MVHD_FOOTER_SIZE] = { 0 }; /* Seek to where the footer SHOULD be */ mvhd_fseeko64(vhdm->f, -MVHD_FOOTER_SIZE, SEEK_END); @@ -179,13 +170,12 @@ create_block(MVHDMeta* vhdm, int blk) } int64_t abs_offset = mvhd_ftello64(vhdm->f); - if (abs_offset % MVHD_SECTOR_SIZE != 0) { + if ((abs_offset % MVHD_SECTOR_SIZE) != 0) { /* Yikes! We're supposed to be on a sector boundary. Add some padding */ - int64_t padding_amount = (int64_t)MVHD_SECTOR_SIZE - (abs_offset % MVHD_SECTOR_SIZE); + int64_t padding_amount = ((int64_t) MVHD_SECTOR_SIZE) - (abs_offset % MVHD_SECTOR_SIZE); uint8_t zero_byte = 0; - for (int i = 0; i < padding_amount; i++) { + for (int i = 0; i < padding_amount; i++) fwrite(&zero_byte, sizeof zero_byte, 1, vhdm->f); - } abs_offset += padding_amount; } @@ -204,37 +194,39 @@ create_block(MVHDMeta* vhdm, int blk) write_bat_entry(vhdm, blk); } - int -mvhd_fixed_read(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* out_buff) { - int64_t addr; - int transfer_sectors, truncated_sectors; +mvhd_fixed_read(MVHDMeta *vhdm, uint32_t offset, int num_sectors, void *out_buff) { + int64_t addr = 0ULL; + int transfer_sectors = 0; + int truncated_sectors = 0; uint32_t total_sectors = (uint32_t)(vhdm->footer.curr_sz / MVHD_SECTOR_SIZE); check_sectors(offset, num_sectors, total_sectors, &transfer_sectors, &truncated_sectors); - addr = (int64_t)offset * MVHD_SECTOR_SIZE; + addr = ((int64_t) offset) * MVHD_SECTOR_SIZE; mvhd_fseeko64(vhdm->f, addr, SEEK_SET); - (void) !fread(out_buff, transfer_sectors*MVHD_SECTOR_SIZE, 1, vhdm->f); + (void) !fread(out_buff, transfer_sectors * MVHD_SECTOR_SIZE, 1, vhdm->f); return truncated_sectors; } - int -mvhd_sparse_read(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* out_buff) +mvhd_sparse_read(MVHDMeta *vhdm, uint32_t offset, int num_sectors, void *out_buff) { - int transfer_sectors, truncated_sectors; + int transfer_sectors = 0; + int truncated_sectors; uint32_t total_sectors = (uint32_t)(vhdm->footer.curr_sz / MVHD_SECTOR_SIZE); check_sectors(offset, num_sectors, total_sectors, &transfer_sectors, &truncated_sectors); uint8_t* buff = (uint8_t*)out_buff; - int64_t addr; - uint32_t s, ls; - int blk, prev_blk, sib; + int64_t addr = 0ULL; + uint32_t s = 0; + uint32_t ls = 0; + int blk = 0; + int prev_blk = -1; + int sib = 0; ls = offset + transfer_sectors; - prev_blk = -1; for (s = offset; s < ls; s++) { blk = s / vhdm->sect_per_block; @@ -245,14 +237,15 @@ mvhd_sparse_read(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* out_buf read_sect_bitmap(vhdm, blk); mvhd_fseeko64(vhdm->f, (uint64_t)sib * MVHD_SECTOR_SIZE, SEEK_CUR); } else { - addr = ((int64_t)vhdm->block_offset[blk] + vhdm->bitmap.sector_count + sib) * MVHD_SECTOR_SIZE; + addr = (((int64_t) vhdm->block_offset[blk]) + vhdm->bitmap.sector_count + sib) * + MVHD_SECTOR_SIZE; mvhd_fseeko64(vhdm->f, addr, SEEK_SET); } } - if (VHD_TESTBIT(vhdm->bitmap.curr_bitmap, sib)) { + if (VHD_TESTBIT(vhdm->bitmap.curr_bitmap, sib)) (void) !fread(buff, MVHD_SECTOR_SIZE, 1, vhdm->f); - } else { + else { memset(buff, 0, MVHD_SECTOR_SIZE); mvhd_fseeko64(vhdm->f, MVHD_SECTOR_SIZE, SEEK_CUR); } @@ -262,19 +255,21 @@ mvhd_sparse_read(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* out_buf return truncated_sectors; } - int -mvhd_diff_read(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* out_buff) +mvhd_diff_read(MVHDMeta *vhdm, uint32_t offset, int num_sectors, void *out_buff) { - int transfer_sectors, truncated_sectors; + int transfer_sectors = 0; + int truncated_sectors = 0; uint32_t total_sectors = (uint32_t)(vhdm->footer.curr_sz / MVHD_SECTOR_SIZE); check_sectors(offset, num_sectors, total_sectors, &transfer_sectors, &truncated_sectors); - uint8_t* buff = (uint8_t*)out_buff; - MVHDMeta* curr_vhdm = vhdm; - uint32_t s, ls; - int blk, sib; + uint8_t *buff = (uint8_t*)out_buff; + MVHDMeta *curr_vhdm = vhdm; + uint32_t s = 0; + uint32_t ls = 0; + int blk = 0; + int sib = 0; ls = offset + transfer_sectors; for (s = offset; s < ls; s++) { @@ -291,11 +286,11 @@ mvhd_diff_read(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* out_buff) /* We handle actual sector reading using the fixed or sparse functions, as a differencing VHD is also a sparse VHD */ - if (curr_vhdm->footer.disk_type == MVHD_TYPE_DIFF || curr_vhdm->footer.disk_type == MVHD_TYPE_DYNAMIC) { + if ((curr_vhdm->footer.disk_type == MVHD_TYPE_DIFF) || + (curr_vhdm->footer.disk_type == MVHD_TYPE_DYNAMIC)) mvhd_sparse_read(curr_vhdm, s, 1, buff); - } else { + else mvhd_fixed_read(curr_vhdm, s, 1, buff); - } curr_vhdm = vhdm; buff += MVHD_SECTOR_SIZE; @@ -304,38 +299,40 @@ mvhd_diff_read(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* out_buff) return truncated_sectors; } - int -mvhd_fixed_write(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* in_buff) +mvhd_fixed_write(MVHDMeta *vhdm, uint32_t offset, int num_sectors, void *in_buff) { - int64_t addr; - int transfer_sectors, truncated_sectors; + int64_t addr = 0ULL; + int transfer_sectors = 0; + int truncated_sectors = 0; uint32_t total_sectors = (uint32_t)(vhdm->footer.curr_sz / MVHD_SECTOR_SIZE); check_sectors(offset, num_sectors, total_sectors, &transfer_sectors, &truncated_sectors); addr = (int64_t)offset * MVHD_SECTOR_SIZE; mvhd_fseeko64(vhdm->f, addr, SEEK_SET); - fwrite(in_buff, transfer_sectors*MVHD_SECTOR_SIZE, 1, vhdm->f); + fwrite(in_buff, transfer_sectors * MVHD_SECTOR_SIZE, 1, vhdm->f); return truncated_sectors; } - int -mvhd_sparse_diff_write(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* in_buff) +mvhd_sparse_diff_write(MVHDMeta *vhdm, uint32_t offset, int num_sectors, void *in_buff) { - int transfer_sectors, truncated_sectors; + int transfer_sectors = 0; + int truncated_sectors = 0; uint32_t total_sectors = (uint32_t)(vhdm->footer.curr_sz / MVHD_SECTOR_SIZE); check_sectors(offset, num_sectors, total_sectors, &transfer_sectors, &truncated_sectors); - uint8_t* buff = (uint8_t*)in_buff; - int64_t addr; - uint32_t s, ls; - int blk, prev_blk, sib; + uint8_t* buff = (uint8_t *) in_buff; + int64_t addr = 0ULL; + uint32_t s = 0; + uint32_t ls = 0; + int blk = 0; + int prev_blk = -1; + int sib = 0; ls = offset + transfer_sectors; - prev_blk = -1; for (s = offset; s < ls; s++) { blk = s / vhdm->sect_per_block; @@ -357,7 +354,8 @@ mvhd_sparse_diff_write(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* i read_sect_bitmap(vhdm, blk); mvhd_fseeko64(vhdm->f, (uint64_t)sib * MVHD_SECTOR_SIZE, SEEK_CUR); } else { - addr = ((int64_t)vhdm->block_offset[blk] + vhdm->bitmap.sector_count + sib) * MVHD_SECTOR_SIZE; + addr = (((int64_t) vhdm->block_offset[blk]) + vhdm->bitmap.sector_count + sib) * + MVHD_SECTOR_SIZE; mvhd_fseeko64(vhdm->f, addr, SEEK_SET); } prev_blk = blk; @@ -374,14 +372,13 @@ mvhd_sparse_diff_write(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* i return truncated_sectors; } - int -mvhd_noop_write(MVHDMeta* vhdm, uint32_t offset, int num_sectors, void* in_buff) +mvhd_noop_write(MVHDMeta *vhdm, uint32_t offset, int num_sectors, void *in_buff) { - (void)vhdm; - (void)offset; - (void)num_sectors; - (void)in_buff; + (void) vhdm; + (void) offset; + (void) num_sectors; + (void) in_buff; return 0; } diff --git a/src/disk/minivhd/minivhd_util.c b/src/disk/minivhd/minivhd_util.c index dd3244322..4901e5841 100644 --- a/src/disk/minivhd/minivhd_util.c +++ b/src/disk/minivhd/minivhd_util.c @@ -462,7 +462,7 @@ mvhd_file_mod_timestamp(const char* path, int *err) { *err = 0; #ifdef _WIN32 - struct _stat file_stat; + struct _stat file_stat = { 0 }; size_t path_len = strlen(path); mvhd_utf16 new_path[260] = {0}; int new_path_len = (sizeof new_path) - 2; @@ -485,7 +485,7 @@ mvhd_file_mod_timestamp(const char* path, int *err) return 0; } #else - struct stat file_stat; + struct stat file_stat = { 0 }; int stat_res = stat(path, &file_stat); if (stat_res != 0) {