Skip to contentSkip to conversation

fw_cfg: check return value of fread()

PATCH In review Pavel Borzenkov 2 participants 2 messages Oct 18, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fw_cfg: check return value of fread()

Spotted by Clang Analyzer

---
 hw/fw_cfg.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
hw/fw_cfg.c +7 -0
index 8df265c..1125e7d 100644
@@ -113,6 +113,13 @@ static FILE *probe_splashfile(char *filename, int *file_sizep, int *file_typep)
     if (file_type == BMP_FILE) {
         fseek(fp, 28, SEEK_SET);
         fop_ret = fread(buf, 1, 2, fp);
+        if (fop_ret != 2) {
+            error_report("Could not read bpp value from '%s': %s",
+                         filename, strerror(errno));
+            fclose(fp);
+            fp = NULL;
+            return fp;
+        }
         bmp_bpp = (buf[0] + (buf[1] << 8)) & 0xffff;
         if (bmp_bpp != 24) {
             error_report("only 24bpp bmp file is supported.");
Signature · 1 line
1.7.7
#1 lore raw
Quoted 15 lines · Pavel Borzenkov

Spotted by Clang Analyzer

---
 hw/fw_cfg.c |    7 +++++++

 1 files changed, 7 insertions(+), 0 deletions(-)
hw/fw_cfg.c +0 -0
index 8df265c..1125e7d 100644
@@ -113,6 +113,13 @@ static FILE *probe_splashfile(char *filename, int *file_sizep, int *file_typep)
     if (file_type == BMP_FILE) {
         fseek(fp, 28, SEEK_SET);
         fop_ret = fread(buf, 1, 2, fp);

hw/fw_cfg.c static FILE *probe_splashfile(char *filename, int *file_sizep, int *file_typep) · line 125

Quoted 10 lines
hw/fw_cfg.c +7 -0
+        if (fop_ret != 2) {
+            error_report("Could not read bpp value from '%s': %s",
+                         filename, strerror(errno));
+            fclose(fp);
+            fp = NULL;
+            return fp;
+        }
         bmp_bpp = (buf[0] + (buf[1] << 8)) & 0xffff;
         if (bmp_bpp != 24) {
             error_report("only 24bpp bmp file is supported.");

Yuck, this code again. We should just replace it with g_file_get_contents() and looking at the resulting buffer. That would be 10% of the code and much less bug-ridden.

FWIW, strictly speaking there isn't a need to check the result of the fread() because if we don't read the data then buf[] will still have the BMP_FILE signature in it and the != 24 check will fail.

Not checking the return code from that fseek(), on the other hand...

-- PMM