From 25a1af26e453e70a704b911e08c84562e5676b15 Mon Sep 17 00:00:00 2001 From: dance858 Date: Fri, 13 Nov 2026 08:26:47 -0800 Subject: [PATCH] Reject shapes with more than INT_MAX entries The engine stores expression sizes as C int, so a shape like (65536, 65537) wrapped and segfaulted in compile(). Check d1*d2 in _check_shape and Node.__init__ so the error is raised where the shape is created. Part of #37 (item 1). Co-Authored-By: Claude Opus 5.5 --- sparsediffpy/_expression.py | 15 ++++++++++++++- tests/test_api.py | 12 ++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/sparsediffpy/_expression.py b/sparsediffpy/_expression.py index 535b318..c94d131 100644 --- a/sparsediffpy/_expression.py +++ b/sparsediffpy/_expression.py @@ -23,7 +23,19 @@ def _check_shape(d1, d2): raise TypeError(f"Shape dimensions must be integers, got ({d1!r}, {d2!r})") if d1 <= 0 or d2 <= 0: raise ValueError(f"Shape dimensions must be positive, got ({d1}, {d2})") - return (int(d1), int(d2)) + shape = (int(d1), int(d2)) + _check_size(shape) + return shape + + +_MAX_SIZE = 2**31 - 1 # the engine stores sizes as C int + + +def _check_size(shape): + """Raise if a shape has more entries than the engine can index.""" + if shape[0] * shape[1] > _MAX_SIZE: + raise ValueError(f"Shape {shape} has {shape[0] * shape[1]} entries; " + f"the engine supports at most {_MAX_SIZE}") class Expression: @@ -101,6 +113,7 @@ def __init__(self, make, args, shape): self.make = make self.args = tuple(args) self.shape = tuple(shape) + _check_size(self.shape) class Constant(Expression): diff --git a/tests/test_api.py b/tests/test_api.py index 99263a9..d5acd6d 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -72,6 +72,18 @@ def test_invalid_shapes(scope, shape): scope.Variable(*shape) +def test_too_many_entries(scope): + """Sizes are C ints in the engine; larger shapes must fail before reaching C.""" + big = (65536, 65537) # 2**32 + 65536 entries + x = scope.Variable(1, 1) + a, b = scope.Variable(65536, 1), scope.Variable(1, 65537) + for make in (lambda: scope.Variable(*big), lambda: scope.Parameter(*big), + lambda: sp.broadcast(x, big), lambda: a @ b): + with pytest.raises(ValueError, match="at most"): + make() + assert sp.broadcast(x, (2**31 - 1, 1)).size == 2**31 - 1 + + def test_reevaluates_at_new_values(scope): x = scope.Variable(2, 1) fn = sp.compile(sp.exp(x))