qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] fw_cfg: check return value of fread()
@ 2011-10-18 17:16 Pavel Borzenkov
  2011-10-18 17:30 ` Peter Maydell
  0 siblings, 1 reply; 2+ messages in thread
From: Pavel Borzenkov @ 2011-10-18 17:16 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-trivial

Spotted by Clang Analyzer

Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
---
 hw/fw_cfg.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
index 8df265c..1125e7d 100644
--- a/hw/fw_cfg.c
+++ b/hw/fw_cfg.c
@@ -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.");
-- 
1.7.7

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH] fw_cfg: check return value of fread()
  2011-10-18 17:16 [Qemu-devel] [PATCH] fw_cfg: check return value of fread() Pavel Borzenkov
@ 2011-10-18 17:30 ` Peter Maydell
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Maydell @ 2011-10-18 17:30 UTC (permalink / raw)
  To: Pavel Borzenkov; +Cc: qemu-trivial, qemu-devel

On 18 October 2011 18:16, Pavel Borzenkov <pavel.borzenkov@gmail.com> wrote:
> Spotted by Clang Analyzer
>
> Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
> ---
>  hw/fw_cfg.c |    7 +++++++
>  1 files changed, 7 insertions(+), 0 deletions(-)
>
> diff --git a/hw/fw_cfg.c b/hw/fw_cfg.c
> index 8df265c..1125e7d 100644
> --- a/hw/fw_cfg.c
> +++ b/hw/fw_cfg.c
> @@ -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.");

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-10-18 17:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-18 17:16 [Qemu-devel] [PATCH] fw_cfg: check return value of fread() Pavel Borzenkov
2011-10-18 17:30 ` Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).