]> git.ipfire.org Git - thirdparty/git.git/commitdiff
test-lib: move malloc-debug setup after $PATH setup
authorJeff King <peff@peff.net>
Thu, 14 Nov 2024 01:39:12 +0000 (20:39 -0500)
committerJunio C Hamano <gitster@pobox.com>
Thu, 14 Nov 2024 03:19:26 +0000 (12:19 +0900)
Originally, the conditional definition of the setup/teardown functions
for malloc checking could be run at any time, because they depended only
on command-line options and the system getconf function.

But since 02d900361c (test-lib: check malloc debug LD_PRELOAD before
using, 2024-11-11), we probe the system by running "git version". Since
this code runs before we've set $PATH to point to the version of Git we
intend to test, we actually run the system version of git.

This mostly works, since what we really care about is whether the
LD_PRELOAD works, and it should work the same with any program. But
there are some corner cases:

  1. You might not have a system git at all, in which case the preload
     will appear to fail, even though it could work with the actual
     built version of git.

  2. Your system git could be linked in a different way. For example, if
     it was built statically, then it will ignore LD_PRELOAD entirely,
     and we might assume that the preload works, even though it might
     not when used with a dynamic build.

We could give a more complete path to the version of Git we intend to
test, but features like GIT_TEST_INSTALLED make that not entirely
trivial. So instead, let's just bump the setup until after we've set up
the $PATH. There's no need for us to do it early, as long as it is done
before the first test runs.

Reported-by: Toon Claes <toon@iotcl.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/test-lib.sh

index e715d07f397d3d350147ee3bc892e12986fbc7bb..424f0b16afc80f87529937e44d62461a37d4d31a 100644 (file)
@@ -577,56 +577,6 @@ case $GIT_TEST_FSYNC in
        ;;
 esac
 
-# Add libc MALLOC and MALLOC_PERTURB test only if we are not executing
-# the test with valgrind and have not compiled with conflict SANITIZE
-# options.
-if test -n "$valgrind" ||
-   test -n "$SANITIZE_ADDRESS" ||
-   test -n "$SANITIZE_LEAK" ||
-   test -n "$TEST_NO_MALLOC_CHECK"
-then
-       setup_malloc_check () {
-               : nothing
-       }
-       teardown_malloc_check () {
-               : nothing
-       }
-else
-       _USE_GLIBC_TUNABLES=
-       _USE_GLIBC_PRELOAD=libc_malloc_debug.so.0
-       if _GLIBC_VERSION=$(getconf GNU_LIBC_VERSION 2>/dev/null) &&
-          _GLIBC_VERSION=${_GLIBC_VERSION#"glibc "} &&
-          expr 2.34 \<= "$_GLIBC_VERSION" >/dev/null &&
-          stderr=$(LD_PRELOAD=$_USE_GLIBC_PRELOAD git version 2>&1 >/dev/null) &&
-          test -z "$stderr"
-       then
-               _USE_GLIBC_TUNABLES=YesPlease
-       fi
-       setup_malloc_check () {
-               local g
-               local t
-               MALLOC_CHECK_=3 MALLOC_PERTURB_=165
-               export MALLOC_CHECK_ MALLOC_PERTURB_
-               if test -n "$_USE_GLIBC_TUNABLES"
-               then
-                       g=
-                       LD_PRELOAD=$_USE_GLIBC_PRELOAD
-                       for t in \
-                               glibc.malloc.check=1 \
-                               glibc.malloc.perturb=165
-                       do
-                               g="${g#:}:$t"
-                       done
-                       GLIBC_TUNABLES=$g
-                       export LD_PRELOAD GLIBC_TUNABLES
-               fi
-       }
-       teardown_malloc_check () {
-               unset MALLOC_CHECK_ MALLOC_PERTURB_
-               unset LD_PRELOAD GLIBC_TUNABLES
-       }
-fi
-
 # Protect ourselves from common misconfiguration to export
 # CDPATH into the environment
 unset CDPATH
@@ -1486,6 +1436,56 @@ GIT_ATTR_NOSYSTEM=1
 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/.."
 export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM GIT_CEILING_DIRECTORIES
 
+# Add libc MALLOC and MALLOC_PERTURB test only if we are not executing
+# the test with valgrind and have not compiled with conflict SANITIZE
+# options.
+if test -n "$valgrind" ||
+   test -n "$SANITIZE_ADDRESS" ||
+   test -n "$SANITIZE_LEAK" ||
+   test -n "$TEST_NO_MALLOC_CHECK"
+then
+       setup_malloc_check () {
+               : nothing
+       }
+       teardown_malloc_check () {
+               : nothing
+       }
+else
+       _USE_GLIBC_TUNABLES=
+       _USE_GLIBC_PRELOAD=libc_malloc_debug.so.0
+       if _GLIBC_VERSION=$(getconf GNU_LIBC_VERSION 2>/dev/null) &&
+          _GLIBC_VERSION=${_GLIBC_VERSION#"glibc "} &&
+          expr 2.34 \<= "$_GLIBC_VERSION" >/dev/null &&
+          stderr=$(LD_PRELOAD=$_USE_GLIBC_PRELOAD git version 2>&1 >/dev/null) &&
+          test -z "$stderr"
+       then
+               _USE_GLIBC_TUNABLES=YesPlease
+       fi
+       setup_malloc_check () {
+               local g
+               local t
+               MALLOC_CHECK_=3 MALLOC_PERTURB_=165
+               export MALLOC_CHECK_ MALLOC_PERTURB_
+               if test -n "$_USE_GLIBC_TUNABLES"
+               then
+                       g=
+                       LD_PRELOAD=$_USE_GLIBC_PRELOAD
+                       for t in \
+                               glibc.malloc.check=1 \
+                               glibc.malloc.perturb=165
+                       do
+                               g="${g#:}:$t"
+                       done
+                       GLIBC_TUNABLES=$g
+                       export LD_PRELOAD GLIBC_TUNABLES
+               fi
+       }
+       teardown_malloc_check () {
+               unset MALLOC_CHECK_ MALLOC_PERTURB_
+               unset LD_PRELOAD GLIBC_TUNABLES
+       }
+fi
+
 if test -z "$GIT_TEST_CMP"
 then
        if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"