diff mbox series

[v3,17/18] mips: fix potential fopen(NULL,...)

Message ID 20180104160523.22995-18-marcandre.lureau@redhat.com
State New
Headers show
Series Various build-sys and sanitizer related fixes | expand

Commit Message

Marc-André Lureau Jan. 4, 2018, 4:05 p.m. UTC
Spotted thanks to ASAN.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 hw/nvram/ds1225y.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Philippe Mathieu-Daudé Jan. 4, 2018, 4:27 p.m. UTC | #1
Hi Marc-André,

On 01/04/2018 01:05 PM, Marc-André Lureau wrote:
> Spotted thanks to ASAN.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  hw/nvram/ds1225y.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/nvram/ds1225y.c b/hw/nvram/ds1225y.c
> index 57d5ab2154..ad7345f288 100644
> --- a/hw/nvram/ds1225y.c
> +++ b/hw/nvram/ds1225y.c
> @@ -80,7 +80,7 @@ static int nvram_post_load(void *opaque, int version_id)
>      }
>  

More diffstats, but this let the code simpler imho:

   if (s->filename) {

>      /* Write back nvram contents */
> -    s->file = fopen(s->filename, "wb");
> +    s->file = s->filename ? fopen(s->filename, "wb") : NULL;
>      if (s->file) {
>          /* Write back contents, as 'wb' mode cleaned the file */
>          if (fwrite(s->contents, s->chip_size, 1, s->file) != 1) {

       ...

   }

> @@ -126,7 +126,7 @@ static int nvram_sysbus_initfn(SysBusDevice *dev)
>      sysbus_init_mmio(dev, &s->iomem);
>  
>      /* Read current file */
> -    file = fopen(s->filename, "rb");
> +    file = s->filename ? fopen(s->filename, "rb") : NULL;
>      if (file) {
>          /* Read nvram contents */
>          if (fread(s->contents, s->chip_size, 1, file) != 1) {
> 

ditto.
diff mbox series

Patch

diff --git a/hw/nvram/ds1225y.c b/hw/nvram/ds1225y.c
index 57d5ab2154..ad7345f288 100644
--- a/hw/nvram/ds1225y.c
+++ b/hw/nvram/ds1225y.c
@@ -80,7 +80,7 @@  static int nvram_post_load(void *opaque, int version_id)
     }
 
     /* Write back nvram contents */
-    s->file = fopen(s->filename, "wb");
+    s->file = s->filename ? fopen(s->filename, "wb") : NULL;
     if (s->file) {
         /* Write back contents, as 'wb' mode cleaned the file */
         if (fwrite(s->contents, s->chip_size, 1, s->file) != 1) {
@@ -126,7 +126,7 @@  static int nvram_sysbus_initfn(SysBusDevice *dev)
     sysbus_init_mmio(dev, &s->iomem);
 
     /* Read current file */
-    file = fopen(s->filename, "rb");
+    file = s->filename ? fopen(s->filename, "rb") : NULL;
     if (file) {
         /* Read nvram contents */
         if (fread(s->contents, s->chip_size, 1, file) != 1) {