Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonnetfmt panic when encountering unescaped regex in string #551

Open
mihaitodor opened this issue Jul 13, 2021 · 3 comments · May be fixed by #724
Open

jsonnetfmt panic when encountering unescaped regex in string #551

mihaitodor opened this issue Jul 13, 2021 · 3 comments · May be fixed by #724
Labels
bug error reporting How the errors are handled (e.g. stack trace quality) release-blocker

Comments

@mihaitodor
Copy link

This issue was reported originally on the Tanka repo here, but it can be easily reproduced with jsonnetfmt.

Given this invalid jsonnet:

{
  x: {
    data: '([^:]+)(?::\d+)?',
  },
}

running jsonnetfmt test.jsonnet (latest main - 51daeb3) panics with the following stack:

panic: Badly formatted string, should have been caught in lexer.

goroutine 1 [running]:
github.com/google/go-jsonnet/internal/formatter.(*EnforceStringStyle).LiteralString(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc0000cc210, 0x0, 0x0)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/formatter/enforce_string_style.go:45 +0x1e5
github.com/google/go-jsonnet/internal/pass.(*Base).Visit(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc0000f8170, 0x0, 0x0)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:431 +0x297
github.com/google/go-jsonnet/internal/pass.(*Base).ObjectField(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc0000f80f0, 0x0, 0x0)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:160 +0x193
github.com/google/go-jsonnet/internal/pass.(*Base).ObjectFields(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc0000cc330, 0x0, 0x0)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:191 +0x76
github.com/google/go-jsonnet/internal/pass.(*Base).Object(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc0000cc2c0, 0x0, 0x0)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:331 +0x66
github.com/google/go-jsonnet/internal/pass.(*Base).Visit(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc0000f8350, 0x0, 0x0)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:435 +0x8a3
github.com/google/go-jsonnet/internal/pass.(*Base).ObjectField(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc0000f82d0, 0x0, 0x0)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:160 +0x193
github.com/google/go-jsonnet/internal/pass.(*Base).ObjectFields(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc0000cc3e0, 0x0, 0x0)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:191 +0x76
github.com/google/go-jsonnet/internal/pass.(*Base).Object(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc0000cc370, 0x0, 0x0)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:331 +0x66
github.com/google/go-jsonnet/internal/pass.(*Base).Visit(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc000096650, 0x0, 0x0)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:435 +0x8a3
github.com/google/go-jsonnet/internal/pass.(*Base).File(0xc0000d8060, 0x11ef0e0, 0xc0000d8060, 0xc000096650, 0xc0000a6090)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:461 +0x88
github.com/google/go-jsonnet/internal/formatter.visitFile(...)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/formatter/jsonnetfmt.go:133
github.com/google/go-jsonnet/internal/formatter.Format(0x7ffeefbffacb, 0xc, 0xc0000d8000, 0x2e, 0x2, 0x2, 0x1, 0x1, 0x1010001, 0x89fffff, ...)
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/formatter/jsonnetfmt.go:168 +0x5f6
main.main()
	/Users/mihaitodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/cmd/jsonnetfmt/cmd.go:283 +0x9c5

While jsonnetfmt is correct to reject this malformed code, panicking is not ideal.

@sbarzowski sbarzowski added bug error reporting How the errors are handled (e.g. stack trace quality) labels Jul 15, 2021
@sbarzowski
Copy link
Collaborator

Thanks for reporting.

The panic happens, because the transformations conceptually cannot fail. They should never be given bad input in the first place.

The easiest fix would be to add a dry run of parser.StringUnescape in the lexer:

@mihaitodor
Copy link
Author

Thank you for looking into this @sbarzowski! I haven't dug that deep into the code yet, but I did notice that the jsonnet command somehow handles this gracefully, so my initial guess was that jsonnetfmt is missing some preflight check.

jsonnet test.jsonnet
<stdin>:3:11-29 Unknown escape sequence in string literal: \d

    data: '([^:]+)(?::\d+)?',

I might try to take a stab at sending a PR, but no promises! :)

@netomi
Copy link

netomi commented Aug 12, 2023

This also seems to be a duplicate of #606

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug error reporting How the errors are handled (e.g. stack trace quality) release-blocker
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants