diff mbox series

[for-4.0,v4,2/4] refactor load_image_size

Message ID 1544063533-10139-3-git-send-email-lizhijian@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show
Series allow to load initrd below 4G for recent kernel | expand

Commit Message

Li Zhijian Dec. 6, 2018, 2:32 a.m. UTC
Don't expect read(2) can always read as many as it's told.

Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

---
V4: add reviewed-by tag
---
 hw/core/loader.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

Comments

Michael S. Tsirkin Dec. 21, 2018, 4:12 p.m. UTC | #1
On Thu, Dec 06, 2018 at 10:32:11AM +0800, Li Zhijian wrote:
> Don't expect read(2) can always read as many as it's told.
> 
> Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

This is more a theoretical bugfix than a refactoring right?

> ---
> V4: add reviewed-by tag
> ---
>  hw/core/loader.c | 11 +++++------
>  1 file changed, 5 insertions(+), 6 deletions(-)
> 
> diff --git a/hw/core/loader.c b/hw/core/loader.c
> index fa41842..9cbceab 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 */
> -- 
> 2.7.4
Li Zhijian Dec. 24, 2018, 2:14 a.m. UTC | #2
On 12/22/18 00:12, Michael S. Tsirkin wrote:
> On Thu, Dec 06, 2018 at 10:32:11AM +0800, Li Zhijian wrote:
>> Don't expect read(2) can always read as many as it's told.
>>
>> Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com>
>> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> This is more a theoretical bugfix than a refactoring right?

Yes, it does.

how about change the title to : "enhance reading on load_image_size()" or such

Thanks
Zhijian


>
>> ---
>> V4: add reviewed-by tag
>> ---
>>   hw/core/loader.c | 11 +++++------
>>   1 file changed, 5 insertions(+), 6 deletions(-)
>>
>> diff --git a/hw/core/loader.c b/hw/core/loader.c
>> index fa41842..9cbceab 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 */
>> -- 
>> 2.7.4
>
>
>
Stefano Garzarella Jan. 7, 2019, 10:33 a.m. UTC | #3
On Mon, Dec 24, 2018 at 3:16 AM Li Zhijian <lizhijian@cn.fujitsu.com> wrote:
>
>
> On 12/22/18 00:12, Michael S. Tsirkin wrote:
> > On Thu, Dec 06, 2018 at 10:32:11AM +0800, Li Zhijian wrote:
> >> Don't expect read(2) can always read as many as it's told.
> >>
> >> Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com>
> >> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> > This is more a theoretical bugfix than a refactoring right?
>
> Yes, it does.
>
> how about change the title to : "enhance reading on load_image_size()" or such

Maybe something like this: "hw/core/loader.c: Read as long as possible
in load_image_size()"

Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>

>
> Thanks
> Zhijian
>
>
> >
> >> ---
> >> V4: add reviewed-by tag
> >> ---
> >>   hw/core/loader.c | 11 +++++------
> >>   1 file changed, 5 insertions(+), 6 deletions(-)
> >>
> >> diff --git a/hw/core/loader.c b/hw/core/loader.c
> >> index fa41842..9cbceab 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 */
> >> --
> >> 2.7.4
> >
> >
> >
> --
> Best regards.
> Li Zhijian (8528)
>
>
>
Li Zhijian Jan. 8, 2019, 1:09 a.m. UTC | #4
On 1/7/19 18:33, Stefano Garzarella wrote:
> On Mon, Dec 24, 2018 at 3:16 AM Li Zhijian <lizhijian@cn.fujitsu.com> wrote:
>>
>> On 12/22/18 00:12, Michael S. Tsirkin wrote:
>>> On Thu, Dec 06, 2018 at 10:32:11AM +0800, Li Zhijian wrote:
>>>> Don't expect read(2) can always read as many as it's told.
>>>>
>>>> Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com>
>>>> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>>> This is more a theoretical bugfix than a refactoring right?
>> Yes, it does.
>>
>> how about change the title to : "enhance reading on load_image_size()" or such
> Maybe something like this: "hw/core/loader.c: Read as long as possible
> in load_image_size()"

It really helps, i will take your suggestion to the subject.

Thanks
Zhijian




>
> Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
>
diff mbox series

Patch

diff --git a/hw/core/loader.c b/hw/core/loader.c
index fa41842..9cbceab 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 */