# [PATCH] mingw: do not set errno to 0 on success

3 messages from 2010-11-23 to 2010-11-24. Participants: Erik Faye-Lund, Junio C Hamano.
Thread: https://gitlist.dev/t/25825

## Erik Faye-Lund, 2010-11-23 19:53

Subject: [PATCH] mingw: do not set errno to 0 on success
Message-ID: <1290541988-4608-1-git-send-email-kusmabite@gmail.com>
URL: https://gitlist.dev/e/1290541988-4608-1-git-send-email-kusmabite%40gmail.com

```
Currently do_lstat always sets errno to 0 on success. This incorrectly
overwrites previous errors.

Fetch the error-code into a temporary variable instead, and assign that
to errno on failure.

Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
---

A bug I found while hunting down another regression. maint-worthy, perhaps?

 compat/mingw.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/compat/mingw.c b/compat/mingw.c
index f2d9e1f..b98e600 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -195,9 +195,10 @@ static inline time_t filetime_to_time_t(const FILETIME *ft)
  */
 static int do_lstat(const char *file_name, struct stat *buf)
 {
+	int err;
 	WIN32_FILE_ATTRIBUTE_DATA fdata;
 
-	if (!(errno = get_file_attr(file_name, &fdata))) {
+	if (!(err = get_file_attr(file_name, &fdata))) {
 		buf->st_ino = 0;
 		buf->st_gid = 0;
 		buf->st_uid = 0;
@@ -211,6 +212,7 @@ static int do_lstat(const char *file_name, struct stat *buf)
 		buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
 		return 0;
 	}
+	errno = err;
 	return -1;
 }
 
-- 
1.7.3.2

```

## Junio C Hamano, 2010-11-24 00:07

Subject: Re: [PATCH] mingw: do not set errno to 0 on success
Message-ID: <7v4ob7vb1m.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v4ob7vb1m.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1290541988-4608-1-git-send-email-kusmabite@gmail.com>

```
Erik Faye-Lund <kusmabite@gmail.com> writes:

> Currently do_lstat always sets errno to 0 on success. This incorrectly
> overwrites previous errors.
>
> Fetch the error-code into a temporary variable instead, and assign that
> to errno on failure.
>
> Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
> ---
>
> A bug I found while hunting down another regression. maint-worthy, perhaps?

I would say so, as long as the error return from get_file_attr() is
trustworthy.

>
>  compat/mingw.c |    4 +++-
>  1 files changed, 3 insertions(+), 1 deletions(-)
>
> diff --git a/compat/mingw.c b/compat/mingw.c
> index f2d9e1f..b98e600 100644
> --- a/compat/mingw.c
> +++ b/compat/mingw.c
> @@ -195,9 +195,10 @@ static inline time_t filetime_to_time_t(const FILETIME *ft)
>   */
>  static int do_lstat(const char *file_name, struct stat *buf)
>  {
> +	int err;
>  	WIN32_FILE_ATTRIBUTE_DATA fdata;
>  
> -	if (!(errno = get_file_attr(file_name, &fdata))) {
> +	if (!(err = get_file_attr(file_name, &fdata))) {
>  		buf->st_ino = 0;
>  		buf->st_gid = 0;
>  		buf->st_uid = 0;
> @@ -211,6 +212,7 @@ static int do_lstat(const char *file_name, struct stat *buf)
>  		buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
>  		return 0;
>  	}
> +	errno = err;
>  	return -1;
>  }
>  
> -- 
> 1.7.3.2

```

## Erik Faye-Lund, 2010-11-24 09:54

Subject: Re: [PATCH] mingw: do not set errno to 0 on success
Message-ID: <AANLkTi=8OnTJHgX3-g9uqLX2B3+eUQf0uykynrwW3L4E@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTi%3D8OnTJHgX3-g9uqLX2B3%2BeUQf0uykynrwW3L4E%40mail.gmail.com
In-Reply-To: <7v4ob7vb1m.fsf@alter.siamese.dyndns.org>

```
On Wed, Nov 24, 2010 at 1:07 AM, Junio C Hamano <gitster@pobox.com> wrote:
> Erik Faye-Lund <kusmabite@gmail.com> writes:
>
>> Currently do_lstat always sets errno to 0 on success. This incorrectly
>> overwrites previous errors.
>>
>> Fetch the error-code into a temporary variable instead, and assign that
>> to errno on failure.
>>
>> Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
>> ---
>>
>> A bug I found while hunting down another regression. maint-worthy, perhaps?
>
> I would say so, as long as the error return from get_file_attr() is
> trustworthy.

It looks trustworthy to me; it returns 0 if GetFileAttributesExA
succeeds, and either EACCES, ENAMETOOLONG, ENOMEM or ENOENT based on
the result of GetLastError().

```
