Skip to content

Commit

Permalink
fix: comment string zero bytes filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
wwoytenko committed Dec 9, 2024
1 parent 1376d18 commit 34dfd68
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ func appendComment(b []byte, name string) []byte {
if name == "" {
return b
}
name = strings.Map(func(r rune) rune {
if r == '\x00' {
return -1
}
return r
}, name)
name = strings.ReplaceAll(name, `/*`, `/\*`)
name = strings.ReplaceAll(name, `*/`, `*\/`)
return append(b, fmt.Sprintf("/* %s */ ", name)...)
Expand Down
8 changes: 8 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ func Test_appendComment(t *testing.T) {
s := appendComment(res, c)
require.Equal(t, "/* /\\* comment *\\/ */ ", string(s))
})

t.Run("zero bytes", func(t *testing.T) {
var res []byte
c := string([]byte{'*', 0, 0, 0, 0, 0, '/'})

s := appendComment(res, c)
require.Equal(t, "/* *\\/ */ ", string(s))
})
}

0 comments on commit 34dfd68

Please sign in to comment.