Fix panic on short "diff --git" args#90
Merged
keegancsmith merged 2 commits intoJul 26, 2026
Merged
Conversation
parseDiffGitArgs compares the two filenames against the 'a/' and 'b/'
prefixes via first[:2] and second[:2] without a length guard, while the
sibling check just above guards len(first) >= 3. A 'diff --git' line
whose unquoted, space-containing args are shorter than two bytes (e.g.
'x ') therefore panics with a slice-bounds error, and this is reachable
from the public ParseFileDiff/ParseMultiFileDiff on malformed input.
Guard the prefix comparison with len(first) >= 2 (first and second have
equal length on this branch), returning the existing ("", "", true)
result for such unextractable-but-valid syntax.
Use the string prefix API so malformed short arguments follow the parser's existing ambiguous-input path without relying on slice bounds. Exercise the complete public parsing path because the panic is only reachable after an empty-file header is recognized. Amp-Thread-ID: https://ampcode.com/threads/T-019f9ef9-10eb-74aa-a4f3-ff804b81049d Co-authored-by: Amp <amp@ampcode.com>
keegancsmith
approved these changes
Jul 26, 2026
keegancsmith
left a comment
Member
There was a problem hiding this comment.
Thanks for the fix! I pushed a slight cleanup using strings.HasPrefix instead and added a testcase using a public API which would of paniced before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
parseDiffGitArgspanics on adiff --gitline whose unquoted, space-containing arguments are shorter than two bytes. It is reachable from the publicParseFileDiff/ParseMultiFileDiffon malformed input:In the ambiguous "unquoted filenames containing spaces" branch, the two candidate names are compared against the
a/andb/prefixes:The first check guards
len(first) >= 3before indexing, but the second slicesfirst[:2]andsecond[:2]with no length guard. When the args are short enough thatfirstis a single byte (e.g.x),first[:2]is out of range and the parser panics instead of returning cleanly.Since
go-diffis commonly used to parse diffs from untrusted or arbitrary sources, a malformed header should be handled, not crash the caller.Fix
Guard the prefix comparison with
len(first) >= 2, mirroring thelen(first) >= 3guard on the sibling check. On this branchfirstandsecondalways have equal length, so guardingfirstcoverssecondtoo. Short, unextractable args now return the existing("", "", true)result (valid syntax, no filename extracted) rather than panicking.Testing
TestParseDiffGitArgs_ShortAmbiguouscoversx,a, andab. Each panics on currentmasterand, with the fix, returns("", "", true). The existingTestParseDiffGitArgs_Success/_Unsuccessfultables still pass, andgofmt/go vetare clean.Note: a few pre-existing
TestParseMultiFileDiffHeaderstestdata cases fail in my local Windows environment on an unmodified checkout as well, so they are unrelated to this change; worth confirming on CI.