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 <m.korotkov@postgrespro.ru>
This commit is contained in:
Maksim Korotkov 2026-04-24 22:45:00 +03:00
parent 551b5b1c56
commit e4e65d1247

View File

@ -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 {