yoheimuta / protolint

A pluggable linter and fixer to enforce Protocol Buffer style and conventions.
MIT License
576 stars 52 forks source link

Fix indentrule causing runtime error when the file has a mix of LF an CRLF #285

Closed yoheimuta closed 2 years ago

yoheimuta commented 2 years ago

ref. https://github.com/yoheimuta/protolint/issues/280

Checked out by a previous version:

(base) ❯ protolint version
protolint version 0.39.0(20ca4d4)

(base) ❯ protolint ./_testdata/rules/indentrule/issue_280_mix_lineending.proto
panic: runtime error: index out of range [56] with length 53

goroutine 1 [running]:
github.com/yoheimuta/protolint/internal/addon/rules.indentVisitor.validateIndent({0xc000096b00, {0x160dc43, 0x2}, 0x0, 0x0, 0x0, 0xc000417170}, {{0xc0000e21f9, 0x39}, 0x59f, ...}, ...)
        /home/runner/work/protolint/protolint/internal/addon/rules/indentRule.go:304 +0x505
github.com/yoheimuta/protolint/internal/addon/rules.indentVisitor.validateIndentLeading(...)
        /home/runner/work/protolint/protolint/internal/addon/rules/indentRule.go:291
github.com/yoheimuta/protolint/internal/addon/rules.indentVisitor.VisitService({0xc000096b00, {0x160dc43, 0x2}, 0x0, 0x0, 0x0, 0xc000417170}, 0xc0000ad5e0)
        /home/runner/work/protolint/protolint/internal/addon/rules/indentRule.go:267 +0x13c
github.com/yoheimuta/protolint/linter/visitor.extendedDisableRuleVisitor.VisitService({{0x16e52e8, 0xc0004171a0}, 0xc00009e858}, 0xc0000ad5e0)
        /home/runner/work/protolint/protolint/linter/visitor/extendedDisableRuleVisitor.go:150 +0xa8
github.com/yoheimuta/go-protoparser/v4/parser.(*Service).Accept(0xc0000ad5e0, {0x16e3a10, 0xc00009e870})
        /home/runner/go/pkg/mod/github.com/yoheimuta/go-protoparser/v4@v4.6.0/parser/service.go:83 +0x39
github.com/yoheimuta/go-protoparser/v4/parser.(*Proto).Accept(0xc0004170e0, {0x16e3a10, 0xc00009e870})
        /home/runner/go/pkg/mod/github.com/yoheimuta/go-protoparser/v4@v4.6.0/parser/proto.go:26 +0x99
github.com/yoheimuta/protolint/linter/visitor.RunVisitor({0x16e52e8, 0xc0004171a0}, 0xc00009e6a8, {0x160fcab, 0x6})
        /home/runner/work/protolint/protolint/linter/visitor/hasExtendedVisitor.go:34 +0xca
github.com/yoheimuta/protolint/internal/addon/rules.IndentRule.Apply({{0x160dc43, 0x14c1aef}, 0x39, 0x0}, 0xc00030e600)
        /home/runner/work/protolint/protolint/internal/addon/rules/indentRule.go:75 +0x117
github.com/yoheimuta/protolint/internal/linter.(*Linter).Run(0x0, 0xc0002a1970, {0xc00031ba00, 0x12, 0x0})
        /home/runner/work/protolint/protolint/internal/linter/linter.go:33 +0xf3
github.com/yoheimuta/protolint/internal/cmd/subcmds/lint.(*CmdLint).runOneFile(0xc0000b0f00, {{0xc0000e21b0, 0x82}, {0xc0000e21f9, 0x39}})
        /home/runner/work/protolint/protolint/internal/cmd/subcmds/lint/cmdLint.go:138 +0x16e
github.com/yoheimuta/protolint/internal/cmd/subcmds/lint.(*CmdLint).run(0xc0000b0f00)
        /home/runner/work/protolint/protolint/internal/cmd/subcmds/lint/cmdLint.go:107 +0xb8
github.com/yoheimuta/protolint/internal/cmd/subcmds/lint.(*CmdLint).Run(0xc0000b0f00)
        /home/runner/work/protolint/protolint/internal/cmd/subcmds/lint/cmdLint.go:84 +0x4d
github.com/yoheimuta/protolint/internal/cmd.doLint({0xc00001e050, 0x0, 0x1013591}, {0x16c7060, 0xc00000e018}, {0x16c7060, 0xc00000e020})
        /home/runner/work/protolint/protolint/internal/cmd/cmd.go:110 +0x2df
github.com/yoheimuta/protolint/internal/cmd.doSub({0xc00001e050, 0x1cb7a10, 0x0}, {0x16c7060, 0xc00000e018}, {0x16c7060, 0xc00000e020})
        /home/runner/work/protolint/protolint/internal/cmd/cmd.go:70 +0x19f
github.com/yoheimuta/protolint/internal/cmd.Do({0xc00001e050, 0x60, 0x0}, {0x16c7060, 0xc00000e018}, {0x16c7060, 0xc00000e020})
        /home/runner/work/protolint/protolint/internal/cmd/cmd.go:49 +0x86
main.main()
        /home/runner/work/protolint/protolint/cmd/protolint/main.go:11 +0x6c

Checked out by this build: It's fixed.

(base) ❯ ./protolint version
protolint version v0.40.0(33bd7e3)

(base) ❯ ./protolint ./_testdata/rules/indentrule/issue_280_mix_lineending.proto
[_testdata/rules/indentrule/issue_280_mix_lineending.proto:1:1] The line length is 88, but it must be shorter than 80
[_testdata/rules/indentrule/issue_280_mix_lineending.proto:28:1] The line length is 135, but it must be shorter than 80
[_testdata/rules/indentrule/issue_280_mix_lineending.proto:47:1] The line length is 85, but it must be shorter than 80
[_testdata/rules/indentrule/issue_280_mix_lineending.proto:58:1] The line length is 85, but it must be shorter than 80
[_testdata/rules/indentrule/issue_280_mix_lineending.proto:13:1] Imports are not sorted.
[_testdata/rules/indentrule/issue_280_mix_lineending.proto:14:1] Imports are not sorted.