anz-bank / sysl

Sysl (pronounced "sizzle") is a system specification language
https://sysl.io
Apache License 2.0
122 stars 42 forks source link

Go panic when file contains mixed tabs and spaces #1078

Open akumria opened 3 years ago

akumria commented 3 years ago

Please do not post any internal, closed source snippets on this public issue tracker!

Description

If a file contains mixed tabs and spaces, validation does not return a panic but instead results in a go panic.

Steps to Reproduce

  1. Create a file and deliberately have one line contain spaces
ReportAPI:
    /api/report/:
        GET:
        ...

In this case the line ... is actually <tab><space><space><space><space>...

$ xxd restfulapi.sysl
00000000: 5265 706f 7274 4150 493a 0a20 2020 202f  ReportAPI:.    /
00000010: 6170 692f 7265 706f 7274 2f3a 0a20 2020  api/report/:.
00000020: 2020 2020 2047 4554 3a0a 0920 2020 202e       GET:..    .
00000030: 2e2e 0a                                  ...
  1. 
    $ sysl validate -v restfulapi.sysl
    DEBU[0000] Logging: {loglevel:debug verbose:true}
    DEBU[0000] Attempting to load module:restfulapi.sysl (root:)
    DEBU[0000] Parsing: restfulapi.sysl
    line 2:16 mismatched input ':' expecting {CURLY_OPEN, DIGITS, TEXT_LINE, Name, E_Name}
    INFO[0000] SyntaxError: Token: COLON
    line 4:5 missing INDENT at '...'
    INFO[0000] SyntaxError: Token: WHATEVER
    line 5:0 no viable alternative at input '<EOF>'
    panic:  [recovered]
    panic: runtime error: index out of range [-1] [recovered]
    panic: runtime error: index out of range [-1] [recovered]
    panic: runtime error: index out of range [-1]

goroutine 1 [running]: github.com/anz-bank/sysl/pkg/grammar.(SyslParser).Sysl_file.func2(0xc000b5f0c0, 0xc0006c9230) /home/runner/work/sysl/sysl/pkg/grammar/sysl_parser.go:27549 +0x145 panic(0x213b680, 0xc0000461e0) /opt/hostedtoolcache/go/1.16.7/x64/src/runtime/panic.go:965 +0x1b9 github.com/anz-bank/sysl/pkg/grammar.(SyslParser).Application.func2(0xc000b5f048, 0xc0006c9230) /home/runner/work/sysl/sysl/pkg/grammar/sysl_parser.go:26919 +0x145 panic(0x213b680, 0xc0000461e0) /opt/hostedtoolcache/go/1.16.7/x64/src/runtime/panic.go:965 +0x1b9 github.com/anz-bank/sysl/pkg/parse.(SyslParserErrorListener).SyntaxError(0xc0008eb060, 0x252e2c8, 0xc0008c2990, 0x217b4a0, 0xc000a04a10, 0x5, 0x0, 0xc0000482a0, 0x26, 0x25172f8, ...) /home/runner/work/sysl/sysl/pkg/parse/error_listener.go:24 +0x109 github.com/antlr/antlr4/runtime/Go/antlr.(ProxyErrorListener).SyntaxError(0xc000b5e820, 0x252e2c8, 0xc0008c2990, 0x217b4a0, 0xc000a04a10, 0x5, 0x0, 0xc0000482a0, 0x26, 0x25172f8, ...) /home/runner/go/pkg/mod/github.com/antlr/antlr4/runtime/!go/antlr@v0.0.0-20210521184019-c5ad59b459ec/error_listener.go:88 +0xef github.com/antlr/antlr4/runtime/Go/antlr.(BaseParser).NotifyErrorListeners(0xc0008c2990, 0xc0000482a0, 0x26, 0x252f8a0, 0xc000a04a10, 0x25172f8, 0xc000bd1950) /home/runner/go/pkg/mod/github.com/antlr/antlr4/runtime/!go/antlr@v0.0.0-20210521184019-c5ad59b459ec/parser.go:418 +0x16a github.com/antlr/antlr4/runtime/Go/antlr.(DefaultErrorStrategy).ReportNoViableAlternative(0xc000528df8, 0x253e0d0, 0xc0006c9230, 0xc000bd1950) /home/runner/go/pkg/mod/github.com/antlr/antlr4/runtime/!go/antlr@v0.0.0-20210521184019-c5ad59b459ec/error_strategy.go:264 +0xe4 github.com/antlr/antlr4/runtime/Go/antlr.(DefaultErrorStrategy).ReportError(0xc000528df8, 0x253e0d0, 0xc0006c9230, 0x25172f8, 0xc000bd1950) /home/runner/go/pkg/mod/github.com/antlr/antlr4/runtime/!go/antlr@v0.0.0-20210521184019-c5ad59b459ec/error_strategy.go:132 +0x295 github.com/anz-bank/sysl/pkg/grammar.(SyslParser).App_decl.func2(0xc000b5efd0, 0xc0006c9230) /home/runner/work/sysl/sysl/pkg/grammar/sysl_parser.go:26670 +0xe7 panic(0x20a6700, 0xc000bd1950) /opt/hostedtoolcache/go/1.16.7/x64/src/runtime/panic.go:965 +0x1b9 github.com/antlr/antlr4/runtime/Go/antlr.(ParserATNSimulator).execATN(0xc00043f500, 0xc00043ef60, 0xc00039f8f0, 0x2531b18, 0xc000448e40, 0x16, 0x2be1c718, 0xc0003e0300, 0x0) /home/runner/go/pkg/mod/github.com/antlr/antlr4/runtime/!go/antlr@v0.0.0-20210521184019-c5ad59b459ec/parser_atn_simulator.go:263 +0xe3f github.com/antlr/antlr4/runtime/Go/antlr.(ParserATNSimulator).AdaptivePredict(0xc00043f500, 0x2531b18, 0xc000448e40, 0xbf, 0x2be1c718, 0xc0003e0300, 0x0) /home/runner/go/pkg/mod/github.com/antlr/antlr4/runtime/!go/antlr@v0.0.0-20210521184019-c5ad59b459ec/parser_atn_simulator.go:146 +0x4e7 github.com/anz-bank/sysl/pkg/grammar.(SyslParser).App_decl(0xc0006c9230, 0x254c618, 0xc0003e0300) /home/runner/work/sysl/sysl/pkg/grammar/sysl_parser.go:26790 +0x38b github.com/anz-bank/sysl/pkg/grammar.(SyslParser).Application(0xc0006c9230, 0x25403a8, 0xc0003f2438) /home/runner/work/sysl/sysl/pkg/grammar/sysl_parser.go:26949 +0x2f1 github.com/anz-bank/sysl/pkg/grammar.(SyslParser).Sysl_file(0xc0006c9230, 0x2549980, 0xc000bfb770) /home/runner/work/sysl/sysl/pkg/grammar/sysl_parser.go:27573 +0x27e github.com/anz-bank/sysl/pkg/parse.parseString(0x7ffeefbff9c1, 0xf, 0x252d630, 0xc0008a8ea0, 0x0, 0x0, 0x0, 0x0) /home/runner/work/sysl/sysl/pkg/parse/parse.go:62 +0x272 github.com/anz-bank/sysl/pkg/parse.(Parser).Parse(0xc000b5f760, 0x7ffeefbff9c1, 0xf, 0x250fda8, 0xc0008a8e70, 0x0, 0x1e8cece, 0x252ebb0) /home/runner/work/sysl/sysl/pkg/parse/parse.go:197 +0xca5 github.com/anz-bank/sysl/pkg/parse.(Parser).ParseFromFs(0xc000c1f760, 0x7ffeefbff9c1, 0xf, 0x252e4a8, 0xc0004600c0, 0xc000c1f780, 0x2, 0x2) /home/runner/work/sysl/sysl/pkg/parse/parse.go:143 +0xa5 github.com/anz-bank/sysl/pkg/parse.LoadAndGetDefaultApp(0x7ffeefbff9c1, 0xf, 0x252e4a8, 0xc0004600c0, 0xc000c1f760, 0x252ebb0, 0x30c0080, 0xc0000c23f0, 0x0, 0x0) /home/runner/work/sysl/sysl/pkg/parse/parse.go:833 +0x57 github.com/anz-bank/sysl/pkg/loader.LoadSyslModule(0x0, 0x0, 0x7ffeefbff9c1, 0xf, 0x252ebb0, 0x30c0080, 0xc0000c23f0, 0x1, 0x1, 0x2, ...) /home/runner/work/sysl/sysl/pkg/loader/loader.go:35 +0x35a main.(cmdRunner).Run(0xc000182540, 0x22523b4, 0x8, 0x252ebb0, 0x30c0080, 0xc0000c23f0, 0x0, 0x0) /home/runner/work/sysl/sysl/cmd/sysl/cmd_runner.go:39 +0x238 main.main3(0xc00003a080, 0x4, 0x4, 0x252ebb0, 0x30c0080, 0xc0000c23f0, 0x2f549e0, 0x30c4b08) /home/runner/work/sysl/sysl/cmd/sysl/sysl.go:43 +0x305 main.main2(0xc00003a080, 0x4, 0x4, 0x252ebb0, 0x30c0080, 0xc0000c23f0, 0x23a26e0, 0xc00009a058) /home/runner/work/sysl/sysl/cmd/sysl/sysl.go:86 +0x8c main.main() /home/runner/work/sysl/sysl/cmd/sysl/sysl.go:114 +0x79


### Expected behaviour

A validation error occurs (if tab / space mixture is disallowed) or no validation problem ensues.

### Actual behaviour

Go panics.

### Your Environment
```sh
$ sysl info
Build:
  Version      : v0.438.0
  Git Commit   : 3550ba28c3dfd16c874be629b8f1f0f3a82a15cf
  Date         : 2021-08-23T00:12:23Z
  Go Version   : go1.16.7 linux/amd64
  OS           : darwin/amd64