From: Vishal Chourasia <vishalc@linux.ibm.com>
To: adityag@linux.ibm.com, harshpb@linux.ibm.com,
	milesg@linux.ibm.com, npiggin@gmail.com,
	peter.maydell@linaro.org, alistair23@gmail.com,
	balaton@eik.bme.hu, qemu-devel@nongnu.org, qemu-ppc@nongnu.org,
	berrange@redhat.com
Cc: "Vishal Chourasia" <vishalc@linux.ibm.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: [Patch v10 4/6] core/loader: add check for zero size in load_image_targphys_as
Date: Fri, 24 Oct 2025 18:35:59 +0530	[thread overview]
Message-ID: <20251024130556.1942835-10-vishalc@linux.ibm.com> (raw)
In-Reply-To: <20251024130556.1942835-2-vishalc@linux.ibm.com>
Currently load_image_targphys_as() returns -1 on file open failure or
when max size is exceeded. Add an explicit check for zero-sized files
to catch this error early, since some callers check for size <= 0.
Also, remove the redundant size > 0 check later in the function.
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Signed-off-by: Vishal Chourasia <vishalc@linux.ibm.com>
---
 hw/core/loader.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/hw/core/loader.c b/hw/core/loader.c
index d7c11c18f1..590c5b02aa 100644
--- a/hw/core/loader.c
+++ b/hw/core/loader.c
@@ -140,18 +140,21 @@ ssize_t load_image_targphys_as(const char *filename,
         return -1;
     }
 
+    if (size == 0) {
+        error_setg(errp, "empty file: %s", filename);
+        return -1;
+    }
+
     if (size > max_sz) {
         error_setg(errp, "%s exceeds maximum image size (%s)",
                    filename, size_to_str(max_sz));
         return -1;
     }
 
-    if (size > 0) {
-        if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
-            error_setg(errp, "could not load '%s' at %" HWADDR_PRIx,
-                       filename, addr);
-            return -1;
-        }
+    if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
+        error_setg(errp, "could not load '%s' at %" HWADDR_PRIx,
+                   filename, addr);
+        return -1;
     }
     return size;
 }
-- 
2.51.0
next prev parent reply	other threads:[~2025-10-24 13:08 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-24 13:05 [Patch v10 0/6] core/loader: capture Error from load_image_targphys Vishal Chourasia
2025-10-24 13:05 ` [Patch v10 1/6] core/loader: Use qemu_open() instead of open() in get_image_size() Vishal Chourasia
2025-10-24 13:05 ` [Patch v10 2/6] core/loader: capture Error from load_image_targphys Vishal Chourasia
2025-10-24 13:05 ` [Patch v10 3/6] core/loader: improve error handling in image loading functions Vishal Chourasia
2025-10-24 13:05 ` Vishal Chourasia [this message]
2025-10-24 13:06 ` [Patch v10 5/6] core: Pass errp to load_image_targphys_as() Vishal Chourasia
2025-10-24 13:06 ` [Patch v10 6/6] ppc: Pass error_fatal to load_image_targphys() Vishal Chourasia
2025-10-25  5:19 ` [Patch v10 0/6] core/loader: capture Error from load_image_targphys Harsh Prateek Bora
2025-10-27 15:39   ` Vishal Chourasia
2025-10-27 19:10     ` Philippe Mathieu-Daudé
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox
  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):
  git send-email \
    --in-reply-to=20251024130556.1942835-10-vishalc@linux.ibm.com \
    --to=vishalc@linux.ibm.com \
    --cc=adityag@linux.ibm.com \
    --cc=alistair23@gmail.com \
    --cc=balaton@eik.bme.hu \
    --cc=berrange@redhat.com \
    --cc=harshpb@linux.ibm.com \
    --cc=milesg@linux.ibm.com \
    --cc=npiggin@gmail.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    /path/to/YOUR_REPLY
  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
  Be sure your reply has a Subject: header at the top and a blank line
  before the message body.
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).