diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-23 22:41:14 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-23 22:41:14 +0300 |
| commit | a12987997b682854d3c91705002d74928d8cb5e6 (patch) | |
| tree | 8fb162e05cab452b75ec96c32846676d6948313a | |
| parent | d1d9dbeafc5cf51497df4fe7886dbb4d43b01777 (diff) | |
fix: prevent stack corruption in Divide() on numerator pop failure
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.
| -rw-r--r-- | internal/rpn/operations_arithmetic.go | 7 |
1 files changed, 1 insertions, 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) |
