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é <philmd@linaro.org>
Signed-off-by: Vishal Chourasia <vishalc@linux.ibm.com>
Message-ID: <20251024130556.1942835-10-vishalc@linux.ibm.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
This commit is contained in:
Vishal Chourasia 2025-10-24 18:35:59 +05:30 committed by Philippe Mathieu-Daudé
parent f62226f7dc
commit beded5ebd0

View file

@ -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;
}