{"thread":{"id":"56342","subject":"[PATCH] compat: let git_mmap use malloc(3) directly","startedAt":"2021-08-21T12:52:46Z","lastAt":"2021-08-23T20:27:54Z","messageCount":3,"participants":["René Scharfe","Jeff King","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"433311","messageId":"9c2fe5de-be23-3e66-6edf-3c2edfb804f3@web.de","threadId":"56342","inReplyTo":null,"subject":"[PATCH] compat: let git_mmap use malloc(3) directly","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2021-08-21T12:52:40Z","receivedAt":"2021-08-21T12:52:46Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"xmalloc() dies on error, allows zero-sized allocations and enforces\nGIT_ALLOC_LIMIT for testing.  Our mmap replacement doesn't need any of\nthat.  Let's cut out the wrapper, reject zero-sized requests as required\nby POSIX and use malloc(3) directly.  Allocation errors were needlessly\nhandled by git_mmap() before; this code becomes reachable now.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n compat/mmap.c | 7 ++++++-\n 1 file changed, 6 insertions(+), 1 deletion(-)\n\ndiff --git a/compat/mmap.c b/compat/mmap.c\nindex 14d31010df..8d6c02d4bc 100644\n--- a/compat/mmap.c\n+++ b/compat/mmap.c\n@@ -7,7 +7,12 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of\n \tif (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ)\n \t\tdie(\"Invalid usage of mmap when built with NO_MMAP\");\n\n-\tstart = xmalloc(length);\n+\tif (length == 0) {\n+\t\terrno = EINVAL;\n+\t\treturn MAP_FAILED;\n+\t}\n+\n+\tstart = malloc(length);\n \tif (start == NULL) {\n \t\terrno = ENOMEM;\n \t\treturn MAP_FAILED;\n--\n2.33.0\n"},{"id":"433455","messageId":"YSPJqc3vVa7ZX63m@coredump.intra.peff.net","threadId":"56342","inReplyTo":"9c2fe5de-be23-3e66-6edf-3c2edfb804f3@web.de","subject":"Re: [PATCH] compat: let git_mmap use malloc(3) directly","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2021-08-23T16:15:37Z","receivedAt":"2021-08-23T16:15:40Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sat, Aug 21, 2021 at 02:52:40PM +0200, René Scharfe wrote:\n\n> xmalloc() dies on error, allows zero-sized allocations and enforces\n> GIT_ALLOC_LIMIT for testing.  Our mmap replacement doesn't need any of\n> that.  Let's cut out the wrapper, reject zero-sized requests as required\n> by POSIX and use malloc(3) directly.  Allocation errors were needlessly\n> handled by git_mmap() before; this code becomes reachable now.\n\nThanks, this is a good idea. As you note, the function was already\nexpecting a possible NULL return. And I think in general it's good for\nour compat/ wrappers to stay as close to the OS-level functions they're\nemulating as possible.\n\n-Peff\n"},{"id":"433490","messageId":"nycvar.QRO.7.76.6.2108232227140.55@tvgsbejvaqbjf.bet","threadId":"56342","inReplyTo":"9c2fe5de-be23-3e66-6edf-3c2edfb804f3@web.de","subject":"Re: [PATCH] compat: let git_mmap use malloc(3) directly","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2021-08-23T20:27:45Z","receivedAt":"2021-08-23T20:27:54Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi René,\n\nOn Sat, 21 Aug 2021, René Scharfe wrote:\n\n> xmalloc() dies on error, allows zero-sized allocations and enforces\n> GIT_ALLOC_LIMIT for testing.  Our mmap replacement doesn't need any of\n> that.  Let's cut out the wrapper, reject zero-sized requests as required\n> by POSIX and use malloc(3) directly.  Allocation errors were needlessly\n> handled by git_mmap() before; this code becomes reachable now.\n\nAcked-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n\nThanks!\nDscho\n\n>\n> Signed-off-by: René Scharfe <l.s.r@web.de>\n> ---\n>  compat/mmap.c | 7 ++++++-\n>  1 file changed, 6 insertions(+), 1 deletion(-)\n>\n> diff --git a/compat/mmap.c b/compat/mmap.c\n> index 14d31010df..8d6c02d4bc 100644\n> --- a/compat/mmap.c\n> +++ b/compat/mmap.c\n> @@ -7,7 +7,12 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of\n>  \tif (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ)\n>  \t\tdie(\"Invalid usage of mmap when built with NO_MMAP\");\n>\n> -\tstart = xmalloc(length);\n> +\tif (length == 0) {\n> +\t\terrno = EINVAL;\n> +\t\treturn MAP_FAILED;\n> +\t}\n> +\n> +\tstart = malloc(length);\n>  \tif (start == NULL) {\n>  \t\terrno = ENOMEM;\n>  \t\treturn MAP_FAILED;\n> --\n> 2.33.0\n>\n"}]}