threads / patch / 60192

patchvar: avoid a segmentation fault when `HOME` is unset

Subject: [PATCH] var: avoid a segmentation fault when `HOME` is unset

## tl;dr

3 messages between Sep 4, 2023 and Sep 5, 2023. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Johannes Schindelin via GitGitGadget· Sep 4, 2023, 06:21 UTC · lore
From: Johannes Schindelin <johannes.schindelin@gmx.de>

The code introduced in 576a37fccbf (var: add attributes files locations, 2023-06-27) paid careful attention to use `xstrdup()` for pointers known never to be `NULL`, and `xstrdup_or_null()` otherwise.

One spot was missed, though: `git_attr_global_file()` can return `NULL`, when the `HOME` variable is not set (and neither `XDG_CONFIG_HOME`), a scenario not too uncommon in certain server scenarios.

Fix this, and add a test case to avoid future regressions.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
    var: avoid a segmentation fault when HOME is unset
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1580%2Fdscho%2Favoid-segfault-in-git-var-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1580/dscho/avoid-segfault-in-git-var-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1580
 builtin/var.c      | 2 +-
 t/t0007-git-var.sh | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
Show changes to 2 files +10 −1

builtin/var.c, t/t0007-git-var.sh

diff --git a/builtin/var.c b/builtin/var.c
index 74161bdf1c6..8cf7dd9e2e5 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -66,7 +66,7 @@ static char *git_attr_val_system(int ident_flag UNUSED)
 
 static char *git_attr_val_global(int ident_flag UNUSED)
 {
-	char *file = xstrdup(git_attr_global_file());
+	char *file = xstrdup_or_null(git_attr_global_file());
 	if (file) {
 		normalize_path_copy(file, file);
 		return file;
diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh
index 8cb597f99c4..ff4fd9348cc 100755
--- a/t/t0007-git-var.sh
+++ b/t/t0007-git-var.sh
@@ -268,4 +268,13 @@ test_expect_success 'listing and asking for variables are exclusive' '
 	test_must_fail git var -l GIT_COMMITTER_IDENT
 '
 
+test_expect_success '`git var -l` works even without HOME' '
+	(
+		XDG_CONFIG_HOME= &&
+		export XDG_CONFIG_HOME &&
+		unset HOME &&
+		git var -l
+	)
+'
+
 test_done

base-commit: 43c8a30d150ecede9709c1f2527c8fba92c65f40
-- 
gitgitgadget
brian m. carlson· Sep 4, 2023, 20:26 UTC · re: Johannes Schindelin via GitGitGadget · lore

Re: [PATCH] var: avoid a segmentation fault when `HOME` is unset

On 2023-09-04 at 06:21:26, Johannes Schindelin via GitGitGadget wrote:
Show 11 quoted lines
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
> 
> The code introduced in 576a37fccbf (var: add attributes files locations,
> 2023-06-27) paid careful attention to use `xstrdup()` for pointers known
> never to be `NULL`, and `xstrdup_or_null()` otherwise.
> 
> One spot was missed, though: `git_attr_global_file()` can return `NULL`,
> when the `HOME` variable is not set (and neither `XDG_CONFIG_HOME`), a
> scenario not too uncommon in certain server scenarios.
> 
> Fix this, and add a test case to avoid future regressions.

Looks good to me. Thanks for the patch, and my apologies for the oversight.

-- 
brian m. carlson (he/him or they/them)
Toronto, Ontario, CA
Johannes Schindelin· Sep 5, 2023, 10:58 UTC · re: brian m. carlson · lore

Re: [PATCH] var: avoid a segmentation fault when `HOME` is unset

Hi brian,
On Mon, 4 Sep 2023, brian m. carlson wrote:
Show 14 quoted lines
> On 2023-09-04 at 06:21:26, Johannes Schindelin via GitGitGadget wrote:
> > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> >
> > The code introduced in 576a37fccbf (var: add attributes files locations,
> > 2023-06-27) paid careful attention to use `xstrdup()` for pointers known
> > never to be `NULL`, and `xstrdup_or_null()` otherwise.
> >
> > One spot was missed, though: `git_attr_global_file()` can return `NULL`,
> > when the `HOME` variable is not set (and neither `XDG_CONFIG_HOME`), a
> > scenario not too uncommon in certain server scenarios.
> >
> > Fix this, and add a test case to avoid future regressions.
>
> Looks good to me.
Thank you for the review.

Ciao, Johannes

← back to recent threads