diff mbox

[for-2.3,3/6] vmdk: Clean up descriptor file reading

Message ID 1417505957-2666-4-git-send-email-famz@redhat.com
State New
Headers show

Commit Message

Fam Zheng Dec. 2, 2014, 7:39 a.m. UTC
Zeroing a buffer that will be filled right after is not necessary, and
allocating a power of two + 1 is naughty.

Suggested-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block/vmdk.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Don Koch Dec. 2, 2014, 5:10 p.m. UTC | #1
On Tue, 2 Dec 2014 15:39:14 +0800
Fam Zheng <famz@redhat.com> wrote:

> Zeroing a buffer that will be filled right after is not necessary, and
> allocating a power of two + 1 is naughty.
> 
> Suggested-by: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  block/vmdk.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/block/vmdk.c b/block/vmdk.c
> index 28d22db..0c5769c 100644
> --- a/block/vmdk.c
> +++ b/block/vmdk.c
> @@ -558,14 +558,15 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
>      }
>  
>      size = MIN(size, 1 << 20);  /* avoid unbounded allocation */
> -    buf = g_malloc0(size + 1);
> +    buf = g_malloc(size);

For a file that is less than 1<<20 bytes, won't this throw away the last byte?
Maybe better is:
    size = MIN(size, (1 << 20) - 1);
    buf = g_malloc(size + 1);
Leave the bdrv_pread as it was and...

>  
> -    ret = bdrv_pread(file, desc_offset, buf, size);
> +    ret = bdrv_pread(file, desc_offset, buf, size - 1);
>      if (ret < 0) {
>          error_setg_errno(errp, -ret, "Could not read from file");
>          g_free(buf);
>          return NULL;
>      }
> +    buf[ret - 1] = 0;

...zero the last byte changes to:
    buf[ret] = 0;

>  
>      return buf;
>  }
> -- 
> 1.9.3

-d
Fam Zheng Dec. 3, 2014, 1:38 a.m. UTC | #2
On Tue, 12/02 12:10, Don Koch wrote:
> On Tue, 2 Dec 2014 15:39:14 +0800
> Fam Zheng <famz@redhat.com> wrote:
> 
> > Zeroing a buffer that will be filled right after is not necessary, and
> > allocating a power of two + 1 is naughty.
> > 
> > Suggested-by: Markus Armbruster <armbru@redhat.com>
> > Signed-off-by: Fam Zheng <famz@redhat.com>
> > ---
> >  block/vmdk.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> > 
> > diff --git a/block/vmdk.c b/block/vmdk.c
> > index 28d22db..0c5769c 100644
> > --- a/block/vmdk.c
> > +++ b/block/vmdk.c
> > @@ -558,14 +558,15 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
> >      }
> >  
> >      size = MIN(size, 1 << 20);  /* avoid unbounded allocation */
> > -    buf = g_malloc0(size + 1);
> > +    buf = g_malloc(size);
> 
> For a file that is less than 1<<20 bytes, won't this throw away the last byte?
> Maybe better is:
>     size = MIN(size, (1 << 20) - 1);
>     buf = g_malloc(size + 1);
> Leave the bdrv_pread as it was and...
> 
> >  
> > -    ret = bdrv_pread(file, desc_offset, buf, size);
> > +    ret = bdrv_pread(file, desc_offset, buf, size - 1);
> >      if (ret < 0) {
> >          error_setg_errno(errp, -ret, "Could not read from file");
> >          g_free(buf);
> >          return NULL;
> >      }
> > +    buf[ret - 1] = 0;
> 
> ...zero the last byte changes to:
>     buf[ret] = 0;

Yes. I'll respin this one. Thanks.

Fam

> 
> >  
> >      return buf;
> >  }
> > -- 
> > 1.9.3
> 
> -d
diff mbox

Patch

diff --git a/block/vmdk.c b/block/vmdk.c
index 28d22db..0c5769c 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -558,14 +558,15 @@  static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     }
 
     size = MIN(size, 1 << 20);  /* avoid unbounded allocation */
-    buf = g_malloc0(size + 1);
+    buf = g_malloc(size);
 
-    ret = bdrv_pread(file, desc_offset, buf, size);
+    ret = bdrv_pread(file, desc_offset, buf, size - 1);
     if (ret < 0) {
         error_setg_errno(errp, -ret, "Could not read from file");
         g_free(buf);
         return NULL;
     }
+    buf[ret - 1] = 0;
 
     return buf;
 }