From 34dfd684e371c24b9f59e9b13ef57660931f0bde Mon Sep 17 00:00:00 2001 From: Vadim Voitenko Date: Mon, 9 Dec 2024 23:50:22 +0200 Subject: [PATCH] fix: comment string zero bytes filtering --- util.go | 6 ++++++ util_test.go | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/util.go b/util.go index ff24a5463..97ed9228a 100644 --- a/util.go +++ b/util.go @@ -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)...) diff --git a/util_test.go b/util_test.go index 9b2069830..918ac5b74 100644 --- a/util_test.go +++ b/util_test.go @@ -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)) + }) }