Skip to content

Commit 60ae5a6

Browse files
kjgormandominikh
authored andcommitted
facts: handle overlapping prefix/suffix
We have a generated file which starts with the comment: ``` // Code generated DO NOT EDIT. ``` This causes staticcheck to panic, because we have an overlapping prefix and suffix when checking for these magic strings, i.e.: ``` "// Code generated " " DO NOT EDIT." ``` Trying to take a slice where end is before start subsequently panics. Add an explicit check to avoid the out of bounds access. Closes: gh-716 [via git-merge-pr] (cherry picked from commit dce81a6)
1 parent e6faca5 commit 60ae5a6

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

facts/generated.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ func isGenerated(path string) (Generator, bool) {
4646
s = bytes.TrimSuffix(s, crnl)
4747
s = bytes.TrimSuffix(s, nl)
4848
if bytes.HasPrefix(s, prefix) && bytes.HasSuffix(s, suffix) {
49+
if len(s)-len(suffix) < len(prefix) {
50+
return Unknown, true
51+
}
52+
4953
text := string(s[len(prefix) : len(s)-len(suffix)])
5054
switch text {
5155
case "by goyacc.":

0 commit comments

Comments
 (0)