Skip to content

Fix panic in format() on negative replacement index - #6136

Open
semx wants to merge 1 commit into
nektos:masterfrom
semx:fix-format-negative-index
Open

Fix panic in format() on negative replacement index#6136
semx wants to merge 1 commit into
nektos:masterfrom
semx:fix-format-negative-index

Conversation

@semx

@semx semx commented Jul 25, 2026

Copy link
Copy Markdown

Description

format() panics with runtime error: index out of range [-1] when a placeholder uses a negative index, e.g. format('{-1}', 'x').

When parsing a {N} placeholder, the - and digits are collected into replacementIndex, so strconv.ParseInt("-1", 10, 32) succeeds and returns -1. The only guards were a parse error and an index that is too large (len(replaceValue) <= int(index)), which is false for a negative index. Execution then reaches replaceValue[index] with a negative index and the runtime panics.

GitHub Actions instead returns a handled expression error for such input, the same way act already handles the out-of-range case {2147483648}.

Reproduction

NewInterpeter(&EvaluationEnvironment{}, Config{}).Evaluate("format('{-1}', 'x')", DefaultStatusCheckNone)

Before: panic: runtime error: index out of range [-1]
After: returns error The following format string is invalid: '{-1}'

Fix

Reject a negative index alongside the existing parse-error guard. A test case format('{-1}', 'x') is added next to the existing {2147483648} case.

@louzt

louzt commented Jul 28, 2026

Copy link
Copy Markdown

Verified locally against act master HEAD 4f41128 — the index < 0 bounds check correctly handles format('{-1}', 'x'), returning the same controlled error as the existing {2147483648} overflow case. The new format-invalid-negative-index test case is a clean regression test. Happy to retest if you rebase or want a second reviewer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants