Skip to contentSkip to conversation

Added NULL check for qemu_find_file()

PATCH In review [email protected] 3 participants 7 messages Mar 12, 2016 – Mar 16, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Added NULL check for qemu_find_file()

From: Rutuja Shah <[email protected]>

This patch adds NULL check for return value from qemu_find_file(), where it is missing. It avoids unnecessary function calls with NULL parameter which in turn return -1. Especially, incase of load_uimage(), two functions are called which return -1 on passing NULL filename.

---
 hw/ppc/e500.c    | 17 +++++++++++++----
 hw/sparc/leon3.c |  6 +++++-
 2 files changed, 18 insertions(+), 5 deletions(-)
hw/ppc/e500.c +13 -4
index 09154fa..006adf1 100644
@@ -1016,15 +1016,24 @@ void ppce500_init(MachineState *machine, PPCE500Params *params)
     }
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
 
-    bios_size = load_elf(filename, NULL, NULL, &bios_entry, &loadaddr, NULL,
-                         1, PPC_ELF_MACHINE, 0, 0);
+    if (filename) {
+        bios_size = load_elf(filename, NULL, NULL, &bios_entry, &loadaddr, NULL,
+                             1, PPC_ELF_MACHINE, 0, 0);
+    } else {
+        bios_size = -1;
+    }
+
     if (bios_size < 0) {
         /*
          * Hrm. No ELF image? Try a uImage, maybe someone is giving us an
          * ePAPR compliant kernel
          */
-        kernel_size = load_uimage(filename, &bios_entry, &loadaddr, NULL,
-                                  NULL, NULL);
+        if (filename) {
+            kernel_size = load_uimage(filename, &bios_entry, &loadaddr, NULL,
+                                      NULL, NULL);
+        } else {
+            kernel_size = -1;
+        }
         if (kernel_size < 0) {
             fprintf(stderr, "qemu: could not load firmware '%s'\n", filename);
             exit(1);
hw/sparc/leon3.c +5 -1
index c579f5b..b4e9334 100644
@@ -168,7 +168,11 @@ static void leon3_generic_hw_init(MachineState *machine)
     }
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
 
-    bios_size = get_image_size(filename);
+    if (filename) {
+        bios_size = get_image_size(filename);
+    } else {
+        bios_size = -1;
+    }
 
     if (bios_size > prom_size) {
         fprintf(stderr, "qemu: could not load prom '%s': file too big\n",

1.9.1

Regards
Rutuja Shah

#1 lore raw
Quoted 3 lines · On Sun, Mar 13, 2016 at 02:06:34AM +0530, [email protected] wrote:

From: Rutuja Shah <[email protected]>

This patch adds NULL check for return value from qemu_find_file(), where it is missing. It avoids unnecessary function calls with NULL parameter which in turn return -1. Especially, incase of load_uimage(), two functions are called which return -1 on passing NULL filename.

What is the benefit of adding the NULL checks? The only difference I see is that the error message from load_elf() is silenced.

Avoiding unnecessary function calls isn't worthwhile if all callers now duplicate the if (filename) ... else size = -1 code.

Please wrap the commit description (the body of the commit message) to 72 characters or at most 80 characters.

Please include your Signed-off-by line here:
http://qemu-project.org/Contribute/SubmitAPatch#Patch_emails_must_include_a_Signed-off-by:_line

Quoted 64 lines
---
 hw/ppc/e500.c    | 17 +++++++++++++----
 hw/sparc/leon3.c |  6 +++++-
 2 files changed, 18 insertions(+), 5 deletions(-)
hw/ppc/e500.c +13 -4
index 09154fa..006adf1 100644
@@ -1016,15 +1016,24 @@ void ppce500_init(MachineState *machine, PPCE500Params *params)
     }
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
 
-    bios_size = load_elf(filename, NULL, NULL, &bios_entry, &loadaddr, NULL,
-                         1, PPC_ELF_MACHINE, 0, 0);
+    if (filename) {
+        bios_size = load_elf(filename, NULL, NULL, &bios_entry, &loadaddr, NULL,
+                             1, PPC_ELF_MACHINE, 0, 0);
+    } else {
+        bios_size = -1;
+    }
+
     if (bios_size < 0) {
         /*
          * Hrm. No ELF image? Try a uImage, maybe someone is giving us an
          * ePAPR compliant kernel
          */
-        kernel_size = load_uimage(filename, &bios_entry, &loadaddr, NULL,
-                                  NULL, NULL);
+        if (filename) {
+            kernel_size = load_uimage(filename, &bios_entry, &loadaddr, NULL,
+                                      NULL, NULL);
+        } else {
+            kernel_size = -1;
+        }
         if (kernel_size < 0) {
             fprintf(stderr, "qemu: could not load firmware '%s'\n", filename);
             exit(1);
hw/sparc/leon3.c +5 -1
index c579f5b..b4e9334 100644
@@ -168,7 +168,11 @@ static void leon3_generic_hw_init(MachineState *machine)
     }
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
 
-    bios_size = get_image_size(filename);
+    if (filename) {
+        bios_size = get_image_size(filename);
+    } else {
+        bios_size = -1;
+    }
 
     if (bios_size > prom_size) {
         fprintf(stderr, "qemu: could not load prom '%s': file too big\n",

1.9.1

Regards
Rutuja Shah

#2 lore raw

Regards
Rutuja Shah

Quoted 7 lines · Stefan Hajnoczi
Quoted 3 lines · On Sun, Mar 13, 2016 at 02:06:34AM +0530, [email protected] wrote:

From: Rutuja Shah <[email protected]>

This patch adds NULL check for return value from qemu_find_file(), where it is missing. It avoids unnecessary function calls with NULL parameter which in turn return -1. Especially, incase of load_uimage(), two functions are called which return -1 on passing NULL filename.

What is the benefit of adding the NULL checks? The only difference I see is that the error message from load_elf() is silenced.

Yes, in case of load_elf, error message is silenced. The immediate call to load_uimage()
is conditional to error from load_elf(). load_uimage() in turn calls load_uboot_image() which
then calls open() with NULL filename and returns with -1. In my opinion, this can be avoided.

Quoted 3 lines

Avoiding unnecessary function calls isn't worthwhile if all callers now duplicate the if (filename) ... else size = -1 code.

As far as I have seen the code, all callers have this check in place already, except the patched files.

Quoted 6 lines

Please wrap the commit description (the body of the commit message) to 72 characters or at most 80 characters.

Please include your Signed-off-by line here:
http://qemu-project.org/Contribute/SubmitAPatch#Patch_emails_must_include_a_Signed-off-by:_line

I will take care of this henceforth.

Quoted 65 lines
Quoted 65 lines
---
 hw/ppc/e500.c    | 17 +++++++++++++----
 hw/sparc/leon3.c |  6 +++++-
 2 files changed, 18 insertions(+), 5 deletions(-)
hw/ppc/e500.c +13 -4
index 09154fa..006adf1 100644
@@ -1016,15 +1016,24 @@ void ppce500_init(MachineState *machine, PPCE500Params *params)
     }
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
 
-    bios_size = load_elf(filename, NULL, NULL, &bios_entry, &loadaddr, NULL,
-                         1, PPC_ELF_MACHINE, 0, 0);
+    if (filename) {
+        bios_size = load_elf(filename, NULL, NULL, &bios_entry, &loadaddr, NULL,
+                             1, PPC_ELF_MACHINE, 0, 0);
+    } else {
+        bios_size = -1;
+    }
+
     if (bios_size < 0) {
         /*
          * Hrm. No ELF image? Try a uImage, maybe someone is giving us an
          * ePAPR compliant kernel
          */
-        kernel_size = load_uimage(filename, &bios_entry, &loadaddr, NULL,
-                                  NULL, NULL);
+        if (filename) {
+            kernel_size = load_uimage(filename, &bios_entry, &loadaddr, NULL,
+                                      NULL, NULL);
+        } else {
+            kernel_size = -1;
+        }
         if (kernel_size < 0) {
             fprintf(stderr, "qemu: could not load firmware '%s'\n", filename);
             exit(1);
hw/sparc/leon3.c +5 -1
index c579f5b..b4e9334 100644
@@ -168,7 +168,11 @@ static void leon3_generic_hw_init(MachineState *machine)
     }
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
 
-    bios_size = get_image_size(filename);
+    if (filename) {
+        bios_size = get_image_size(filename);
+    } else {
+        bios_size = -1;
+    }
 
     if (bios_size > prom_size) {
         fprintf(stderr, "qemu: could not load prom '%s': file too big\n",

1.9.1

Regards
Rutuja Shah

#3 lore raw
Quoted 9 lines · rutuja shah
Quoted 2 lines

What is the benefit of adding the NULL checks? The only difference I see is that the error message from load_elf() is silenced.

Yes, in case of load_elf, error message is silenced. The immediate call to load_uimage()
is conditional to error from load_elf(). load_uimage() in turn calls load_uboot_image() which
then calls open() with NULL filename and returns with -1. In my opinion, this can be avoided.

Quoted 1 line
Quoted 4 lines
Quoted 2 lines

Avoiding unnecessary function calls isn't worthwhile if all callers now duplicate the if (filename) ... else size = -1 code.

As far as I have seen the code, all callers have this check in place already, except the patched files.

Does this mean the main purpose of the patch is to make these load_elf()/load_uimage() callers consistent with other callers?

The commit description should say "why" rather than "what" the patch is doing.

In it's current state I'm not sure what the benefit of this change is. I also wonder whether the load_elf()/load_uimage() function prototype should be adjusted to avoid the boilerplate if statement in all callers.

Stefan

#4 lore raw

Regards
Rutuja Shah

Quoted 10 lines · Stefan Hajnoczi
Quoted 9 lines · rutuja shah
Quoted 2 lines

What is the benefit of adding the NULL checks? The only difference I see is that the error message from load_elf() is silenced.

Yes, in case of load_elf, error message is silenced. The immediate call to load_uimage()
is conditional to error from load_elf(). load_uimage() in turn calls load_uboot_image() which
then calls open() with NULL filename and returns with -1. In my opinion, this can be avoided.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Avoiding unnecessary function calls isn't worthwhile if all callers now duplicate the if (filename) ... else size = -1 code.

As far as I have seen the code, all callers have this check in place already, except the patched files.

Does this mean the main purpose of the patch is to make these load_elf()/load_uimage() callers consistent with other callers?

This is a task listed on http://qemu-project.org/BiteSizedTasks
(presently, I am unaware of the intention it is put up here). I propose this change so that callers are consistent. Also it adds to the performance, if such a case is encountered for reasons mentioned earlier.

Quoted 3 lines

The commit description should say "why" rather than "what" the patch is doing.

Ok.

Quoted 4 lines

In it's current state I'm not sure what the benefit of this change is. I also wonder whether the load_elf()/load_uimage() function prototype should be adjusted to avoid the boilerplate if statement in all callers.

Changing the prototype would result in a cleaner code, for all the functions which are using the file name received from qemu_find_file().

Quoted 2 lines

Stefan

#5 lore raw
Quoted 16 lines · rutuja shah
Quoted 15 lines · Stefan Hajnoczi
Quoted 13 lines · rutuja shah
Quoted 2 lines

What is the benefit of adding the NULL checks? The only difference I see is that the error message from load_elf() is silenced.

Yes, in case of load_elf, error message is silenced. The immediate call to load_uimage()
is conditional to error from load_elf(). load_uimage() in turn calls load_uboot_image() which
then calls open() with NULL filename and returns with -1. In my opinion, this can be avoided.

Quoted 3 lines

Avoiding unnecessary function calls isn't worthwhile if all callers now duplicate the if (filename) ... else size = -1 code.

As far as I have seen the code, all callers have this check in place already, except the patched files.

Quoted 7 lines
Quoted 2 lines

Does this mean the main purpose of the patch is to make these load_elf()/load_uimage() callers consistent with other callers?

This is a task listed on http://qemu-project.org/BiteSizedTasks
(presently, I am unaware of the intention it is put up here). I propose this change so that callers are consistent. Also it adds to the performance, if such a case is encountered for reasons mentioned earlier.

The task description on the wiki is terse. I'm not sure the intention was to make the change you have posted. I suspect the issue is related to unexpected behavior due to callers dereferencing a NULL filename pointer or assuming size > 0.

Maybe one of the CCed maintainers remembers or has an opinion. If there is no additional feedback from anyone then I'd hold back on this patch because it perturbs the code without a strong reason to do so.

Stefan

#6 lore raw
In reply to [email protected]
Quoted 3 lines · On 03/12/2016 01:36 PM, [email protected] wrote:

From: Rutuja Shah <[email protected]>

This patch adds NULL check for return value from qemu_find_file(), where it is missing. It avoids unnecessary function calls with NULL parameter which in turn return -1. Especially, incase of load_uimage(), two functions are called which return -1 on passing NULL

Please wrap your commit messages at 70 or so columns (since 'git log' will display your text with indentation, and many people still prefer 80-column terminal windows).

s/incase/in case/

Quoted 8 lines
---
 hw/ppc/e500.c    | 17 +++++++++++++----
 hw/sparc/leon3.c |  6 +++++-
 2 files changed, 18 insertions(+), 5 deletions(-)

This S-o-b is in the wrong place; it needs to appear before the --- separator to be included in the git log after a maintainer does 'git am' on your patch.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org