threads / patch / 51480

patchread-cache.c: do not die if mmap fails

Subject: [PATCH] read-cache.c: do not die if mmap fails

## tl;dr

2 messages between Jul 14, 2019 and Jul 14, 2019. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Varun Naik· Jul 14, 2019, 03:01 UTC · lore

do_read_index() mmaps the index, or tries to die with an error message on failure. It should call xmmap_gently(), which returns MAP_FAILED, rather than xmmap(), which dies with its own error message.

An easy way to cause this mmap to fail is by setting $GIT_INDEX_FILE to a path to a directory and then invoking any command that reads from the index.

Signed-off-by: Varun Naik <vcnaik94@gmail.com>
---
I believe this is the only place that calls xmmap() when it should be
calling xmmap_gently(). There is a related recent commit 3203566a71
("Use xmmap_gently instead of xmmap in use_pack", 2019-05-16).
 read-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to read-cache.c +1 −1
diff --git a/read-cache.c b/read-cache.c
index 22e7b9944e..4e30dafa9d 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2140,7 +2140,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
 	if (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
 		die(_("%s: index file smaller than expected"), path);
 
-	mmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);
+	mmap = xmmap_gently(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);
 	if (mmap == MAP_FAILED)
 		die_errno(_("%s: unable to map index file"), path);
 	close(fd);
-- 
2.22.0
Junio C Hamano· Jul 14, 2019, 18:59 UTC · re: Varun Naik · lore

Re: [PATCH] read-cache.c: do not die if mmap fails

Varun Naik <vcnaik94@gmail.com> writes:
Show 13 quoted lines
> do_read_index() mmaps the index, or tries to die with an error message
> on failure. It should call xmmap_gently(), which returns MAP_FAILED,
> rather than xmmap(), which dies with its own error message.
>
> An easy way to cause this mmap to fail is by setting $GIT_INDEX_FILE to
> a path to a directory and then invoking any command that reads from the
> index.
>
> Signed-off-by: Varun Naik <vcnaik94@gmail.com>
> ---
> I believe this is the only place that calls xmmap() when it should be
> calling xmmap_gently(). There is a related recent commit 3203566a71
> ("Use xmmap_gently instead of xmmap in use_pack", 2019-05-16).
Nice find and thanks for checking other callsites of xmmap().
Show 17 quoted lines
>
>  read-cache.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/read-cache.c b/read-cache.c
> index 22e7b9944e..4e30dafa9d 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -2140,7 +2140,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
>  	if (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
>  		die(_("%s: index file smaller than expected"), path);
>  
> -	mmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);
> +	mmap = xmmap_gently(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);
>  	if (mmap == MAP_FAILED)
>  		die_errno(_("%s: unable to map index file"), path);
>  	close(fd);

← back to recent threads