Skip to content

Commit

Permalink
fix(gitparse): don't trim filename (trufflesecurity#2201)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgmz authored Dec 14, 2023
1 parent 22ae6a7 commit e72fdb6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/gitparse/gitparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func pathFromBinaryLine(line []byte) string {
return ""
}
bRaw := sbytes[1]
return strings.TrimSpace(string(bRaw[:len(bRaw)-7])) // drop the "b/" and " differ"
return string(bRaw[:len(bRaw)-8]) // drop the "b/" and " differ\n"
}

// --- a/internal/addrs/move_endpoint_module.go
Expand Down
22 changes: 18 additions & 4 deletions pkg/gitparse/gitparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ func TestLineChecks(t *testing.T) {
MessageEndLine,
[]byte("diff --git a/pkg/sources/source_unit.go b/pkg/sources/source_unit.go"),
},
{
MessageEndLine,
[]byte("diff --git a/ Lunch and Learn - HCDiag.pdf b/ Lunch and Learn - HCDiag.pdf"),
},
{
BinaryFileLine,
[]byte("diff --git a/pkg/decoders/utf16_test.go b/pkg/decoders/utf16_test.go"),
Expand Down Expand Up @@ -292,6 +296,10 @@ func TestLineChecks(t *testing.T) {
IndexLine,
[]byte("Binary files /dev/null and b/plugin.sig differ"),
},
{
IndexLine,
[]byte("Binary files /dev/null and b/ Lunch and Learn - HCDiag.pdf differ"),
},
},
fails: []testCaseLine{
{
Expand Down Expand Up @@ -648,10 +656,16 @@ Co-authored-by: Sergey Beryozkin <[email protected]>
}

func TestBinaryPathParse(t *testing.T) {
filename := pathFromBinaryLine([]byte("Binary files /dev/null and b/plugin.sig differ"))
expected := "plugin.sig"
if filename != expected {
t.Errorf("Expected: %s, Got: %s", expected, filename)
cases := map[string]string{
"Binary files /dev/null and b/plugin.sig differ\n": "plugin.sig",
"Binary files /dev/null and b/ Lunch and Learn - HCDiag.pdf differ\n": " Lunch and Learn - HCDiag.pdf",
}

for name, expected := range cases {
filename := pathFromBinaryLine([]byte(name))
if filename != expected {
t.Errorf("Expected: %s, Got: %s", expected, filename)
}
}
}

Expand Down

0 comments on commit e72fdb6

Please sign in to comment.