git/compat
Ævar Arnfjörð Bjarmason 6eccfc3adf tr2: leave the parent list empty upon failure & don't leak memory
In a subsequent commit I'll be replacing most of this code to log N
parents, but let's first fix bugs introduced in the recent
2f732bf15e (tr2: log parent process name, 2021-07-21).

It was using the strbuf_read_file() in the wrong way, its return value
is either a length or a negative value on error. If we didn't have a
procfs, or otherwise couldn't access it we'd end up pushing an empty
string to the trace2 ancestry array.

It was also using the strvec_push() API the wrong way. That API always
does an xstrdup(), so by detaching the strbuf here we'd leak
memory. Let's instead pass in our pointer for strvec_push() to
xstrdup(), and then free our own strbuf. I do have some WIP changes to
make strvec_push_nodup() non-static, which makes this and some other
callsites nicer, but let's just follow the prevailing pattern of using
strvec_push() for now.

We'll also need to free that "procfs_path" strbuf whether or not
strbuf_read_file() succeeds, which was another source of memory leaks
in 2f732bf15e, i.e. we'd leak that memory as well if we weren't on a
system where we could read the file from procfs.

Let's move all the freeing of the memory to the end of the
function. If we're still at STRBUF_INIT with "name" due to not having
taken the branch where the strbuf_read_file() succeeds freeing it is
redundant. So we could move it into the body of the "if", but just
handling freeing the same way for all branches of the function makes
it more readable.

In combination with the preceding commit this makes all of
t[0-9]*trace2*.sh pass under SANITIZE=leak on Linux.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Acked-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-09-07 11:07:59 -07:00
..
linux tr2: leave the parent list empty upon failure & don't leak memory 2021-09-07 11:07:59 -07:00
nedmalloc Fix spelling errors in no-longer-updated-from-upstream modules 2019-11-10 16:00:55 +09:00
poll mingw: workaround for hangs when sending STDIN 2020-02-27 14:23:29 -08:00
regex compat/regex: move stdlib.h up in inclusion chain 2020-04-27 11:21:16 -07:00
simple-ipc simple-ipc: correct ifdefs when NO_PTHREADS is defined 2021-05-21 07:55:00 +09:00
stub tr2: make process info collection platform-generic 2021-07-22 13:35:20 -07:00
vcbuild ci(vs-build): stop passing the iconv library location explicitly 2020-12-04 12:03:15 -08:00
win32 run-command: trigger PATH lookup properly on Cygwin 2020-03-27 11:06:17 -07:00
access.c git-compat-util: work around for access(X_OK) under root 2019-04-25 17:49:44 +09:00
apple-common-crypto.h
basename.c
bswap.h compat/bswap.h: don't assume MSVC is little-endian 2020-11-11 11:24:47 -08:00
compiler.h bugreport: add compiler info 2020-04-16 15:23:42 -07:00
fileno.c git-compat-util: work around for access(X_OK) under root 2019-04-25 17:49:44 +09:00
fopen.c git_fopen: fix a sparse 'not declared' warning 2017-05-26 12:33:55 +09:00
hstrerror.c
inet_ntop.c
inet_pton.c
memmem.c
mingw.c mingw: align symlinks-related rmdir() behavior with Linux 2021-08-02 15:10:58 -07:00
mingw.h Makefile: add OPEN_RETURNS_EINTR knob 2021-02-26 14:15:51 -08:00
mkdir.c
mkdtemp.c
mmap.c compat: make sure git_mmap is not expected to write 2018-10-25 18:51:03 +09:00
msvc.c
msvc.h msvc: add pragmas for common warnings 2019-06-25 10:46:57 -07:00
obstack.c compat/obstack: fix -Wcast-function-type warnings 2019-01-17 11:13:38 -08:00
obstack.h obstack: avoid computing offsets from NULL pointer 2020-01-28 23:13:25 -08:00
open.c Makefile: add OPEN_RETURNS_EINTR knob 2021-02-26 14:15:51 -08:00
pread.c
precompose_utf8.c precompose_utf8: make precompose_string_if_needed() public 2021-04-05 17:30:04 -07:00
precompose_utf8.h precompose_utf8: make precompose_string_if_needed() public 2021-04-05 17:30:04 -07:00
qsort_s.c compat: add qsort_s() 2017-01-23 11:02:34 -08:00
setenv.c
sha1-chunked.c
sha1-chunked.h
snprintf.c
stat.c
strcasestr.c
strdup.c compat: move strdup(3) replacement to its own file 2016-09-07 10:41:45 -07:00
strlcpy.c
strtoimax.c
strtoumax.c
terminal.c strvec: convert more callers away from argv_array name 2020-07-28 15:02:18 -07:00
terminal.h terminal: add a new function to read a single keystroke 2020-01-15 12:06:17 -08:00
unsetenv.c
win32.h
win32mmap.c mmap(win32): avoid expensive fstat() call 2016-04-22 15:01:16 -07:00
winansi.c mingw: work around incorrect standard handles 2019-11-23 11:17:01 +09:00