sim_imd: Resolve coverity issues.

This commit is contained in:
Howard M. Harte 2022-02-19 13:25:50 -08:00
parent ba01a5793c
commit 10c7e50f1c

View file

@ -90,12 +90,12 @@ static t_stat commentParse(DISK_INFO *myDisk, uint8 comment[], uint32 buffLen)
/* rewind to the beginning of the file. */ /* rewind to the beginning of the file. */
rewind(myDisk->file); rewind(myDisk->file);
cData = fgetc(myDisk->file); cData = (uint8)fgetc(myDisk->file);
while ((!feof(myDisk->file)) && (cData != 0x1a)) { while ((!feof(myDisk->file)) && (cData != 0x1a)) {
if ((comment != NULL) && (commentLen < buffLen)) { if ((comment != NULL) && (commentLen < buffLen)) {
comment[commentLen++] = cData; comment[commentLen++] = cData;
} }
cData = fgetc(myDisk->file); cData = (uint8)fgetc(myDisk->file);
} }
if (comment != NULL) { if (comment != NULL) {
if (commentLen == buffLen) 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"); */ /* sim_debug(myDisk->debugmask, myDisk->device, "Uncompressed Data\n"); */
if (sectorMap[i]-start_sect < MAX_SPT) { if (sectorMap[i]-start_sect < MAX_SPT) {
myDisk->track[imd.cyl][imd.head].sectorOffsetMap[sectorMap[i]-start_sect] = ftell(myDisk->file); 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 { else {
sim_printf("SIM_IMD: ERROR: Illegal sector offset %d\n", sectorMap[i]-start_sect); 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->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. */ myDisk->flags |= FD_FLAG_WRITELOCK; /* Write-protect the disk if any sectors are compressed. */
if (1) { if (1) {
uint8 cdata = fgetc(myDisk->file); uint8 cdata = (uint8)fgetc(myDisk->file);
sim_debug(myDisk->debugmask, myDisk->device, "Compressed Data = 0x%02x", cdata); 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) { if(sim_fsize(fileref) != 0) {
sim_printf("SIM_IMD: Disk image already has data, do you want to overwrite it? "); 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')) { if((answer != 'y') && (answer != 'Y')) {
return (SCPE_OPENERR); return (SCPE_OPENERR);
@ -380,7 +380,8 @@ t_stat diskCreate(FILE *fileref, const char *ctlr_comment)
rewind(fileref); rewind(fileref);
/* Erase the contents of the IMD file in case we are overwriting an existing image. */ /* 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"); sim_printf("SIM_IMD: Error overwriting disk image.\n");
return(SCPE_OPENERR); 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_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) { switch(sectRecordType) {
case SECT_RECORD_UNAVAILABLE: /* Data could not be read from the original media */ case SECT_RECORD_UNAVAILABLE: /* Data could not be read from the original media */
*flags |= IMD_DISK_IO_ERROR_GENERAL; *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_ERR: /* Normal Data with read error */
case SECT_RECORD_NORM_DAM_ERR: /* Normal Data with deleted address mark with read error */ case SECT_RECORD_NORM_DAM_ERR: /* Normal Data with deleted address mark with read error */
*flags |= IMD_DISK_IO_ERROR_CRC; *flags |= IMD_DISK_IO_ERROR_CRC;
/* fall through */
case SECT_RECORD_NORM: /* Normal Data */ case SECT_RECORD_NORM: /* Normal Data */
case SECT_RECORD_NORM_DAM: /* Normal Data with deleted address mark */ 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_COMP_ERR: /* Compressed Normal Data */
case SECT_RECORD_NORM_DAM_COMP_ERR: /* Compressed Normal Data with deleted address mark */ case SECT_RECORD_NORM_DAM_COMP_ERR: /* Compressed Normal Data with deleted address mark */
*flags |= IMD_DISK_IO_ERROR_CRC; *flags |= IMD_DISK_IO_ERROR_CRC;
/* fall through */
case SECT_RECORD_NORM_COMP: /* Compressed Normal Data */ case SECT_RECORD_NORM_COMP: /* Compressed Normal Data */
case SECT_RECORD_NORM_DAM_COMP: /* Compressed Normal Data with deleted address mark */ case SECT_RECORD_NORM_DAM_COMP: /* Compressed Normal Data with deleted address mark */
/* sim_debug(myDisk->debugmask, myDisk->device, "Compressed Data\n"); */ /* 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]; 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) { if (*flags & IMD_DISK_IO_ERROR_GENERAL) {
sectRecordType = SECT_RECORD_UNAVAILABLE; sectRecordType = SECT_RECORD_UNAVAILABLE;
@ -668,6 +671,7 @@ t_stat trackWrite(DISK_INFO *myDisk,
FILE *fileref; FILE *fileref;
IMD_HEADER track_header = { 0 }; IMD_HEADER track_header = { 0 };
uint8 *sectorData; uint8 *sectorData;
t_addr comment;
unsigned long i; unsigned long i;
unsigned long dataLen; unsigned long dataLen;
uint8 sectsize = 0; uint8 sectsize = 0;
@ -697,7 +701,8 @@ t_stat trackWrite(DISK_INFO *myDisk,
commentParse(myDisk, NULL, 0); commentParse(myDisk, NULL, 0);
/* Truncate the IMD file after the comment field. */ /* 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"); sim_printf("Disk truncation failed.\n");
*flags |= IMD_DISK_IO_ERROR_GENERAL; *flags |= IMD_DISK_IO_ERROR_GENERAL;
return(SCPE_IOERR); return(SCPE_IOERR);
@ -730,7 +735,7 @@ t_stat trackWrite(DISK_INFO *myDisk,
track_header.sectsize = sectsize; track_header.sectsize = sectsize;
/* Forward to end of the file, write track header and sector map. */ /* 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(&track_header, 1, sizeof(IMD_HEADER), fileref);
sim_fwrite(sectorMap, 1, numSectors, fileref); sim_fwrite(sectorMap, 1, numSectors, fileref);
@ -767,7 +772,7 @@ t_stat assignDiskType(UNIT *uptr) {
char header[4]; char header[4];
t_offset pos = sim_ftell(uptr->fileref); 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) if (fgets(header, 4, uptr->fileref) == NULL)
uptr->u3 = IMAGE_TYPE_DSK; uptr->u3 = IMAGE_TYPE_DSK;
else if (strncmp(header, "IMD", 3) == 0) else if (strncmp(header, "IMD", 3) == 0)
@ -779,6 +784,6 @@ t_stat assignDiskType(UNIT *uptr) {
} }
else else
uptr->u3 = IMAGE_TYPE_DSK; uptr->u3 = IMAGE_TYPE_DSK;
sim_fseeko(uptr->fileref, pos, SEEK_SET); (void)sim_fseeko(uptr->fileref, pos, SEEK_SET);
return result; return result;
} }