@@ -80,13 +80,15 @@ static FILE *probe_splashfile(char *filename, int *file_sizep, int *file_typep)
file_size = ftell(fp);
if (file_size < 2) {
error_report("file size is less than 2 bytes '%s'.", filename);
- fclose(fp);
- fp = NULL;
- return fp;
+ goto fail;
}
/* check magic ID */
fseek(fp, 0L, SEEK_SET);
fop_ret = fread(buf, 1, 2, fp);
+ if (fop_ret != 2) {
+ goto fail;
+ }
+
filehead_value = (buf[0] + (buf[1] << 8)) & 0xffff;
if (filehead_value == 0xd8ff) {
file_type = JPG_FILE;
@@ -98,9 +100,7 @@ static FILE *probe_splashfile(char *filename, int *file_sizep, int *file_typep)
if (file_type < 0) {
error_report("'%s' not jpg/bmp file,head:0x%x.",
filename, filehead_value);
- fclose(fp);
- fp = NULL;
- return fp;
+ goto fail;
}
/* check BMP bpp */
if (file_type == BMP_FILE) {
@@ -109,15 +109,17 @@ static FILE *probe_splashfile(char *filename, int *file_sizep, int *file_typep)
bmp_bpp = (buf[0] + (buf[1] << 8)) & 0xffff;
if (bmp_bpp != 24) {
error_report("only 24bpp bmp file is supported.");
- fclose(fp);
- fp = NULL;
- return fp;
+ goto fail;
}
}
/* return values */
*file_sizep = file_size;
*file_typep = file_type;
return fp;
+
+fail:
+ fclose(fp);
+ return NULL;
}
static void fw_cfg_bootsplash(FWCfgState *s)
@@ -169,8 +171,8 @@ static void fw_cfg_bootsplash(FWCfgState *s)
}
/* probing the file */
fp = probe_splashfile(filename, &file_size, &file_type);
+ qemu_free(filename);
if (fp == NULL) {
- qemu_free(filename);
return;
}
/* loading file data */
@@ -182,6 +184,10 @@ static void fw_cfg_bootsplash(FWCfgState *s)
fseek(fp, 0L, SEEK_SET);
fop_ret = fread(boot_splash_filedata, 1, file_size, fp);
fclose(fp);
+ if (fop_ret != file_size) {
+ return;
+ }
+
/* insert data */
if (file_type == JPG_FILE) {
fw_cfg_add_file(s, "bootsplash.jpg",
@@ -190,7 +196,6 @@ static void fw_cfg_bootsplash(FWCfgState *s)
fw_cfg_add_file(s, "bootsplash.bmp",
boot_splash_filedata, boot_splash_filedata_size);
}
- qemu_free(filename);
}
}
master branch doesnt build due to 3d3b8303c6f83b9b245bc774af530a6403cc4ce6 hw/fw_cfg.c: In function ‘probe_splashfile’: hw/fw_cfg.c:66:9: error: variable ‘fop_ret’ set but not used [-Werror=unused-but-set-variable] hw/fw_cfg.c: In function ‘fw_cfg_bootsplash’: hw/fw_cfg.c:130:9: error: variable ‘fop_ret’ set but not used [-Werror=unused-but-set-variable] This is a basically untested fix, compile tested only. Cheers --- commit fb4515b04705414ab9ea9d5deb4728b41525761e Author: Edgar E. Iglesias <edgar.iglesias@gmail.com> Date: Thu Aug 4 01:17:16 2011 +0200 fw_cfg: Simplify error paths and avoid warnings Signed-off-by: Edgar E. Iglesias <edgar.iglesias@gmail.com>