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 <adlternative@gmail.com> [jc: further test fixes] Signed-off-by: Junio C Hamano <gitster@pobox.com>

ZheNing Hu committed May 11, 2021 at 15:35 UTC 1e1c4c5eac972516db47394ae94a52a3b97cb41a
2 files changed +20 -1
ref-filter.c
+1 -1
@@ -1664,7 +1664,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
1664 else
1665 v->s = xstrdup("");
1666 continue;
1667 - } else if (atom->u.remote_ref.push) {
1667 + } else if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:")) {
1668 const char *branch_name;
1669 v->s = xstrdup("");
1670 if (!skip_prefix(ref->refname, "refs/heads/",
t/t6302-for-each-ref-filter.sh
+19
@@ -113,6 +113,25 @@ test_expect_success '%(color) must fail' '
113 test_must_fail git for-each-ref --format="%(color)%(refname)"
114 '
115
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 +
135 test_expect_success 'left alignment is default' '
136 cat >expect <<-\EOF &&
137 refname is refs/heads/master |refs/heads/master