From 7cecd41384c8b4359b76b69ba2418145216b5545 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 24 May 2026 13:40:30 +0300 Subject: refactor(rpn): extract checkStackOverflow helper in pushLiteral (#3j) Deduplicate the repeated stack overflow check in pushLiteral() by extracting it into a checkStackOverflow() helper method. Replaces three identical inline checks with calls to the new helper. --- internal/rpn/rpn_parse.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/internal/rpn/rpn_parse.go b/internal/rpn/rpn_parse.go index ca43769..8bc2b96 100644 --- a/internal/rpn/rpn_parse.go +++ b/internal/rpn/rpn_parse.go @@ -478,6 +478,14 @@ func extractVariableName(token string) string { return token } +// checkStackOverflow returns an error if the stack has reached the maximum size. +func (r *RPN) checkStackOverflow(stack *Stack) error { + if stack.Len() >= r.maxStack { + return fmt.Errorf("stack overflow") + } + return nil +} + // pushLiteral attempts to push a literal value (boolean, number, metric number, @metric) onto the stack. // Returns (true, nil) if a literal was pushed, (false, nil) if not a literal, or (false, err) on error. func (r *RPN) pushLiteral(stack *Stack, token string) (bool, error) { @@ -493,8 +501,8 @@ func (r *RPN) pushLiteral(stack *Stack, token string) (bool, error) { // Check if it's a number if num, err := strconv.ParseFloat(token, 64); err == nil { - if stack.Len() >= r.maxStack { - return false, fmt.Errorf("stack overflow") + if err := r.checkStackOverflow(stack); err != nil { + return false, err } if r.ops.GetMode() == RationalMode { rat, err := NewRatFromString(token) @@ -510,8 +518,8 @@ func (r *RPN) pushLiteral(stack *Stack, token string) (bool, error) { // Check if it's a number with a metric suffix (e.g., 100Mbps, 5.5GB, 2hr) if num, metric, ok := parseNumberWithMetric(token, r.ops.MetricRegistry()); ok { - if stack.Len() >= r.maxStack { - return false, fmt.Errorf("stack overflow") + if err := r.checkStackOverflow(stack); err != nil { + return false, err } stack.Push(NewNumberWithMetric(num, r.ops.GetMode(), metric)) return true, nil @@ -522,8 +530,8 @@ func (r *RPN) pushLiteral(stack *Stack, token string) (bool, error) { if len(token) > 1 && token[0] == '@' { metricName := token[1:] if metric, ok := r.ops.MetricRegistry().FindWithAliases(metricName); ok { - if stack.Len() >= r.maxStack { - return false, fmt.Errorf("stack overflow") + if err := r.checkStackOverflow(stack); err != nil { + return false, err } stack.Push(NewNumberWithMetric(1, r.ops.GetMode(), metric)) return true, nil -- cgit v1.2.3