Skip to content

Reject shapes with more than INT_MAX entries - #38

Merged
dance858 merged 1 commit into
mainfrom
reject-oversized-shapes
Nov 13, 2026
Merged

dance858 merged 1 commit into
mainfrom
reject-oversized-shapes

Conversation

@dance858

Copy link
Copy Markdown
Collaborator

The engine stores each expression's d1, d2 and size as C int. A shape with more than 2**31 - 1 entries wrapped around and crashed in compile(). For example, sp.broadcast(x, (65536, 65537)) hit a bus error in new_broadcast / problem_init_derivatives.

This PR checks d1·d2 in _check_shape, which covers Variable, Parameter, Constant, broadcast, reshape and index_flat, and in Node.__init__, which covers derived shapes like matmul and stacking. A ValueError is now raised on the line that creates the oversized shape, before anything reaches C. It adds test_too_many_entries.

Part of #37 (item 1). It doesn't check the scope-wide counts (total variables, total parameters, summed constraint size) or the engine's nnz.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@dance858
dance858 merged commit 1d6ebea into main Nov 13, 2026
17 of 25 checks passed
dance858 added a commit that referenced this pull request Nov 15, 2026
* Limit sparse constants by nonzeros, not rows times columns

A scipy.sparse constant is passed to the engine as CSR, so only its
dimensions and nonzero count must fit in a C int. The check from #38
rejected it by rows times columns, which ruled out e.g. the 126015 x
78478 branch incidence matrix of a 78k-bus power network. Such a
constant can be the matrix of a matmul or quad_form; any use that needs
it dense is still rejected, now when the expression is compiled.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012aJMgvtfPYcdJMWR6ox56t

* Explain when a constant becomes a dense engine parameter

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012aJMgvtfPYcdJMWR6ox56t

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant