Fix parsing of mode lines with trailing space (#38)

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.
This commit is contained in:
Billy Keyes
2022-12-26 17:56:30 -05:00
committed by GitHub
parent 1575e0c4ef
commit 981bc4b60c
2 changed files with 23 additions and 2 deletions
+2 -2
View File
@@ -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
}
+21
View File
@@ -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,
@@ -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,
@@ -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{