]> git.ipfire.org Git - thirdparty/git.git/commitdiff
chainlint.sed: don't mistake `<< word` in string as here-doc operator
authorEric Sunshine <sunshine@sunshineco.com>
Mon, 13 Dec 2021 06:30:56 +0000 (01:30 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 13 Dec 2021 22:15:29 +0000 (14:15 -0800)
Tighten here-doc recognition to prevent it from being fooled by text
which looks like a here-doc operator but happens merely to be the
content of a string, such as this real-world case from t7201:

    echo "<<<<<<< ours" &&
    echo ourside &&
    echo "=======" &&
    echo theirside &&
    echo ">>>>>>> theirs"

This problem went unnoticed because chainlint.sed is not a real parser,
but rather applies heuristics to pretend to understand shell code. In
this case, it saw what it thought was a here-doc operator (`<< ours`),
and fell off the end of the test looking for the closing tag "ours"
which it never found, thus swallowed the remainder of the test without
checking it for &&-chain breakage.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/chainlint.sed
t/chainlint/not-heredoc.expect [new file with mode: 0644]
t/chainlint/not-heredoc.test [new file with mode: 0644]

index b38274652604bdeec6804ec4e429a976b55252bf..2f786f890df82ff9b82cd7904de2663337fff5b5 100644 (file)
@@ -89,6 +89,7 @@
 # here-doc -- swallow it to avoid false hits within its body (but keep the
 # command to which it was attached)
 /<<-*[         ]*[\\'"]*[A-Za-z0-9_]/ {
+       /"[^"]*<<[^"]*"/bnotdoc
        s/^\(.*\)<<-*[  ]*[\\'"]*\([A-Za-z0-9_][A-Za-z0-9_]*\)['"]*/<\2>\1<</
        s/[     ]*<<//
        :hered
        s/^<[^>]*>//
        s/\n.*$//
 }
+:notdoc
 
 # one-liner "(...) &&"
 /^[    ]*!*[   ]*(..*)[        ]*&&[   ]*$/boneline
@@ -151,8 +153,10 @@ s/.*\n//
        /"[^'"]*'[^'"]*"/!bsqstr
 }
 :folded
-# here-doc -- swallow it
-/<<-*[         ]*[\\'"]*[A-Za-z0-9_]/bheredoc
+# here-doc -- swallow it (but not "<<" in a string)
+/<<-*[         ]*[\\'"]*[A-Za-z0-9_]/{
+       /"[^"]*<<[^"]*"/!bheredoc
+}
 # comment or empty line -- discard since final non-comment, non-empty line
 # before closing ")", "done", "elsif", "else", or "fi" will need to be
 # re-visited to drop "suspect" marking since final line of those constructs
diff --git a/t/chainlint/not-heredoc.expect b/t/chainlint/not-heredoc.expect
new file mode 100644 (file)
index 0000000..2e9bb13
--- /dev/null
@@ -0,0 +1,14 @@
+echo "<<<<<<< ours" &&
+echo ourside &&
+echo "=======" &&
+echo theirside &&
+echo ">>>>>>> theirs" &&
+
+(
+       echo "<<<<<<< ours" &&
+       echo ourside &&
+       echo "=======" &&
+       echo theirside &&
+       echo ">>>>>>> theirs" ?!AMP?!
+       poodle
+) >merged
diff --git a/t/chainlint/not-heredoc.test b/t/chainlint/not-heredoc.test
new file mode 100644 (file)
index 0000000..9aa5734
--- /dev/null
@@ -0,0 +1,16 @@
+# LINT: "<< ours" inside string is not here-doc
+echo "<<<<<<< ours" &&
+echo ourside &&
+echo "=======" &&
+echo theirside &&
+echo ">>>>>>> theirs" &&
+
+(
+# LINT: "<< ours" inside string is not here-doc
+       echo "<<<<<<< ours" &&
+       echo ourside &&
+       echo "=======" &&
+       echo theirside &&
+       echo ">>>>>>> theirs"
+       poodle
+) >merged