fix: panic in extractKey #25
Conversation
Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
|
To generate Unit Tests for this PR, please click here. |
There was a problem hiding this comment.
Pull Request Overview
Fixes a panic in the extractKey function by adding proper bounds checking and improving key extraction logic. The fix addresses the issue where accessing string indices without length validation could cause out-of-bounds panics.
- Adds bounds checking before accessing string indices to prevent panics
- Refactors key extraction logic to be more robust with edge cases
- Introduces a helper function
unquoteKeyfor consistent key cleaning
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| } | ||
|
|
||
| // Remove a single leading +/- if present. | ||
| if line[0] == '-' || line[0] == '+' { |
There was a problem hiding this comment.
This line can still cause a panic if line is an empty string after trimming. The empty string check should be performed before accessing line[0].
| if line[0] == '-' || line[0] == '+' { | |
| if len(line) > 0 && (line[0] == '-' || line[0] == '+') { |
There was a problem hiding this comment.
good to have but this will never happen since line 223 handles that
| expectMap = map[string]interface{}{cleanExpectKey: jsonObj} | ||
|
|
There was a problem hiding this comment.
[nitpick] There's an unnecessary empty line that breaks the consistency of the switch statement formatting.
| expectMap = map[string]interface{}{cleanExpectKey: jsonObj} |
Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
Fixes a panic in the extractKey function by adding proper bounds checking and improving key extraction logic. The fix addresses the issue where accessing string indices without length validation could cause out-of-bounds panics.