{"thread":{"id":"51480","subject":"[PATCH] read-cache.c: do not die if mmap fails","startedAt":"2019-07-14T03:02:29Z","lastAt":"2019-07-14T18:59:57Z","messageCount":2,"participants":["Varun Naik","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"378924","messageId":"20190714030153.22022-1-vcnaik94@gmail.com","threadId":"51480","inReplyTo":null,"subject":"[PATCH] read-cache.c: do not die if mmap fails","fromName":"Varun Naik","fromEmail":"vcnaik94@gmail.com","sentAt":"2019-07-14T03:01:53Z","receivedAt":"2019-07-14T03:02:29Z","isPatch":true,"sender":{"key":"vcnaik94@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2382501?v=4"},"body":"do_read_index() mmaps the index, or tries to die with an error message\non failure. It should call xmmap_gently(), which returns MAP_FAILED,\nrather than xmmap(), which dies with its own error message.\n\nAn easy way to cause this mmap to fail is by setting $GIT_INDEX_FILE to\na path to a directory and then invoking any command that reads from the\nindex.\n\nSigned-off-by: Varun Naik <vcnaik94@gmail.com>\n---\nI believe this is the only place that calls xmmap() when it should be\ncalling xmmap_gently(). There is a related recent commit 3203566a71\n(\"Use xmmap_gently instead of xmmap in use_pack\", 2019-05-16).\n\n read-cache.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/read-cache.c b/read-cache.c\nindex 22e7b9944e..4e30dafa9d 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -2140,7 +2140,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)\n \tif (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz)\n \t\tdie(_(\"%s: index file smaller than expected\"), path);\n \n-\tmmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);\n+\tmmap = xmmap_gently(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);\n \tif (mmap == MAP_FAILED)\n \t\tdie_errno(_(\"%s: unable to map index file\"), path);\n \tclose(fd);\n-- \n2.22.0\n\n"},{"id":"378934","messageId":"xmqq7e8k5tn9.fsf@gitster-ct.c.googlers.com","threadId":"51480","inReplyTo":"20190714030153.22022-1-vcnaik94@gmail.com","subject":"Re: [PATCH] read-cache.c: do not die if mmap fails","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2019-07-14T18:59:54Z","receivedAt":"2019-07-14T18:59:57Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Varun Naik <vcnaik94@gmail.com> writes:\n\n> do_read_index() mmaps the index, or tries to die with an error message\n> on failure. It should call xmmap_gently(), which returns MAP_FAILED,\n> rather than xmmap(), which dies with its own error message.\n>\n> An easy way to cause this mmap to fail is by setting $GIT_INDEX_FILE to\n> a path to a directory and then invoking any command that reads from the\n> index.\n>\n> Signed-off-by: Varun Naik <vcnaik94@gmail.com>\n> ---\n> I believe this is the only place that calls xmmap() when it should be\n> calling xmmap_gently(). There is a related recent commit 3203566a71\n> (\"Use xmmap_gently instead of xmmap in use_pack\", 2019-05-16).\n\nNice find and thanks for checking other callsites of xmmap().\n\n>\n>  read-cache.c | 2 +-\n>  1 file changed, 1 insertion(+), 1 deletion(-)\n>\n> diff --git a/read-cache.c b/read-cache.c\n> index 22e7b9944e..4e30dafa9d 100644\n> --- a/read-cache.c\n> +++ b/read-cache.c\n> @@ -2140,7 +2140,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)\n>  \tif (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz)\n>  \t\tdie(_(\"%s: index file smaller than expected\"), path);\n>  \n> -\tmmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);\n> +\tmmap = xmmap_gently(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);\n>  \tif (mmap == MAP_FAILED)\n>  \t\tdie_errno(_(\"%s: unable to map index file\"), path);\n>  \tclose(fd);\n"}]}