From e4e65d1247bb6f92fb1fd1ded5d0ed3486136ed3 Mon Sep 17 00:00:00 2001 From: Maksim Korotkov Date: Fri, 24 Apr 2026 22:45:00 +0300 Subject: [PATCH] promql: prevent nil pointer dereference in DurationExpr.PositionRange Fixed a bug in the `PositionRange` method where a panic occurred when `e.RHS` was nil. Previously, the code checked `if e.RHS == nil` but then immediately accessed `e.RHS.PositionRange().End`, causing a runtime error. This commit updates the logic to correctly use `e.LHS.PositionRange().End` when the RHS is missing. Fixes: ee7d5158a ("Add step(), min(a,b) and max(a,b) in promql duration expressions") Found by PostgresPro with the Svace static analyzer. Signed-off-by: Maksim Korotkov --- promql/parser/ast.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/promql/parser/ast.go b/promql/parser/ast.go index 6496095287..49608a1322 100644 --- a/promql/parser/ast.go +++ b/promql/parser/ast.go @@ -487,7 +487,7 @@ func (e *BinaryExpr) PositionRange() posrange.PositionRange { } func (e *DurationExpr) PositionRange() posrange.PositionRange { - if e.Op == STEP || e.Op == RANGE { + if e.RHS == nil && e.LHS == nil { return posrange.PositionRange{ Start: e.StartPos, End: e.EndPos, @@ -496,7 +496,7 @@ func (e *DurationExpr) PositionRange() posrange.PositionRange { if e.RHS == nil { return posrange.PositionRange{ Start: e.StartPos, - End: e.RHS.PositionRange().End, + End: e.LHS.PositionRange().End, } } if e.LHS == nil {