# [PATCH] close clobbers mmap's errno in read_cache

3 messages from 2005-10-07 to 2005-10-07. Participants: Alex Riesen, Sven Verdoolaege.
Thread: https://gitlist.dev/t/2043

## Alex Riesen, 2005-10-07 21:45

Subject: [PATCH] close clobbers mmap's errno in read_cache
Message-ID: <20051007214551.GA8893@steel.home>
URL: https://gitlist.dev/e/20051007214551.GA8893%40steel.home

```
Save errno before close(fd) for strerror in die().

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>

--- a/read-cache.c	2005-10-03 09:16:31.000000000 +0200
+++ b/read-cache.c	2005-10-07 23:23:40.000000000 +0200
@@ -482,9 +482,10 @@ int read_cache(void)
 		if (size >= sizeof(struct cache_header) + 20)
 			map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
 	}
+	i = errno;
 	close(fd);
 	if (map == MAP_FAILED)
-		die("index file mmap failed (%s)", strerror(errno));
+		die("index file mmap failed (%s)", strerror(i));
 
 	hdr = map;
 	if (verify_hdr(hdr, size) < 0)

```

## Sven Verdoolaege, 2005-10-07 21:48

Subject: Re: [PATCH] close clobbers mmap's errno in read_cache
Message-ID: <20051007214845.GH8383MdfPADPa@greensroom.kotnet.org>
URL: https://gitlist.dev/e/20051007214845.GH8383MdfPADPa%40greensroom.kotnet.org
In-Reply-To: <20051007214551.GA8893@steel.home>

```
On Fri, Oct 07, 2005 at 11:45:51PM +0200, Alex Riesen wrote:
>  	}
> +	i = errno;
>  	close(fd);
>  	if (map == MAP_FAILED)
> -		die("index file mmap failed (%s)", strerror(errno));
> +		die("index file mmap failed (%s)", strerror(i));
>  

Why don't you just move the close after the test ?
There's no point in closing if you're going to die.

skimo

```

## Alex Riesen, 2005-10-07 23:50

Subject: Re: [PATCH] close clobbers mmap's errno in read_cache
Message-ID: <20051007235011.GA8189@steel.home>
URL: https://gitlist.dev/e/20051007235011.GA8189%40steel.home
In-Reply-To: <20051007214845.GH8383MdfPADPa@greensroom.kotnet.org>

```
Sven Verdoolaege, Fri, Oct 07, 2005 23:48:45 +0200:
> On Fri, Oct 07, 2005 at 11:45:51PM +0200, Alex Riesen wrote:
> >  	}
> > +	i = errno;
> >  	close(fd);
> >  	if (map == MAP_FAILED)
> > -		die("index file mmap failed (%s)", strerror(errno));
> > +		die("index file mmap failed (%s)", strerror(i));
> >  
> 
> Why don't you just move the close after the test ?

I don't know.

> There's no point in closing if you're going to die.
> 
> skimo

Of course you're right :) I'm just blind after eight.

```
