Jeff King 6d110c4afa read_gitfile_gently(): return non-repo path on error
This patch fixes a potential segfault when resolving a .git file that
points to an invalid path. The bug was introduced by 1dd27bfbfd (setup:
improve error diagnosis for invalid .git files, 2026-03-04).

In setup_git_directory_gently() we call read_gitfile_gently(), which may
return a numeric code to us on error. If die_on_error is set, we then
feed that code to read_gitfile_error_die(), which also wants the path to
the gitfile and, in the case of ERROR_NOT_A_REPO, the non-repo directory
that the gitfile pointed to.

But we don't have that pointed-to directory available, so we just pass
NULL. That ends up calling die("not a git repository: %s", NULL). This
may crash, though on many systems (like glibc) it will just print
"(null)". So even if we don't crash, we're generating nonsense output.

The problem comes from 1dd27bfbfd. Before that, when die_on_error was
set we'd pass NULL to read_gitfile_gently()'s return_error_code
parameter, which means it would call read_gitfile_error_die() itself.
And it _does_ have that pointed-to directory as a string, and correctly
passes it.  But since 1dd27bfbfd, we always get the numeric error code
back from read_gitfile_gently(), and then decide whether to call
read_gitfile_error_die() in the caller. And since we don't have the
"dir" parameter, we just pass NULL.

Unfortunately the fix is not a simple matter of passing the string to
the right function. We have to get it out of read_gitfile_gently() in
the first place, which means we have to return it as another
out-parameter. And because it involves allocating memory, we can't just
do so unconditionally; callers need to be ready to free it after
handling the error.

I've tried to make the minimally-invasive fix here:

  1. We only copy the string when we hit READ_GITFILE_ERR_NOT_A_REPO,
     so other error codes don't have to worry about freeing it.

  2. We'll turn read_gitfile_gently() into a wrapper which passes NULL
     by default, leaving other callers unaffected.

The result is kind of gross. There's an extra layer of macro
indirection, and the validity of the string is subtly tied to the
NOT_A_REPO error. A cleaner solution might be an error struct that
couples the code and the output string together, along with a function
to free the error struct. But then all callers would have to be modified
to call the free function. Alternatively, we could perhaps put a
large-ish fixed-size buffer in the struct, though that means potential
truncation and a larger stack footprint in each caller (even when they
don't have see an error).

So I've left that as possible work for the future, or maybe never. Some
of this gross-ness was already there. For example, the only other caller
of read_gitfile_error_die() is in submodule.c, and it also passes NULL
for the "dir" parameter. But it does so only when the code is not
NOT_A_REPO! So it is depending on the same subtle connection to avoid
triggering the bug.

There's an existing test in t0002 which triggers this case, but we
didn't notice the problem because it checks only that we said "not a
repository", and not the full string. So if we print "(null)" it is
happy. It will probably crash on some non-glibc platforms, but nobody
seems to have reported it yet (the breakage is recent-ish as of v2.54).
I'm also somewhat surprised that building with ASan/UBSan doesn't catch
this, but it doesn't seem to (and I found an open issue with somebody
asking for it to be implemented in the sanitizers).

We can beef up the test by checking for the full string, which does
demonstrate the bug.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-06-02 16:23:14 +09:00
2026-03-27 11:00:01 -07:00
2026-02-03 09:41:52 -08:00
2026-04-01 10:28:19 -07:00
2026-03-20 09:31:04 +08:00
2026-03-23 09:20:29 -07:00
2026-02-07 17:41:03 -08:00
2026-02-09 12:09:09 -08:00
2026-02-23 13:23:41 -08:00
2026-02-23 13:23:41 -08:00
2026-03-24 12:31:32 -07:00
2026-03-23 09:20:29 -07:00
2026-04-08 10:19:17 -07:00
2026-03-23 08:33:10 -07:00
2026-03-23 08:33:10 -07:00
2026-04-19 19:01:39 -07:00
2026-03-24 12:26:58 -07:00
2026-03-10 14:23:24 -07:00
2026-02-17 13:30:41 -08:00
2026-02-24 11:16:34 -08:00
2026-03-31 20:43:14 -07:00
2026-03-31 20:43:14 -07:00
2026-02-23 13:21:18 -08:00
2026-04-08 10:19:17 -07:00
2026-04-08 10:19:17 -07:00
2026-03-26 09:38:06 -07:00
2026-03-26 09:38:06 -07:00
2026-02-08 15:03:06 -08:00
2026-03-09 14:36:55 -07:00
2026-03-31 20:43:14 -07:00
2026-04-08 10:19:17 -07:00
2026-03-23 21:27:17 -07:00
2026-02-05 15:42:01 -08:00
2026-03-31 20:43:14 -07:00
2026-04-08 10:19:18 -07:00
2026-03-25 12:58:05 -07:00
2026-03-26 09:38:06 -07:00
2026-04-03 13:01:09 -07:00
2026-04-03 13:01:09 -07:00
2026-03-31 20:43:14 -07:00
2026-04-01 10:28:19 -07:00
2025-12-29 22:02:54 +09:00
2026-01-09 18:36:17 -08:00
2026-01-09 18:36:17 -08:00
2026-02-08 15:16:49 -08:00
2026-02-08 15:16:49 -08:00
2026-03-31 20:43:14 -07:00

Build status

Git - fast, scalable, distributed revision control system

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses, compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

Please read the file INSTALL for installation instructions.

Many Git online resources are accessible from https://git-scm.com/ including full documentation and Git related tools.

See Documentation/gittutorial.adoc to get started, then see Documentation/giteveryday.adoc for a useful minimum set of commands, and Documentation/git-<commandname>.adoc for documentation of each command. If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with man git-<commandname> or git help <commandname>.

CVS users may also want to read Documentation/gitcvs-migration.adoc (man gitcvs-migration or git help cvs-migration if git is installed).

The user discussion and development of Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read Documentation/SubmittingPatches for instructions on patch submission and Documentation/CodingGuidelines).

Those wishing to help with error message, usage and informational message string translations (localization l10) should see po/README.md (a po file is a Portable Object file that holds the translations).

To subscribe to the list, send an email to git+subscribe@vger.kernel.org (see https://subspace.kernel.org/subscribing.html for details). The mailing list archives are available at https://lore.kernel.org/git/, https://marc.info/?l=git and other archival sites.

Issues which are security relevant should be disclosed privately to the Git Security mailing list git-security@googlegroups.com.

The maintainer frequently sends the "What's cooking" reports that list the current status of various development topics to the mailing list. The discussion following them give a good reference for project status, development direction and remaining tasks.

The name "git" was given by Linus Torvalds when he wrote the very first version. He described the tool as "the stupid content tracker" and the name as (depending on your mood):

  • random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of "get" may or may not be relevant.
  • stupid. contemptible and despicable. simple. Take your pick from the dictionary of slang.
  • "global information tracker": you're in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
  • "goddamn idiotic truckload of sh*t": when it breaks
Description
A fork of Git containing Windows-specific patches.
Readme 443 MiB
2025-08-19 03:50:05 -05:00
Languages
C 50.2%
Shell 39.2%
Perl 4.3%
Tcl 3%
Python 0.8%
Other 2.3%