From: Li Zhijian Date: Thu, 17 Jan 2019 12:49:02 +0000 (+0800) Subject: hw/core/loader.c: Read as long as possible in load_image_size() X-Git-Tag: v4.0.0-rc0~114^2~40 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1f40547f5ce0c135faa7d14f066b97002fd8c204;p=thirdparty%2Fqemu.git hw/core/loader.c: Read as long as possible in load_image_size() Don't expect read(2) can always read as many as it's told. CC: Richard Henderson CC: Stefano Garzarella Signed-off-by: Li Zhijian Reviewed-by: Richard Henderson Reviewed-by: Stefano Garzarella Signed-off-by: Paolo Bonzini --- diff --git a/hw/core/loader.c b/hw/core/loader.c index 3a000d576b3..fe5cb241225 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -77,21 +77,20 @@ int64_t get_image_size(const char *filename) ssize_t load_image_size(const char *filename, void *addr, size_t size) { int fd; - ssize_t actsize; + ssize_t actsize, l = 0; fd = open(filename, O_RDONLY | O_BINARY); if (fd < 0) { return -1; } - actsize = read(fd, addr, size); - if (actsize < 0) { - close(fd); - return -1; + while ((actsize = read(fd, addr + l, size - l)) > 0) { + l += actsize; } + close(fd); - return actsize; + return actsize < 0 ? -1 : l; } /* read()-like version */