style: Silenced Cppcheck warnings
[para.git] / scripts / runleaks
blob5fdf4543a1e2314fa1dc2e9f6a7b59219665360a
1 #!/bin/sh
4 # Run leaks with every test profile.
6 # Copyright 2024 Odin Kroeger.
8 # This file is part of Para.
10 # Para is free software: you can redistribute it and/or modify it under
11 # the terms of the GNU General Public License as published by the Free
12 # Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # Para is distributed in the hope that it will be useful, but WITHOUT
16 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 # License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Para. If not, see <https://www.gnu.org/licenses/>.
24 # shellcheck disable=2015,2016,2034,2094
27 # Init
30 set -eu
31 scriptdir="$(cd -P "$(dirname -- "$0")" && pwd)"
32 : "${scriptdir:?}"
33 readonly scriptdir
34 rootdir="$(cd "$scriptdir/.." && pwd)"
35 : "${rootdir:?}"
36 : "${BUILDDIR:="$rootdir"}"
37 : "${SRCDIR:="$rootdir"}"
38 unset rootdir
39 readonly BUILDDIR SRCDIR
41 # shellcheck disable=2154
42 export PATH="$BUILDDIR:$PATH"
44 # shellcheck disable=1091
45 . "$SRCDIR/libutil.sh" || exit
46 init || exit
47 # shellcheck disable=2119
48 mktmpdir || exit
50 # Always exit with a status indicating an error unless $fin is set
51 atexit="${atexit-:}; [ \"\${caught-}\" ] || [ \"\${fin-}\" ] || exit 4"
53 eval "$(para -C)"
54 export HAVE_GETLOADAVG NEXTENSIONS
58 # Defaults
61 # Where to look for test cases
62 testsdir="$SRCDIR/tests"
64 # Be quiet?
65 quiet=
67 # Stop at first memory leak?
68 stop=
70 # Options to pass to leaks
71 leaksopts=
75 # Arguments
78 OPTARG='' OPTIND=1 opt=''
79 while getopts 'dho:qs' opt
81 case $opt in
82 (h) exec cat <<EOF
83 $progname - run leaks with every test
85 Usage: $progname [-d] [-o opt]... [-q] [-s] [test ...]
86 $progname -h
88 Options:
89 -d Enable debugging mode.
90 -o opt Pass opt to leaks.
91 -q Be quiet.
92 -s Stop at first leak.
93 -h Show this help screen.
94 EOF
96 (d) set -x ;;
97 (o) leaksopts="$leaksopts$OPTARG " ;;
98 (q) quiet=y ;;
99 (s) stop=y ;;
100 (*) exit 2
101 esac
102 done
103 shift $((OPTIND - 1))
105 if [ "$#" -eq 0 ]
106 then set -- "$testsdir/integration"
111 # Prelude
114 readonly findpipe="$TMPDIR/find.fifo"
115 mkfifo "$findpipe"
119 # Main
122 export MallocCheckZeroOnFreeCorruption=1
123 export MallocDebugReport=crash
124 export MallocErrorAbort=1
125 export MallocGuardEdges=1
126 export MallocNanoZone=0
127 export MallocScribble=1
128 export MallocStackLogging=1
129 export MallocZeroOnFree=1
131 find "$@" -type f ! -name '.*' >"$findpipe" & findpid=$!
132 failc=0 passc=0 runc=0
133 while read -r file
135 # shellcheck disable=2119
136 spin
138 # shellcheck disable=1090
139 . "$file"
141 : "${command:=}"
142 : "${environ=}"
144 if ! [ "${command-}" ]
145 then continue
148 for var in $environ
149 do export "${var?}"
150 done
152 set +e
153 eval "leaks $leaksopts -atExit -quiet -- $command" >/dev/null 2>&1
154 retval=$?
155 set -e
157 for var in $environ
158 do unset "${var%%=*}"
159 done
161 case $retval in
162 (0) passc="$((passc + 1))"
164 (1) failc="$((failc + 1))"
165 warn '%s: Detected issue' "$command"
167 (*) err 'leaks %s: Exited with status $d' "$command" "$retval"
168 esac
170 unset environ command
172 if [ "$stop" ] && [ "$failc" -gt 0 ]
173 then exit 1
176 runc="$((runc + 1))"
177 done <"$findpipe"
178 wait "$findpid"
180 # shellcheck disable=2119
181 rewindln
183 fin=y
184 warn -q 'Checked %d test case(s), found %d issue(s)' "$runc" "$failc"