From: Vishal Chourasia Date: Fri, 24 Oct 2025 13:05:59 +0000 (+0530) Subject: hw/core/loader: add check for zero size in load_image_targphys_as X-Git-Tag: v10.2.0-rc1~41^2~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=beded5ebd076d9d72862b8834f215712d94d3473;p=thirdparty%2Fqemu.git hw/core/loader: add check for zero size in load_image_targphys_as 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é Signed-off-by: Vishal Chourasia Message-ID: <20251024130556.1942835-10-vishalc@linux.ibm.com> Signed-off-by: Philippe Mathieu-Daudé --- 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; }