Skip to content

Commit 1e1c4c5

Browse files
adlternativegitster
authored andcommitted
ref-filter: fix read invalid union member bug
used_atom.u is an union, and it has different members depending on what atom the auxiliary data the union part of the "struct used_atom" wants to record. At most only one of the members can be valid at any one time. Since the code checks u.remote_ref without even making sure if the atom is "push" or "push:" (which are only two cases that u.remote_ref.push becomes valid), but u.remote_ref shares the same storage for other members of the union, the check was reading from an invalid member, which was the bug. Modify the condition here to check whether the atom name equals to "push" or starts with "push:", to avoid reading the value of invalid member of the union. Signed-off-by: ZheNing Hu <[email protected]> [jc: further test fixes] Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6ff7f46 commit 1e1c4c5

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

ref-filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
16641664
else
16651665
v->s = xstrdup("");
16661666
continue;
1667-
} else if (atom->u.remote_ref.push) {
1667+
} else if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:")) {
16681668
const char *branch_name;
16691669
v->s = xstrdup("");
16701670
if (!skip_prefix(ref->refname, "refs/heads/",

t/t6302-for-each-ref-filter.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ test_expect_success '%(color) must fail' '
113113
test_must_fail git for-each-ref --format="%(color)%(refname)"
114114
'
115115

116+
test_expect_success '%(color:#aa22ac) must succeed' '
117+
test_when_finished rm -rf test &&
118+
git init test &&
119+
(
120+
cd test &&
121+
test_commit initial &&
122+
git branch -M main &&
123+
cat >expect <<-\EOF &&
124+
refs/heads/main
125+
refs/tags/initial
126+
EOF
127+
git remote add origin nowhere &&
128+
git config branch.main.remote origin &&
129+
git config branch.main.merge refs/heads/main &&
130+
git for-each-ref --format="%(color:#aa22ac)%(refname)" >actual &&
131+
test_cmp expect actual
132+
)
133+
'
134+
116135
test_expect_success 'left alignment is default' '
117136
cat >expect <<-\EOF &&
118137
refname is refs/heads/master |refs/heads/master

0 commit comments

Comments
 (0)