Patchwork [5/6] load_linux: report open kernel file & its size error

login
register
mail settings
Submitter liguang
Date Feb. 4, 2013, 2:27 a.m.
Message ID <1359944880-6039-6-git-send-email-lig.fnst@cn.fujitsu.com>
Download mbox | patch
Permalink /patch/217818/
State New
Headers show

Comments

liguang - Feb. 4, 2013, 2:27 a.m.
Signed-off-by: liguang <lig.fnst@cn.fujitsu.com>
---
 hw/pc.c |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)
Blue Swirl - Feb. 4, 2013, 6:20 p.m.
On Mon, Feb 4, 2013 at 2:27 AM, liguang <lig.fnst@cn.fujitsu.com> wrote:
> Signed-off-by: liguang <lig.fnst@cn.fujitsu.com>
> ---
>  hw/pc.c |   14 +++++++++++---
>  1 files changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/hw/pc.c b/hw/pc.c
> index 01d00f6..0ccd775 100644
> --- a/hw/pc.c
> +++ b/hw/pc.c
> @@ -652,12 +652,20 @@ static void load_linux(void *fw_cfg,
>      char *vmode;
>
>      /* Align to 16 bytes as a paranoia measure */
> -    cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
> +    cmdline_size = (strlen(kernel_cmdline)+16) & ~0xf;

Here 15 is 16 - 1, so 0xf seems out of place. I'd use QEMU_ALIGN_UP().

If you touch the line, please add spaces around '+'.

>
>      /* load the kernel header */
>      f = fopen(kernel_filename, "rb");
> -    if (!f || !(kernel_size = get_file_size(f)) ||
> -        fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
> +    if (!f) {
> +        fprintf(stderr, "can't open kernel image file\n");

The error message from strerror(errno) would be interesting.

> +        exit(1);
> +    }
> +    kernel_size = get_file_size(f);
> +    if (kernel_size <= 0) {
> +        fprintf(stderr, "can't get size of kernel image file\n");
> +        exit(1);
> +    }
> +    if (fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
>          MIN(ARRAY_SIZE(header), kernel_size)) {
>          fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
>                  kernel_filename, strerror(errno));
> --
> 1.7.2.5
>
>
liguang - Feb. 5, 2013, 12:50 a.m.
OK, will fix.
Thanks!

在 2013-02-04一的 18:20 +0000,Blue Swirl写道:
> On Mon, Feb 4, 2013 at 2:27 AM, liguang <lig.fnst@cn.fujitsu.com> wrote:
> > Signed-off-by: liguang <lig.fnst@cn.fujitsu.com>
> > ---
> >  hw/pc.c |   14 +++++++++++---
> >  1 files changed, 11 insertions(+), 3 deletions(-)
> >
> > diff --git a/hw/pc.c b/hw/pc.c
> > index 01d00f6..0ccd775 100644
> > --- a/hw/pc.c
> > +++ b/hw/pc.c
> > @@ -652,12 +652,20 @@ static void load_linux(void *fw_cfg,
> >      char *vmode;
> >
> >      /* Align to 16 bytes as a paranoia measure */
> > -    cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
> > +    cmdline_size = (strlen(kernel_cmdline)+16) & ~0xf;
> 
> Here 15 is 16 - 1, so 0xf seems out of place. I'd use QEMU_ALIGN_UP().
> 
> If you touch the line, please add spaces around '+'.
> 
> >
> >      /* load the kernel header */
> >      f = fopen(kernel_filename, "rb");
> > -    if (!f || !(kernel_size = get_file_size(f)) ||
> > -        fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
> > +    if (!f) {
> > +        fprintf(stderr, "can't open kernel image file\n");
> 
> The error message from strerror(errno) would be interesting.
> 
> > +        exit(1);
> > +    }
> > +    kernel_size = get_file_size(f);
> > +    if (kernel_size <= 0) {
> > +        fprintf(stderr, "can't get size of kernel image file\n");
> > +        exit(1);
> > +    }
> > +    if (fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
> >          MIN(ARRAY_SIZE(header), kernel_size)) {
> >          fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
> >                  kernel_filename, strerror(errno));
> > --
> > 1.7.2.5
> >
> >

Patch

diff --git a/hw/pc.c b/hw/pc.c
index 01d00f6..0ccd775 100644
--- a/hw/pc.c
+++ b/hw/pc.c
@@ -652,12 +652,20 @@  static void load_linux(void *fw_cfg,
     char *vmode;
 
     /* Align to 16 bytes as a paranoia measure */
-    cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
+    cmdline_size = (strlen(kernel_cmdline)+16) & ~0xf;
 
     /* load the kernel header */
     f = fopen(kernel_filename, "rb");
-    if (!f || !(kernel_size = get_file_size(f)) ||
-        fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
+    if (!f) {
+        fprintf(stderr, "can't open kernel image file\n");
+        exit(1);
+    }
+    kernel_size = get_file_size(f);
+    if (kernel_size <= 0) {
+        fprintf(stderr, "can't get size of kernel image file\n");
+        exit(1);
+    }
+    if (fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
         MIN(ARRAY_SIZE(header), kernel_size)) {
         fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
                 kernel_filename, strerror(errno));