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

fix(parsers.xpath): Fix panic for JSON name expansion #12724

Merged
merged 2 commits into from
Feb 22, 2023

Conversation

srebhan
Copy link
Member

@srebhan srebhan commented Feb 22, 2023

When using name-expansion for JSON documents containing mixes of arrays and objects, the xpath parser causes a panic

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x98 pc=0x438c0b7]

goroutine 88 [running]:
github.com/influxdata/telegraf/plugins/parsers/xpath.(*jsonDocument).GetNodePath(0xc0005d6a20?, {0x5c62b80?, 0xc0005dd020?}, {0x5c62b80?, 0xc0005dc7e0}, {0x6f29010, 0x1})
        /.../telegraf/plugins/parsers/xpath/json_document.go:47 +0x117
github.com/influxdata/telegraf/plugins/parsers/xpath.(*Parser).parseQuery(_, {_, _, _}, {_, _}, {_, _}, {{0xc000e62391, 0x5}, ...})
        /.../telegraf/plugins/parsers/xpath/parser.go:400 +0x1623
github.com/influxdata/telegraf/plugins/parsers/xpath.(*Parser).Parse(0xc0005d6a20, {0xc0009f6b00, 0x385, 0x580})
        /.../telegraf/plugins/parsers/xpath/parser.go:173 +0x58c
...

The panic is caused by non-array parent nodes of array elements. Those nodes do have a Value() of nil and thus reflect.TypeOf() panics. Fix the issue by checking for nil values and handle them correctly.

The test-case is taken from https://community.influxdata.com/t/json-v2-array-parsing/28529.

@telegraf-tiger telegraf-tiger bot added fix pr to fix corresponding bug plugin/parser 1. Request for new parser plugins 2. Issues/PRs that are related to parser plugins labels Feb 22, 2023
@telegraf-tiger
Copy link
Contributor

@srebhan srebhan added ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. area/json json and json_v2 parser/serialiser related labels Feb 22, 2023
@powersj powersj merged commit 245705c into influxdata:master Feb 22, 2023
powersj pushed a commit that referenced this pull request Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/json json and json_v2 parser/serialiser related fix pr to fix corresponding bug plugin/parser 1. Request for new parser plugins 2. Issues/PRs that are related to parser plugins ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants