Subject: Re: [PATCH 1/4] strbuf.c: keep errno in strbuf_read_file()

From: Michael Haggerty <[email-masked]>
Date: Fri, 26 Sep 2014 12:30:38 +0200

On 07/25/2014 12:43 PM, Nguyễn Thái Ngọc Duy wrote:
> This function is used to replaced some code in the next patch that
> does this (i.e. keep the errno when read() fails)
> 
> Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
> ---
>  strbuf.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/strbuf.c b/strbuf.c
> index 33018d8..61d685d 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -454,15 +454,18 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
>  
>  int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
>  {
> -	int fd, len;
> +	int fd, len, saved_errno;
>  
>  	fd = open(path, O_RDONLY);
>  	if (fd < 0)
>  		return -1;
>  	len = strbuf_read(sb, fd, hint);
> +	saved_errno = errno;
>  	close(fd);

Theoretically close() can fail, though it seems a little far-fetched
(and also uninteresting) if it fails for a file opened read-only. But if
it did, you would not notice the error.

So I grepped through our code to see whether we typically bother to
check the return value when close()ing a read-only file. And I found
that we rarely even check its return value when *writing* to a file.
(Many of those places are probably bugs.)

So, carry on and forget I said anything :-)

> -	if (len < 0)
> +	if (len < 0) {
> +		errno = saved_errno;
>  		return -1;
> +	}
>  	return len;
>  }

Reviewed-by: Michael Haggerty <[email-masked]>

Michael

-- 
Michael Haggerty
[email-masked]
http://softwareswirl.blogspot.com/