Skip to content

DataModel: validate constraint array lengths against the row count #2254

Open

Description

@ramakrishnap-nv

Problem

DataModel accepts two row representations: rhs + row types (set_constraint_bounds, set_row_types) and lower/upper bounds (set_constraint_lower_bounds, set_constraint_upper_bounds). The setters in data_model_wrapper.pyx store whatever array they receive. Nothing checks that its length equals the row count implied by A_offsets (len(A_offsets) - 1).

The solve path then passes each array to the C++ data_model_view with its own shape[0]. Any consumer that assumes one representation, or assumes the sizes match, can read out of bounds. #2249 is one instance, in _compute_slack_csr, which indexed rhs[i] and sense[i] when only the bounds were set. cvxpy, pyomo and linopy all use set_constraint_lower_bounds, so this path is exercised by third-party interfaces.

Proposal

Validate at the Python boundary (the setters, or the solve entry once the matrix is known):

  • rhs, row_types, constraint_lower_bounds and constraint_upper_bounds must each be empty or have length equal to the row count.
  • Lower and upper bounds must be set together or not at all.
  • Raise a clear ValueError otherwise.

Optionally, as Ishika Roy suggested, normalize so that rhs + sense is always converted to lower/upper bounds when the model is built, so consumers only deal with one representation. That is a larger change and could be a second step.

Open question

Does the C++ data_model_view already validate these sizes against the row count? That needs checking, since the Python-side check would then be a duplicate of it.

Related: #2249, #2253, #1615.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions