Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Fix panic in jsonpath func with empty string value - #17

Merged
babarot merged 2 commits into
babarot:masterfrom
dragon3:fix-jsonpath-empty-string-value
Dec 10, 2019
Merged

babarot merged 2 commits into
babarot:masterfrom
dragon3:fix-jsonpath-empty-string-value

Conversation

@dragon3

@dragon3 dragon3 commented Dec 4, 2019

Copy link
Copy Markdown
Collaborator

WHAT

This pull request fixes panic in jsonpath func with empty string value.
( also include changes by go mod tidy )

WHY

Before this change, you can get panic with empty string value.

➜  stein git:(fix-jsonpath-empty-string) ✗ make test
?       github.com/b4b4r07/stein        [no test files]
?       github.com/b4b4r07/stein/lint   [no test files]
?       github.com/b4b4r07/stein/lint/internal/policy   [no test files]
=== RUN   TestGJSONFunc
=== RUN   TestGJSONFunc/simple.json
=== RUN   TestGJSONFunc/simple_kubernetes.yaml
=== RUN   TestGJSONFunc/string_value.yaml
=== RUN   TestGJSONFunc/number_value.yaml
=== RUN   TestGJSONFunc/object_value.yaml
=== RUN   TestGJSONFunc/tuple_value.yaml
=== RUN   TestGJSONFunc/strict_string.json
=== RUN   TestGJSONFunc/strict_boolean.json
=== RUN   TestGJSONFunc/strict_boolean.json#01
=== RUN   TestGJSONFunc/strict_boolean.json#02
=== RUN   TestGJSONFunc/empty_string.json
    --- FAIL: TestGJSONFunc/empty_string_value.json (0.00s)
        jsonpath_test.go:113: unexpected error: panic in function implementation: runtime error: index out of range [0] with length 0
            goroutine 54 [running]:
            runtime/debug.Stack(0xc000166d80, 0x20, 0xc000180180)
                /usr/local/go/src/runtime/debug/stack.go:24 +0xab
            github.com/zclconf/go-cty/cty/function.errorForPanic(...)
                /Users/dragon3/pkg/mod/github.com/zclconf/go-cty@v0.0.0-20181231001355-67e3da15e430/cty/function/error.go:44
            github.com/zclconf/go-cty/cty/function.Function.Call.func1(0xc000166d80, 0xc000166da0)
                /Users/dragon3/pkg/mod/github.com/zclconf/go-cty@v0.0.0-20181231001355-67e3da15e430/cty/function/function.go:239 +0x11f
            panic(0x1348820, 0xc000180180)
                /usr/local/go/src/runtime/panic.go:679 +0x1b2
            github.com/b4b4r07/stein/lint/internal/policy/funcs.GJSONFunc.func2(0xc000184140, 0x1, 0x1, 0x13c3fa0, 0xc0000ae181, 0xc0000ae181, 0x0, 0x0, 0x104f215, 0x1577898, ...)
                /Users/dragon3/src/github.com/b4b4r07/stein/lint/internal/policy/funcs/jsonpath.go:87 +0xa7a
            github.com/zclconf/go-cty/cty/function.Function.Call(0xc00018e540, 0xc000184140, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
                /Users/dragon3/pkg/mod/github.com/zclconf/go-cty@v0.0.0-20181231001355-67e3da15e430/cty/function/function.go:243 +0x508
            github.com/b4b4r07/stein/lint/internal/policy/funcs.GJSON(0x1369512, 0x17, 0xc0000ae840, 0xa, 0xa, 0x13c3fa0, 0xc0000ae181, 0x13143a0, 0xc000148180, 0x10836cc, ...)
                /Users/dragon3/src/github.com/b4b4r07/stein/lint/internal/policy/funcs/jsonpath.go:42 +0x12d
            github.com/b4b4r07/stein/lint/internal/policy/funcs.TestGJSONFunc.func1(0xc00018a400)
                /Users/dragon3/src/github.com/b4b4r07/stein/lint/internal/policy/funcs/jsonpath_test.go:111 +0x16a
            testing.tRunner(0xc00018a400, 0xc00017c210)
                /usr/local/go/src/testing/testing.go:909 +0x19a
            created by testing.(*T).Run
                /usr/local/go/src/testing/testing.go:960 +0x652
=== RUN   TestGrep
=== RUN   TestGrep/Match
=== RUN   TestGrep/NotMatch
--- PASS: TestGrep (0.00s)
    --- PASS: TestGrep/Match (0.00s)
    --- PASS: TestGrep/NotMatch (0.00s)
=== RUN   TestWc
=== RUN   TestWc/OneLineWithCharOption
=== RUN   TestWc/OneLineWithWordOption
=== RUN   TestWc/OneLine
--- PASS: TestWc (0.00s)
    --- PASS: TestWc/OneLineWithCharOption (0.00s)
    --- PASS: TestWc/OneLineWithWordOption (0.00s)
    --- PASS: TestWc/OneLine (0.00s)
FAIL
FAIL    github.com/b4b4r07/stein/lint/internal/policy/funcs     1.537s
?       github.com/b4b4r07/stein/lint/internal/policy/loader    [no test files]
?       github.com/b4b4r07/stein/lint/internal/policy/terraform [no test files]
=== RUN   TestDuplicatedNode
--- PASS: TestDuplicatedNode (0.00s)
=== RUN   TestRemoveNotExistEdge
--- PASS: TestRemoveNotExistEdge (0.00s)
=== RUN   TestWikipedia
--- PASS: TestWikipedia (0.00s)
=== RUN   TestCycle
--- PASS: TestCycle (0.00s)
PASS
ok      github.com/b4b4r07/stein/lint/internal/topological      (cached)
?       github.com/b4b4r07/stein/pkg/logging    [no test files]
FAIL
make: *** [test] Error 1

This is because getJSON returns if the value is empty string (e.g. {"ok": ""}) then switch b[0] panic with index out of range error.

@babarot

babarot commented Dec 10, 2019

Copy link
Copy Markdown
Owner

👀

Thank you for the contribution 🙇 I'll review this soon 🙇

@babarot babarot left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Nice catch! Thank you!

@babarot
babarot merged commit 02b6f18 into babarot:master Dec 10, 2019
@babarot

babarot commented Dec 10, 2019

Copy link
Copy Markdown
Owner

Tagged and uploaded as new release: https://github.com/b4b4r07/stein/releases/tag/v0.2.11

@dragon3

dragon3 commented Dec 10, 2019

Copy link
Copy Markdown
Collaborator Author

Thanks!

@dragon3
dragon3 deleted the fix-jsonpath-empty-string-value branch December 10, 2019 10:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

⌂ 换网址