Skip to content

fix: return 'invalid' from Level.String instead of panicking on InvalidLevel#119

Open
SAY-5 wants to merge 1 commit intoapex:masterfrom
SAY-5:fix/level-string-invalid-75
Open

fix: return 'invalid' from Level.String instead of panicking on InvalidLevel#119
SAY-5 wants to merge 1 commit intoapex:masterfrom
SAY-5:fix/level-string-invalid-75

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 21, 2026

What

Fixes #75.

Level.String indexed levelNames directly - a fixed array keyed by the DebugLevel..FatalLevel block. An InvalidLevel value (-1, e.g. from a zero-value pointer or a failed ParseLevel that the caller did not error-check) therefore sliced levelNames[-1] and produced:

panic: runtime error: index out of range [-1]

MarshalJSON wraps String, so any JSON encoder touching a Level field that was never explicitly set crashes the host process instead of surfacing a useful error.

Fix

Bounds-check l against the valid Debug..Fatal span and return "invalid" for anything outside it. Valid Debug..Fatal values keep their existing names byte-for-byte.

Verification

Locally on macOS, go 1.26.2:

  • gofmt -s -l levels.go: clean
  • go vet ./...: clean
  • go test -race -count=1 ./...: pass across all handlers

Closes #75

…idLevel

Level.String indexed levelNames directly, which is a fixed array
keyed by the DebugLevel..FatalLevel block. An InvalidLevel value
(-1, e.g. from a zero-value pointer or a failed ParseLevel that
the caller did not error-check) therefore sliced
levelNames[-1] and produced 'index out of range'. MarshalJSON
wraps String, so any JSON encoder touching a Level field that was
never explicitly set crashes the host process instead of surfacing
a useful error (apex#75).

Bounds-check l against the valid Debug..Fatal span and return
'invalid' for anything outside it. Valid Debug..Fatal values keep
their existing names byte-for-byte.

Closes apex#75

Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
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