From: Paul Floyd Date: Fri, 17 Jun 2022 11:52:45 +0000 (+0200) Subject: Fix a few issues with reallocf and add a FreeBSD amd64 regtest X-Git-Tag: VALGRIND_3_20_0~31 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f5fcd59dab99d85b61f8bcff5e1d18b9d3ff1e5;p=thirdparty%2Fvalgrind.git Fix a few issues with reallocf and add a FreeBSD amd64 regtest 1. new_size is size_t (unsigned) and can't be negative 2. NULL not returned when the size is 0 and the memory freed 3. set ENOMEM if the allocation fails (but this time NULL does get returned) --- diff --git a/.gitignore b/.gitignore index 1bce8a6287..8accdba2b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1358,6 +1358,7 @@ /memcheck/tests/amd64-freebsd/Makefile.in /memcheck/tests/amd64-freebsd/posix_fadvise /memcheck/tests/amd64-freebsd/posix_fallocate +/memcheck/tests/amd64-freebsd/reallocf # /memcheck/tests/x86-freebsd /memcheck/tests/x86-freebsd/*.stderr.diff diff --git a/coregrind/m_replacemalloc/vg_replace_malloc.c b/coregrind/m_replacemalloc/vg_replace_malloc.c index 164c9ed910..eb087676ca 100644 --- a/coregrind/m_replacemalloc/vg_replace_malloc.c +++ b/coregrind/m_replacemalloc/vg_replace_malloc.c @@ -1431,15 +1431,17 @@ extern int *___errno (void) __attribute__((weak)); one which we know exists. */ \ return VG_REPLACE_FUNCTION_EZU(10010,VG_Z_LIBC_SONAME,malloc) \ (new_size); \ - if (new_size <= 0) { \ + if (new_size == 0) { \ VG_REPLACE_FUNCTION_EZU(10050,VG_Z_LIBC_SONAME,free)(ptrV); \ MALLOC_TRACE(" = 0\n"); \ - return NULL; \ + return ptrV; \ } \ v = (void*)VALGRIND_NON_SIMD_CALL2( info.tl_realloc, ptrV, new_size ); \ MALLOC_TRACE(" = %p\n", v ); \ - if (v == NULL) \ + if (v == NULL) {\ VG_REPLACE_FUNCTION_EZU(10050,VG_Z_LIBC_SONAME,free)(ptrV); \ + SET_ERRNO_ENOMEM; \ + } \ MALLOC_TRACE(" = %p\n", v ); \ return v; \ } diff --git a/memcheck/tests/amd64-freebsd/Makefile.am b/memcheck/tests/amd64-freebsd/Makefile.am index 80854419dd..190fdd6aeb 100644 --- a/memcheck/tests/amd64-freebsd/Makefile.am +++ b/memcheck/tests/amd64-freebsd/Makefile.am @@ -7,10 +7,12 @@ EXTRA_DIST = \ posix_fadvise.vgtest \ posix_fallocate.vgtest \ posix_fadvise.stderr.exp \ - posix_fallocate.stderr.exp + posix_fallocate.stderr.exp \ + reallocf.vgtest \ + reallocf.stderr.out check_PROGRAMS = \ - posix_fadvise posix_fallocate + posix_fadvise posix_fallocate reallocf AM_CFLAGS += @FLAG_M64@ AM_CXXFLAGS += @FLAG_M64@ diff --git a/memcheck/tests/amd64-freebsd/reallocf.c b/memcheck/tests/amd64-freebsd/reallocf.c new file mode 100644 index 0000000000..043d0d6d7a --- /dev/null +++ b/memcheck/tests/amd64-freebsd/reallocf.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include "../../memcheck.h" + +int main(void) +{ + int *pi = NULL; + VALGRIND_DO_LEAK_CHECK; + pi = reallocf(pi, 10*sizeof(int)); + VALGRIND_DO_ADDED_LEAK_CHECK; + pi = reallocf(pi, 0); + VALGRIND_DO_CHANGED_LEAK_CHECK; + pi = NULL; + pi = realloc(pi, 10*sizeof(int)); + VALGRIND_DO_ADDED_LEAK_CHECK; + errno = 0; + pi = reallocf(pi, 1UL << 49); + assert(!pi); + assert(errno == ENOMEM); + VALGRIND_DO_CHANGED_LEAK_CHECK; +} diff --git a/memcheck/tests/amd64-freebsd/reallocf.stderr.exp b/memcheck/tests/amd64-freebsd/reallocf.stderr.exp new file mode 100644 index 0000000000..b3e6658b78 --- /dev/null +++ b/memcheck/tests/amd64-freebsd/reallocf.stderr.exp @@ -0,0 +1,34 @@ + +All heap blocks were freed -- no leaks are possible + +LEAK SUMMARY: + definitely lost: 0 (+0) bytes in 0 (+0) blocks + indirectly lost: 0 (+0) bytes in 0 (+0) blocks + possibly lost: 0 (+0) bytes in 0 (+0) blocks + still reachable: 40 (+40) bytes in 1 (+1) blocks + suppressed: 0 (+0) bytes in 0 (+0) blocks +Reachable blocks (those to which a pointer was found) are not shown. +To see them, rerun with: --leak-check=full --show-leak-kinds=all + +All heap blocks were freed -- no leaks are possible + +LEAK SUMMARY: + definitely lost: 0 (+0) bytes in 0 (+0) blocks + indirectly lost: 0 (+0) bytes in 0 (+0) blocks + possibly lost: 0 (+0) bytes in 0 (+0) blocks + still reachable: 40 (+0) bytes in 1 (+0) blocks + suppressed: 0 (+0) bytes in 0 (+0) blocks +Reachable blocks (those to which a pointer was found) are not shown. +To see them, rerun with: --leak-check=full --show-leak-kinds=all + +All heap blocks were freed -- no leaks are possible + + +HEAP SUMMARY: + in use at exit: 0 bytes in 0 blocks + total heap usage: 3 allocs, 3 frees, 562,949,953,421,392 bytes allocated + +For a detailed leak analysis, rerun with: --leak-check=full + +For lists of detected and suppressed errors, rerun with: -s +ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) diff --git a/memcheck/tests/amd64-freebsd/reallocf.vgtest b/memcheck/tests/amd64-freebsd/reallocf.vgtest new file mode 100644 index 0000000000..a01b083b8e --- /dev/null +++ b/memcheck/tests/amd64-freebsd/reallocf.vgtest @@ -0,0 +1 @@ +prog: reallocf