From 6d83c91d498ac8daaedcd931cc2de1b1dc55cb40 Mon Sep 17 00:00:00 2001 From: Mickael Carl Date: Thu, 6 Mar 2025 23:31:09 +0000 Subject: [PATCH] Fix an out-of-bounds array access when formatting some empty comment --- formatter/testdata/empty_comment.fmt.golden | 2 ++ formatter/testdata/empty_comment.jsonnet | 2 ++ internal/formatter/enforce_comment_style.go | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 formatter/testdata/empty_comment.fmt.golden create mode 100644 formatter/testdata/empty_comment.jsonnet diff --git a/formatter/testdata/empty_comment.fmt.golden b/formatter/testdata/empty_comment.fmt.golden new file mode 100644 index 0000000..13e4943 --- /dev/null +++ b/formatter/testdata/empty_comment.fmt.golden @@ -0,0 +1,2 @@ +// +{} diff --git a/formatter/testdata/empty_comment.jsonnet b/formatter/testdata/empty_comment.jsonnet new file mode 100644 index 0000000..4470772 --- /dev/null +++ b/formatter/testdata/empty_comment.jsonnet @@ -0,0 +1,2 @@ +# +{} diff --git a/internal/formatter/enforce_comment_style.go b/internal/formatter/enforce_comment_style.go index f508e9a..f8302a6 100644 --- a/internal/formatter/enforce_comment_style.go +++ b/internal/formatter/enforce_comment_style.go @@ -38,7 +38,7 @@ func (c *EnforceCommentStyle) FodderElement(p pass.ASTPass, element *ast.FodderE *comment = "#" + (*comment)[2:] } if c.Options.CommentStyle == CommentStyleSlash && (*comment)[0] == '#' { - if !c.seenFirstFodder && (*comment)[1] == '!' { + if !c.seenFirstFodder && len(*comment) > 1 && (*comment)[1] == '!' { return } *comment = "//" + (*comment)[1:]