Skip to content

Report a short diff command line as a parser error instead of crashing - #245

Merged
wumpz merged 1 commit into
java-diff-utils:masterfrom
amalsaidov:fix-reader-short-diff-line
Dec 3, 2026
Merged

wumpz merged 1 commit into
java-diff-utils:masterfrom
amalsaidov:fix-reader-short-diff-line

Conversation

@amalsaidov

Copy link
Copy Markdown
Contributor

UnifiedDiffReader.parseFileNames splits the diff command line on spaces and reads split[2] and split[3] without checking how many tokens there are. A command line with fewer than four words therefore throws ArrayIndexOutOfBoundsException from inside parseUnifiedDiff, rather than the UnifiedDiffParserException the reader uses for malformed input:

// both throw ArrayIndexOutOfBoundsException on master
UnifiedDiffReader.parseFileNames("diff a b");
UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(
        "diff a b\n--- a\n+++ b\n@@ -1 +1 @@\n-a\n+b\n".getBytes(StandardCharsets.UTF_8)));

Since DIFF_COMMAND matches any line that starts with diff followed by whitespace, any such short or free-text header line hits this path.

This change throws UnifiedDiffParserException when the line does not carry both file names, leaving the parsing of well-formed command lines (diff -u a b, diff --git a/x b/y) unchanged.

Added two regression tests in UnifiedDiffReaderTest (direct parseFileNames call and the full parseUnifiedDiff path); both fail on master with ArrayIndexOutOfBoundsException and pass with the change.

Validation on Temurin/OpenJDK: mvn -pl java-diff-utils test passes (170 run, 0 failures, 3 pre-existing skips); spotless:check and checkstyle:check report no new findings.

UnifiedDiffReader.parseFileNames read split[2] and split[3] without
checking the token count, so a diff command line with fewer than four
words (for example "diff a b") threw ArrayIndexOutOfBoundsException from
inside parseUnifiedDiff instead of the documented UnifiedDiffParserException.

Throw UnifiedDiffParserException when the line does not carry both file
names. Adds regression tests that fail before this change.
@wumpz
wumpz merged commit 52662db into java-diff-utils:master Dec 3, 2026
4 checks passed
@wumpz

wumpz commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Thx for using java-diff-utils.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants