]> git.ipfire.org Git - thirdparty/qemu.git/commitdiff
hw/core/loader: improve error handling in image loading functions
authorVishal Chourasia <vishalc@linux.ibm.com>
Fri, 24 Oct 2025 13:05:57 +0000 (18:35 +0530)
committerPhilippe Mathieu-Daudé <philmd@linaro.org>
Tue, 28 Oct 2025 07:19:18 +0000 (08:19 +0100)
Add error checking for lseek() failure and provide better error
messages when image loading fails, including filenames and addresses.

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Aditya Gupta <adityag@linux.ibm.com>
Signed-off-by: Vishal Chourasia <vishalc@linux.ibm.com>
Message-ID: <20251024130556.1942835-8-vishalc@linux.ibm.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
hw/core/loader.c

index 7aca4989ef07223f62e625f950cc6daf6007fc32..d7c11c18f11785f62c224045cb493b9e89d181c6 100644 (file)
@@ -79,6 +79,10 @@ int64_t get_image_size(const char *filename, Error **errp)
     if (fd < 0)
         return -1;
     size = lseek(fd, 0, SEEK_END);
+    if (size < 0) {
+        error_setg_errno(errp, errno, "lseek failure: %s", filename);
+        return -1;
+    }
     close(fd);
     return size;
 }
@@ -132,11 +136,20 @@ ssize_t load_image_targphys_as(const char *filename,
     ssize_t size;
 
     size = get_image_size(filename, errp);
-    if (size < 0 || size > max_sz) {
+    if (size < 0) {
+        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;
         }
     }