From a12987997b682854d3c91705002d74928d8cb5e6 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 23 May 2026 22:41:14 +0300 Subject: fix: prevent stack corruption in Divide() on numerator pop failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Divide() popped the denominator (b) first, validated it, then popped the numerator (a). If the second pop failed (stack had only 1 element), the denominator was already removed — leaving the stack corrupted. Now uses popTwo() to atomically pop both operands before any validation, matching how other binary operators (Add, Subtract, Multiply, etc.) handle the error case. --- internal/rpn/operations_arithmetic.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/internal/rpn/operations_arithmetic.go b/internal/rpn/operations_arithmetic.go index 96676b8..8504d34 100644 --- a/internal/rpn/operations_arithmetic.go +++ b/internal/rpn/operations_arithmetic.go @@ -107,7 +107,7 @@ func (o *Operations) Multiply(stack *Stack) error { // Divide pops two values from stack, divides (a / b), and pushes result. func (o *Operations) Divide(stack *Stack) error { - b, err := popStack(stack, "/") + a, b, err := popTwo(stack, "/") if err != nil { return err } @@ -120,11 +120,6 @@ func (o *Operations) Divide(stack *Stack) error { return buildError("/", fmt.Errorf("division by zero")) } - a, err := popStack(stack, "/") - if err != nil { - return err - } - aM, err := resolveMetric(o.metricRegistry, a) if err != nil { return buildError("/", err) -- cgit v1.2.3