From 10c7e50f1c1f504ae0a37eb1350d31ec509bb67f Mon Sep 17 00:00:00 2001 From: "Howard M. Harte" Date: Sat, 19 Feb 2022 13:25:50 -0800 Subject: [PATCH] sim_imd: Resolve coverity issues. --- sim_imd.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/sim_imd.c b/sim_imd.c index 9d045c31..546b77de 100644 --- a/sim_imd.c +++ b/sim_imd.c @@ -90,12 +90,12 @@ static t_stat commentParse(DISK_INFO *myDisk, uint8 comment[], uint32 buffLen) /* rewind to the beginning of the file. */ rewind(myDisk->file); - cData = fgetc(myDisk->file); + cData = (uint8)fgetc(myDisk->file); while ((!feof(myDisk->file)) && (cData != 0x1a)) { if ((comment != NULL) && (commentLen < buffLen)) { comment[commentLen++] = cData; } - cData = fgetc(myDisk->file); + cData = (uint8)fgetc(myDisk->file); } if (comment != NULL) { if (commentLen == buffLen) @@ -251,7 +251,7 @@ static t_stat diskParse(DISK_INFO *myDisk, uint32 isVerbose) /* sim_debug(myDisk->debugmask, myDisk->device, "Uncompressed Data\n"); */ if (sectorMap[i]-start_sect < MAX_SPT) { myDisk->track[imd.cyl][imd.head].sectorOffsetMap[sectorMap[i]-start_sect] = ftell(myDisk->file); - sim_fseek(myDisk->file, sectorSize, SEEK_CUR); + (void)sim_fseek(myDisk->file, sectorSize, SEEK_CUR); } else { sim_printf("SIM_IMD: ERROR: Illegal sector offset %d\n", sectorMap[i]-start_sect); @@ -266,7 +266,7 @@ static t_stat diskParse(DISK_INFO *myDisk, uint32 isVerbose) myDisk->track[imd.cyl][imd.head].sectorOffsetMap[sectorMap[i]-start_sect] = ftell(myDisk->file); myDisk->flags |= FD_FLAG_WRITELOCK; /* Write-protect the disk if any sectors are compressed. */ if (1) { - uint8 cdata = fgetc(myDisk->file); + uint8 cdata = (uint8)fgetc(myDisk->file); sim_debug(myDisk->debugmask, myDisk->device, "Compressed Data = 0x%02x", cdata); } @@ -344,7 +344,7 @@ t_stat diskCreate(FILE *fileref, const char *ctlr_comment) if(sim_fsize(fileref) != 0) { sim_printf("SIM_IMD: Disk image already has data, do you want to overwrite it? "); - answer = getchar(); + answer = (uint8)getchar(); if((answer != 'y') && (answer != 'Y')) { return (SCPE_OPENERR); @@ -380,7 +380,8 @@ t_stat diskCreate(FILE *fileref, const char *ctlr_comment) rewind(fileref); /* Erase the contents of the IMD file in case we are overwriting an existing image. */ - if (sim_set_fsize(fileref, (t_addr)ftell (fileref)) == -1) { + if (sim_set_fsize(fileref, 0) == -1) { + free(comment); sim_printf("SIM_IMD: Error overwriting disk image.\n"); return(SCPE_OPENERR); } @@ -512,9 +513,9 @@ t_stat sectRead(DISK_INFO *myDisk, sim_debug(myDisk->debugmask, myDisk->device, "Reading C:%d/H:%d/S:%d, len=%d, offset=0x%08x\n", Cyl, Head, Sector, buflen, sectorFileOffset); - sim_fseek(myDisk->file, sectorFileOffset-1, SEEK_SET); + (void)sim_fseek(myDisk->file, sectorFileOffset-1, SEEK_SET); - sectRecordType = fgetc(myDisk->file); + sectRecordType = (uint8)fgetc(myDisk->file); switch(sectRecordType) { case SECT_RECORD_UNAVAILABLE: /* Data could not be read from the original media */ *flags |= IMD_DISK_IO_ERROR_GENERAL; @@ -522,6 +523,7 @@ t_stat sectRead(DISK_INFO *myDisk, case SECT_RECORD_NORM_ERR: /* Normal Data with read error */ case SECT_RECORD_NORM_DAM_ERR: /* Normal Data with deleted address mark with read error */ *flags |= IMD_DISK_IO_ERROR_CRC; + /* fall through */ case SECT_RECORD_NORM: /* Normal Data */ case SECT_RECORD_NORM_DAM: /* Normal Data with deleted address mark */ @@ -534,6 +536,7 @@ t_stat sectRead(DISK_INFO *myDisk, case SECT_RECORD_NORM_COMP_ERR: /* Compressed Normal Data */ case SECT_RECORD_NORM_DAM_COMP_ERR: /* Compressed Normal Data with deleted address mark */ *flags |= IMD_DISK_IO_ERROR_CRC; + /* fall through */ case SECT_RECORD_NORM_COMP: /* Compressed Normal Data */ case SECT_RECORD_NORM_DAM_COMP: /* Compressed Normal Data with deleted address mark */ /* sim_debug(myDisk->debugmask, myDisk->device, "Compressed Data\n"); */ @@ -611,7 +614,7 @@ t_stat sectWrite(DISK_INFO *myDisk, sectorFileOffset = myDisk->track[Cyl][Head].sectorOffsetMap[Sector-start_sect]; - sim_fseek(myDisk->file, sectorFileOffset-1, SEEK_SET); + (void)sim_fseek(myDisk->file, sectorFileOffset-1, SEEK_SET); if (*flags & IMD_DISK_IO_ERROR_GENERAL) { sectRecordType = SECT_RECORD_UNAVAILABLE; @@ -668,6 +671,7 @@ t_stat trackWrite(DISK_INFO *myDisk, FILE *fileref; IMD_HEADER track_header = { 0 }; uint8 *sectorData; + t_addr comment; unsigned long i; unsigned long dataLen; uint8 sectsize = 0; @@ -697,7 +701,8 @@ t_stat trackWrite(DISK_INFO *myDisk, commentParse(myDisk, NULL, 0); /* Truncate the IMD file after the comment field. */ - if (sim_set_fsize(fileref, (t_addr)ftell (fileref)) == -1) { + if (((comment = (t_addr)ftell (fileref)) == (t_addr)-1) || + (sim_set_fsize(fileref, comment) == -1)) { sim_printf("Disk truncation failed.\n"); *flags |= IMD_DISK_IO_ERROR_GENERAL; return(SCPE_IOERR); @@ -730,7 +735,7 @@ t_stat trackWrite(DISK_INFO *myDisk, track_header.sectsize = sectsize; /* Forward to end of the file, write track header and sector map. */ - sim_fseek(myDisk->file, 0, SEEK_END); + (void)sim_fseek(myDisk->file, 0, SEEK_END); sim_fwrite(&track_header, 1, sizeof(IMD_HEADER), fileref); sim_fwrite(sectorMap, 1, numSectors, fileref); @@ -767,7 +772,7 @@ t_stat assignDiskType(UNIT *uptr) { char header[4]; t_offset pos = sim_ftell(uptr->fileref); - sim_fseek(uptr->fileref, (t_addr)0, SEEK_SET); + rewind(uptr->fileref); if (fgets(header, 4, uptr->fileref) == NULL) uptr->u3 = IMAGE_TYPE_DSK; else if (strncmp(header, "IMD", 3) == 0) @@ -779,6 +784,6 @@ t_stat assignDiskType(UNIT *uptr) { } else uptr->u3 = IMAGE_TYPE_DSK; - sim_fseeko(uptr->fileref, pos, SEEK_SET); + (void)sim_fseeko(uptr->fileref, pos, SEEK_SET); return result; }