Skip to content

Commit

Permalink
memory leak fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfdelnero committed Nov 12, 2023
1 parent 95259cb commit 5d71e72
Showing 1 changed file with 56 additions and 49 deletions.
105 changes: 56 additions & 49 deletions libhxcfe/sources/loaders/copyqm_loader/copyqm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ int CopyQm_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,

/* Alloc memory for the image */
flatimg = malloc( image_size );
if ( ! flatimg )
if ( !flatimg )
{
imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"malloc error!");
hxc_fclose(f_img);
Expand All @@ -243,73 +243,78 @@ int CopyQm_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,
/* Start reading */
/* Note that it seems like each track starts a new block */

while ( curwritepos < image_size )
while ( curwritepos < image_size )
{
/* Read the length */
unsigned char lengthBuf[2];
res = hxc_fread( lengthBuf, 2, f_img );
if ( res <= 0 )
{
/* Read the length */
unsigned char lengthBuf[2];
res = hxc_fread( lengthBuf, 2, f_img );
if ( res <= 0 )
if ( feof( f_img ) )
{
if ( feof( f_img ) )
{
/* End of file - fill with f6 - do not update CRC for these */
memset( flatimg + curwritepos, 0xf6, image_size - curwritepos );
curwritepos += image_size - curwritepos;
}
else
/* End of file - fill with f6 - do not update CRC for these */
memset( flatimg + curwritepos, 0xf6, image_size - curwritepos );
curwritepos += image_size - curwritepos;
}
else
{
free(flatimg);
hxc_fclose(f_img);
return HXCFE_FILECORRUPTED;
}
}
else
{
int length = get_i16( lengthBuf, 0 );
if ( length < 0 )
{
/* Negative number - next byte is repeated (-length) times */
int c = fgetc( f_img );
if ( c == EOF )
{
free(flatimg);
hxc_fclose(f_img);
hxc_fclose(f_img);
return HXCFE_FILECORRUPTED;
}
/* Copy the byte into memory and update the offset */
memset( flatimg + curwritepos, c, -length );
curwritepos -= length;
/* Update CRC */
for( ; length != 0; ++length )
{
drv_qm_update_crc( &crc, (unsigned char)c );
}
}
else
{
int length = get_i16( lengthBuf, 0 );
if ( length < 0 )
if ( length != 0 )
{
/* Negative number - next byte is repeated (-length) times */
int c = fgetc( f_img );
if ( c == EOF ) return HXCFE_FILECORRUPTED;
/* Copy the byte into memory and update the offset */
memset( flatimg + curwritepos, c, -length );
curwritepos -= length;
/* Update CRC */
for( ; length != 0; ++length )
/* Positive number - length different characters */
res = hxc_fread( flatimg + curwritepos, length, f_img );
/* Update CRC (and write pos) */
while ( length-- )
{
drv_qm_update_crc( &crc, (unsigned char)c );
drv_qm_update_crc( &crc, flatimg[curwritepos++] );
}
}
else
{
if ( length != 0 )

if ( res <= 0 )
{
/* Positive number - length different characters */
res = hxc_fread( flatimg + curwritepos, length, f_img );
/* Update CRC (and write pos) */
while ( length-- )
{
drv_qm_update_crc( &crc, flatimg[curwritepos++] );
}

if ( res <= 0 )
{
free(flatimg);
hxc_fclose(f_img);
return HXCFE_FILECORRUPTED;
}
free(flatimg);
hxc_fclose(f_img);
return HXCFE_FILECORRUPTED;
}
}
}
}
}

/* Compare the CRCs */
/* The CRC is zero on old images so it cannot be checked then */
if ( crc32 ) {
if ( crc32!= crc ) {
imgldr_ctx->hxcfe->hxc_printf(MSG_WARNING,"Copyqm loader: Bad CRC ?");
}
/* Compare the CRCs */
/* The CRC is zero on old images so it cannot be checked then */
if ( crc32 ) {
if ( crc32!= crc ) {
imgldr_ctx->hxcfe->hxc_printf(MSG_WARNING,"Copyqm loader: Bad CRC ?");
}
}

hxc_fclose(f_img);

Expand All @@ -321,6 +326,8 @@ int CopyQm_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,

ret = raw_iso_loader(imgldr_ctx, floppydisk, 0, flatimg, image_size, &rawcfg);

free(flatimg);

return ret;
}

Expand Down

0 comments on commit 5d71e72

Please sign in to comment.