Skip to content

levels: return "invalid" for out-of-range Level.String() - #120

Open
vulragrag-star wants to merge 1 commit into
apex:masterfrom
vulragrag-star:fix/75-invalid-level-string
Open

vulragrag-star wants to merge 1 commit into
apex:masterfrom
vulragrag-star:fix/75-invalid-level-string

Conversation

@vulragrag-star

Copy link
Copy Markdown

What I did

Level.String() indexed levelNames without bounds checks. InvalidLevel is -1, so calling String() (or MarshalJSON, which uses it) panicked. Return "invalid" for out-of-range levels instead.

Fixes #75.

How I did it

Guard l < DebugLevel || int(l) >= len(levelNames) before indexing. Matches the approach @tj already +1'd on the issue.

How to verify it

go test -count=1 ./...

Includes TestLevel_String / TestLevel_MarshalJSON_Invalid covering InvalidLevel and an out-of-range value.

InvalidLevel is -1 and indexing levelNames panics. Guard the bounds and
return "invalid" so String/MarshalJSON stay safe. Fixes apex#75.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Level.String panics if Level == InvalidLevel

1 participant