Skip to content

Commit

Permalink
Fix parsing of mode lines with trailing space (#38)
Browse files Browse the repository at this point in the history
If a patch is passed through a system that converts line endings to
'\r\n', mode lines end up with trailing whitespace that confuses
strconv.ParseInt. In Git, this is avoided by using strtoul() for parsing,
which stops at the first non-digit character.

Changing line endings in patch content itself will cause other problems
so it is best to avoid this transform, but if it does happen, it
shouldn't cause a parse error.
  • Loading branch information
bluekeyes authored Dec 26, 2022
1 parent 1575e0c commit 981bc4b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gitdiff/file_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,12 @@ func parseGitHeaderNewName(f *File, line, defaultName string) error {
}

func parseGitHeaderOldMode(f *File, line, defaultName string) (err error) {
f.OldMode, err = parseMode(line)
f.OldMode, err = parseMode(strings.TrimSpace(line))
return
}

func parseGitHeaderNewMode(f *File, line, defaultName string) (err error) {
f.NewMode, err = parseMode(line)
f.NewMode, err = parseMode(strings.TrimSpace(line))
return
}

Expand Down
21 changes: 21 additions & 0 deletions gitdiff/file_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,12 @@ func TestParseGitHeaderData(t *testing.T) {
OldMode: os.FileMode(0100644),
},
},
"oldModeWithTrailingSpace": {
Line: "old mode 100644\r\n",
OutputFile: &File{
OldMode: os.FileMode(0100644),
},
},
"invalidOldMode": {
Line: "old mode rw\n",
Err: true,
Expand All @@ -496,6 +502,12 @@ func TestParseGitHeaderData(t *testing.T) {
NewMode: os.FileMode(0100755),
},
},
"newModeWithTrailingSpace": {
Line: "new mode 100755\r\n",
OutputFile: &File{
NewMode: os.FileMode(0100755),
},
},
"invalidNewMode": {
Line: "new mode rwx\n",
Err: true,
Expand All @@ -518,6 +530,15 @@ func TestParseGitHeaderData(t *testing.T) {
IsNew: true,
},
},
"newFileModeWithTrailingSpace": {
Line: "new file mode 100755\r\n",
DefaultName: "dir/file.txt",
OutputFile: &File{
NewName: "dir/file.txt",
NewMode: os.FileMode(0100755),
IsNew: true,
},
},
"copyFrom": {
Line: "copy from dir/file.txt\n",
OutputFile: &File{
Expand Down

0 comments on commit 981bc4b

Please sign in to comment.