mirror of
https://github.com/git/git.git
synced 2024-12-11 10:54:21 +08:00
7ce4088ab7
When handling OPT_FILENAME(), we have to stick the "prefix" (if any) in front of the filename to make up for the fact that Git has chdir()'d to the top of the repository. We can do this with prefix_filename(), but there are a few special cases we handle ourselves. Unfortunately the memory allocation is inconsistent here; if we do make it to prefix_filename(), we'll allocate a string which the caller must free to avoid a leak. But if we hit our special cases, we'll return the string as-is, and a caller which tries to free it will crash. So there's no way to win. Let's consistently allocate, so that callers can do the right thing. There are now three cases to care about in the function (and hence a three-armed if/else): 1. we got a NULL input (and should leave it as NULL, though arguably this is the sign of a bug; let's keep the status quo for now and we can pick at that scab later) 2. we hit a special case that means we leave the name intact; we should duplicate the string. This includes our special "-" matching. Prior to this patch, it also included empty prefixes and absolute filenames. But we can observe that prefix_filename() already handles these, so we don't need to detect them. 3. everything else goes to prefix_filename() I've dropped the "const" from the "char **file" parameter to indicate that we're allocating, though in practice it's not really important. This is all being shuffled through a void pointer via opt->value before it hits code which ever looks at the string. And it's even a bit weird, because we are really taking _in_ a const string and using the same out-parameter for a non-const string. A better function signature would be: static char *fix_filename(const char *prefix, const char *file); but that would mean the caller dereferences the double-pointer (and the NULL check is currently handled inside this function). So I took the path of least-change here. Note that we have to fix several callers in this commit, too, or we'll break the leak-checking tests. These are "new" leaks in the sense that they are now triggered by the test suite, but these spots have always been leaky when Git is run in a subdirectory of the repository. I fixed all of the cases that trigger with GIT_TEST_PASSING_SANITIZE_LEAK. There may be others in scripts that have other leaks, but we can fix them later along with those other leaks (and again, you _couldn't_ fix them before this patch, so this is the necessary first step). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com> |
||
---|---|---|
.. | ||
.gitignore | ||
test-advise.c | ||
test-bitmap.c | ||
test-bloom.c | ||
test-bundle-uri.c | ||
test-cache-tree.c | ||
test-chmtime.c | ||
test-config.c | ||
test-crontab.c | ||
test-csprng.c | ||
test-ctype.c | ||
test-date.c | ||
test-delta.c | ||
test-dir-iterator.c | ||
test-drop-caches.c | ||
test-dump-cache-tree.c | ||
test-dump-fsmonitor.c | ||
test-dump-split-index.c | ||
test-dump-untracked-cache.c | ||
test-example-decorate.c | ||
test-fake-ssh.c | ||
test-fast-rebase.c | ||
test-fsmonitor-client.c | ||
test-genrandom.c | ||
test-genzeros.c | ||
test-getcwd.c | ||
test-hash-speed.c | ||
test-hash.c | ||
test-hashmap.c | ||
test-hexdump.c | ||
test-index-version.c | ||
test-json-writer.c | ||
test-lazy-init-name-hash.c | ||
test-match-trees.c | ||
test-mergesort.c | ||
test-mktemp.c | ||
test-oid-array.c | ||
test-oidmap.c | ||
test-oidtree.c | ||
test-online-cpus.c | ||
test-pack-mtimes.c | ||
test-parse-options.c | ||
test-parse-pathspec-file.c | ||
test-partial-clone.c | ||
test-path-utils.c | ||
test-pcre2-config.c | ||
test-pkt-line.c | ||
test-prio-queue.c | ||
test-proc-receive.c | ||
test-progress.c | ||
test-reach.c | ||
test-read-cache.c | ||
test-read-graph.c | ||
test-read-midx.c | ||
test-ref-store.c | ||
test-reftable.c | ||
test-regex.c | ||
test-repository.c | ||
test-revision-walking.c | ||
test-rot13-filter.c | ||
test-run-command.c | ||
test-scrap-cache-tree.c | ||
test-serve-v2.c | ||
test-sha1.c | ||
test-sha1.sh | ||
test-sha256.c | ||
test-sigchain.c | ||
test-simple-ipc.c | ||
test-strcmp-offset.c | ||
test-string-list.c | ||
test-submodule-config.c | ||
test-submodule-nested-repo-config.c | ||
test-submodule.c | ||
test-subprocess.c | ||
test-tool-utils.h | ||
test-tool.c | ||
test-tool.h | ||
test-trace2.c | ||
test-urlmatch-normalization.c | ||
test-userdiff.c | ||
test-wildmatch.c | ||
test-windows-named-pipe.c | ||
test-write-cache.c | ||
test-xml-encode.c |