compat: let git_mmap use malloc(3) directly
PATCH 1/1 compat: let git_mmap use malloc(3) directly
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.
--- compat/mmap.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
compat/mmap.c +6 -1
@@ -7,7 +7,12 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t ofif (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
Quoted 5 lines · René Scharfe
xmalloc()dies on error, allows zero-sized allocations and enforcesGIT_ALLOC_LIMITfor 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 bygit_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
Hi René,
Quoted 5 lines · René Scharfe
xmalloc()dies on error, allows zero-sized allocations and enforcesGIT_ALLOC_LIMITfor 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 bygit_mmap()before; this code becomes reachable now.
Thanks!
Dscho
Quoted 27 lines
Signed-off-by René Scharfe <[email protected]>--- compat/mmap.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)compat/mmap.c +6
-2@@ -7,7 +7,12 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t ofif (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