Comfy-Org/ComfyUI · warning · ValueError

Exponent {exp} exceeds maximum allowed ({MAX_EXPONENT})

Error message

Exponent {exp} exceeds maximum allowed ({MAX_EXPONENT})

What it means

Raised by the _safe_pow wrapper exposed as pow() in the math-expression node's function table. simpleeval's own safe_power already caps the ** operator; pow() as a callable would bypass that guard, so this wrapper rejects any exponent with absolute value over MAX_EXPONENT (4000) to prevent multi-minute hangs / DoS from astronomical exponents.

Source

Thrown at comfy_extras/nodes_math.py:34

MAX_EXPONENT = 4000


def _variadic_sum(*args):
    """Support both sum(values) and sum(a, b, c)."""
    if len(args) == 1 and hasattr(args[0], "__iter__"):
        return sum(args[0])
    return sum(args)


def _safe_pow(base, exp):
    """Wrap pow() with an exponent cap to prevent DoS via huge exponents.

    The ** operator is already guarded by simpleeval's safe_power, but
    pow() as a callable bypasses that guard.
    """
    if abs(exp) > MAX_EXPONENT:
        raise ValueError(f"Exponent {exp} exceeds maximum allowed ({MAX_EXPONENT})")
    return pow(base, exp)


MATH_FUNCTIONS = {
    "sum": _variadic_sum,
    "min": min,
    "max": max,
    "abs": abs,
    "round": round,
    "pow": _safe_pow,
    "sqrt": math.sqrt,
    "ceil": math.ceil,
    "floor": math.floor,
    "log": math.log,
    "log2": math.log2,
    "log10": math.log10,
    "sin": math.sin,
    "cos": math.cos,

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Restructure the math: use exp(n * log(x)) style formulations, or square repeatedly with bounded loop counts.
  2. Check whether such a huge exponent is really intended — pow(2, 4001) already overflows float64 to inf.
  3. Cap dynamic exponents before use: min(abs(e), 4000) or clamp values upstream.

Example fix

// before
pow(values[0], 99999)

// after
exp(99999 * log(values[0]))  // or reduce the exponent to a sane magnitude
Defensive patterns

Strategy: validation

Validate before calling

MAX_EXPONENT = 4000
exp = int(exp) if isinstance(exp, (int, float)) else len(exp)
assert abs(exp) <= MAX_EXPONENT, f"exponent {exp} exceeds the {MAX_EXPONENT} cap; restructure the math (exp/log) instead"

Prevention

When it happens

Trigger: Writing pow(x, 10000) or pow(x, 10**9) in the expression string; computing the exponent dynamically (pow(2, values[0]*2000)) so it exceeds 4000 at runtime; negative huge exponents like pow(2, -99999).

Common situations: Iterative-growth formulas, factorial approximations, or user typos (extra zero in the exponent) in the Math Expression node.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/692203888f74d2e7. Report an issue: GitHub.