Skip to content

Commit

Permalink
Add sanity checks to Jacobian / Hessian indices
Browse files Browse the repository at this point in the history
  • Loading branch information
chrhansk committed Sep 15, 2023
1 parent 5b089a0 commit 394665d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cyipopt/cython/ipopt_wrapper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,21 @@ cdef Bool jacobian_cb(Index n,
np_iRow = np.array(ret_val[0], dtype=DTYPEi).flatten()
np_jCol = np.array(ret_val[1], dtype=DTYPEi).flatten()

if (np_iRow.size != nele_jac) or (np_jCol.size != nele_jac):
msg = b"Invalid number of indices returned from jacobianstructure"
log(msg, logging.ERROR)
return False

if (np_iRow < 0).any() or (np_iRow >= m).any():
msg = b"Invalid row indices returned from jacobianstructure"
log(msg, logging.ERROR)
return False

if (np_jCol < 0).any() or (np_jCol >= n).any():
msg = b"Invalid column indices returned from jacobianstructure"
log(msg, logging.ERROR)
return False

for i in range(nele_jac):
iRow[i] = np_iRow[i]
jCol[i] = np_jCol[i]
Expand Down Expand Up @@ -1078,6 +1093,26 @@ cdef Bool hessian_cb(Index n,
np_iRow = np.array(ret_val[0], dtype=DTYPEi).flatten()
np_jCol = np.array(ret_val[1], dtype=DTYPEi).flatten()

if (np_iRow.size != nele_hess) or (np_jCol.size != nele_hess):
msg = b"Invalid number of indices returned from hessianstructure"
log(msg, logging.ERROR)
return False

if not(np_iRow >= np_jCol).all():
msg = b"Indices are not lower triangular in hessianstructure"
log(msg, logging.ERROR)
return False

if (np_jCol < 0).any():
msg = b"Invalid column indices returned from hessianstructure"
log(msg, logging.ERROR)
return False

if (np_iRow >= n).any():
msg = b"Invalid row indices returned from hessianstructure"
log(msg, logging.ERROR)
return False

for i in range(nele_hess):
iRow[i] = np_iRow[i]
jCol[i] = np_jCol[i]
Expand Down

0 comments on commit 394665d

Please sign in to comment.