git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] compat: let git_mmap use malloc(3) directly
@ 2021-08-21 12:52 René Scharfe
  2021-08-23 16:15 ` Jeff King
  2021-08-23 20:27 ` Johannes Schindelin
  0 siblings, 2 replies; 3+ messages in thread
From: René Scharfe @ 2021-08-21 12:52 UTC (permalink / raw)
  To: Git List; +Cc: Junio C Hamano, Johannes Schindelin

xmalloc() dies on error, allows zero-sized allocations and enforces
GIT_ALLOC_LIMIT for testing.  Our mmap replacement doesn't need any of
that.  Let's cut out the wrapper, reject zero-sized requests as required
by POSIX and use malloc(3) directly.  Allocation errors were needlessly
handled by git_mmap() before; this code becomes reachable now.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 compat/mmap.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/compat/mmap.c b/compat/mmap.c
index 14d31010df..8d6c02d4bc 100644
--- a/compat/mmap.c
+++ b/compat/mmap.c
@@ -7,7 +7,12 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of
 	if (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ)
 		die("Invalid usage of mmap when built with NO_MMAP");

-	start = xmalloc(length);
+	if (length == 0) {
+		errno = EINVAL;
+		return MAP_FAILED;
+	}
+
+	start = malloc(length);
 	if (start == NULL) {
 		errno = ENOMEM;
 		return MAP_FAILED;
--
2.33.0

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] compat: let git_mmap use malloc(3) directly
  2021-08-21 12:52 [PATCH] compat: let git_mmap use malloc(3) directly René Scharfe
@ 2021-08-23 16:15 ` Jeff King
  2021-08-23 20:27 ` Johannes Schindelin
  1 sibling, 0 replies; 3+ messages in thread
From: Jeff King @ 2021-08-23 16:15 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List, Junio C Hamano, Johannes Schindelin

On Sat, Aug 21, 2021 at 02:52:40PM +0200, René Scharfe wrote:

> xmalloc() dies on error, allows zero-sized allocations and enforces
> GIT_ALLOC_LIMIT for testing.  Our mmap replacement doesn't need any of
> that.  Let's cut out the wrapper, reject zero-sized requests as required
> by POSIX and use malloc(3) directly.  Allocation errors were needlessly
> handled by git_mmap() before; this code becomes reachable now.

Thanks, this is a good idea. As you note, the function was already
expecting a possible NULL return. And I think in general it's good for
our compat/ wrappers to stay as close to the OS-level functions they're
emulating as possible.

-Peff

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] compat: let git_mmap use malloc(3) directly
  2021-08-21 12:52 [PATCH] compat: let git_mmap use malloc(3) directly René Scharfe
  2021-08-23 16:15 ` Jeff King
@ 2021-08-23 20:27 ` Johannes Schindelin
  1 sibling, 0 replies; 3+ messages in thread
From: Johannes Schindelin @ 2021-08-23 20:27 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List, Junio C Hamano

[-- Attachment #1: Type: text/plain, Size: 1241 bytes --]

Hi René,

On Sat, 21 Aug 2021, René Scharfe wrote:

> xmalloc() dies on error, allows zero-sized allocations and enforces
> GIT_ALLOC_LIMIT for testing.  Our mmap replacement doesn't need any of
> that.  Let's cut out the wrapper, reject zero-sized requests as required
> by POSIX and use malloc(3) directly.  Allocation errors were needlessly
> handled by git_mmap() before; this code becomes reachable now.

Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de>

Thanks!
Dscho

>
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  compat/mmap.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/compat/mmap.c b/compat/mmap.c
> index 14d31010df..8d6c02d4bc 100644
> --- a/compat/mmap.c
> +++ b/compat/mmap.c
> @@ -7,7 +7,12 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of
>  	if (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ)
>  		die("Invalid usage of mmap when built with NO_MMAP");
>
> -	start = xmalloc(length);
> +	if (length == 0) {
> +		errno = EINVAL;
> +		return MAP_FAILED;
> +	}
> +
> +	start = malloc(length);
>  	if (start == NULL) {
>  		errno = ENOMEM;
>  		return MAP_FAILED;
> --
> 2.33.0
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-08-23 20:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-21 12:52 [PATCH] compat: let git_mmap use malloc(3) directly René Scharfe
2021-08-23 16:15 ` Jeff King
2021-08-23 20:27 ` Johannes Schindelin

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).