]> source.dussan.org Git - jgit.git/commitdiff
FooterLine: Protect from ill-formed message 47/1173047/5
authorKamil Musin <kamilm@google.com>
Tue, 5 Dec 2023 15:22:08 +0000 (16:22 +0100)
committerIvan Frade <ifrade@google.com>
Wed, 6 Dec 2023 00:00:41 +0000 (16:00 -0800)
A raw commit message has some headers and then the actual
message. RawParseUtils.commitMessage returns the start position of the
actual message, or -1 when the message is not raw. FooterLine is not
handling this -1 and throws an IndexOutOfBounds exception.

Consider than msgB can be -1 when looking for the beginning of the
last paragraph.

FooterLine javadoc and parameter talk only about "raw" but previous
code accepted non-raw messages (used mostly in unit tests) so we need
to keep this behavior.

Change-Id: I4b88c507e210fdd200a85b01665c8524ab393b00

org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/FooterLineTest.java
org.eclipse.jgit/src/org/eclipse/jgit/revwalk/FooterLine.java

index ca6fd4644ed50fba688af83c20e37389138baded..a6808f635143fa56342eb59ae1c50c6996509a5e 100644 (file)
@@ -79,6 +79,24 @@ public class FooterLineTest extends RepositoryTestCase {
                assertEquals(0, footers.size());
        }
 
+       @Test
+       public void testNoFooters_noRawMsg_SingleLineNoHeaders() {
+               String noRawMsg = "commit message with no header lines\n";
+               List<FooterLine> footers = FooterLine.fromMessage(noRawMsg);
+               assertNotNull(footers);
+               assertEquals(0, footers.size());
+       }
+
+       @Test
+       public void testOneFooter_noRawMsg_MultiParagraphNoHeaders() {
+               String noRawMsg = "subject\n\n"
+                       + "Not: footer\n\n"
+                       + "Footer: value\n";
+               List<FooterLine> footers = FooterLine.fromMessage(noRawMsg);
+               assertNotNull(footers);
+               assertEquals(1, footers.size());
+       }
+
        @Test
        public void testSignedOffBy_OneUserNoLF() {
                String msg = buildMessage("subject\n\nbody of commit\n" + "\n"
index 3968617f8f4c0b0426ba9b078c4bd3a5669f080c..d651b63afba7c71c961aa4c1e07afe24ebd3d167 100644 (file)
@@ -87,8 +87,15 @@ public final class FooterLine {
 
                // Search for the beginning of last paragraph
                int parStart = parEnd;
-               for (; parStart > msgB && (raw[parStart - 1] != '\n' || raw[parStart - 2] != '\n'); --parStart) {
-                       // empty
+               for (; parStart > msgB; --parStart) {
+                       if (parStart < 2) {
+                               // Too close to beginning: this is not a raw message
+                               parStart = 0;
+                               break;
+                       }
+                       if (raw[parStart - 1] == '\n' && raw[parStart - 2] == '\n') {
+                               break;
+                       }
                }
 
                for (int ptr = parStart; ptr < parEnd;) {
@@ -101,7 +108,8 @@ public final class FooterLine {
                        }
 
                        // Skip over the ': *' at the end of the key before the value.
-                       int valStart, valEnd;
+                       int valStart;
+                       int valEnd;
                        for (valStart = keyEnd + 1; valStart < raw.length
                                        && raw[valStart] == ' '; ++valStart) {
                                // empty