On 24/10/25 18:52, Philippe Mathieu-Daudé wrote:
On 24/10/25 14:58, Vishal Chourasia wrote:
Hi Philippe,
On 24/10/25 18:17, Philippe Mathieu-Daudé wrote:
On 24/10/25 14:11, Vishal Chourasia wrote:
Hi Balaton, Philippe
On 24/10/25 16:55, BALATON Zoltan wrote:
On Fri, 24 Oct 2025, Vishal Chourasia wrote:
Add error checking for lseek() failure and provide better error
messages when image loading fails, including filenames and
addresses.
Signed-off-by: Vishal Chourasia <[email protected]>
---
hw/core/loader.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/hw/core/loader.c b/hw/core/loader.c
index 7aca4989ef..48dd4e7b33 100644
--- a/hw/core/loader.c
+++ b/hw/core/loader.c
@@ -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;
}
@@ -129,14 +133,24 @@ ssize_t load_image_targphys_as(const char
*filename,
hwaddr addr, uint64_t max_sz,
AddressSpace *as,
Error **errp)
{
+ ERRP_GUARD();
ssize_t size;
size = get_image_size(filename, errp);
- if (size < 0 || size > max_sz) {
+ if (*errp) {
+ return -1;
+ }
+
+ if (size > max_sz) {
+ error_setg(errp, "%s exceeds maximum image size (%"
PRIu64 " MiB)",
+ filename, max_sz / MiB);
MiB is arbitrary here. This function is used to load all kinds of
images such as ROMs which may be 64k-2MB or even executables in
generic loader that can be a few kilobytes. This might result in
errors saying max size is 0 MiB if the allowed size is less than a
MiB (e.g. amigaone PROM_SIZE = 512 KiB) and integer division
discards fractions. Do we have a function to pretty print sizes or
maybe this should be left as bytes or at most kilobytes?
Yes, for images sizes less than a megabyte.
Perhaps we can leave it at Kilobytes
I will send out another version.
If so, then please use qemu_strtosz().
There is another function size_to_str() suggested by Peter I was
planning to use it, as it was doing the expected job.
It seems qemu_strtosz() converts size strings to bytes.
Yes, they are the opposite ;)
:)
Hi Philippe,
I have sent out v10 in which I have used size_to_str() to pretty print
size information.
I have retained your RB, but now I am unsure if i should have not.
Can you review again perhaps
Thanks,
Vishal