]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-filter: fix read invalid union member bug
authorZheNing Hu <adlternative@gmail.com>
Tue, 11 May 2021 15:35:20 +0000 (15:35 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 11 May 2021 23:13:14 +0000 (08:13 +0900)
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>
ref-filter.c
t/t6302-for-each-ref-filter.sh

index bf7b70299b43b1145c22b353f78eed1ec372f023..a6c0804f4047e2a132755ce3bf245567bef5292d 100644 (file)
@@ -1664,7 +1664,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
                        else
                                v->s = xstrdup("");
                        continue;
-               } else if (atom->u.remote_ref.push) {
+               } else if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:")) {
                        const char *branch_name;
                        v->s = xstrdup("");
                        if (!skip_prefix(ref->refname, "refs/heads/",
index 35408d53fd8af5d627326dd2a47e532d64a4c304..529ca556c8b4a097c0430d365b883f1d47ac824d 100755 (executable)
@@ -113,6 +113,25 @@ test_expect_success '%(color) must fail' '
        test_must_fail git for-each-ref --format="%(color)%(refname)"
 '
 
+test_expect_success '%(color:#aa22ac) must succeed' '
+       test_when_finished rm -rf test &&
+       git init test &&
+       (
+               cd test &&
+               test_commit initial &&
+               git branch -M main &&
+               cat >expect <<-\EOF &&
+               refs/heads/main
+               refs/tags/initial
+               EOF
+               git remote add origin nowhere &&
+               git config branch.main.remote origin &&
+               git config branch.main.merge refs/heads/main &&
+               git for-each-ref --format="%(color:#aa22ac)%(refname)" >actual &&
+               test_cmp expect actual
+       )
+'
+
 test_expect_success 'left alignment is default' '
        cat >expect <<-\EOF &&
        refname is refs/heads/master  |refs/heads/master