Fix integer overflow in LAPACKE nancheck helpers for large matrices#1294
Merged
langou merged 1 commit intoJun 12, 2026
Merged
Conversation
Offset computations like j*lda overflow 32-bit lapack_int once n = lda >= 46341, e.g. LAPACKE_dpotrf crashes in its NaN check before the factorization runs. Cast to size_t (or int64_t for the tz offsets) the same way ge/gb/tp/tf already do.
langou
reviewed
Jun 12, 2026
| for( j = st; j < n; j++ ) { | ||
| for( i = 0; i < MIN( j+1-st, lda ); i++ ) { | ||
| if( LAPACK_CISNAN( a[i+j*lda] ) ) | ||
| if( LAPACK_CISNAN( a[i+(size_t)j*lda] ) ) |
Contributor
There was a problem hiding this comment.
The use of size_t here is certainly going to have a significant impact. Thanks!
langou
reviewed
Jun 12, 2026
|
|
||
| /* Initial offsets and sizes of triangular and rectangular parts */ | ||
| lapack_int tri_offset = 0; | ||
| int64_t tri_offset = 0; |
Contributor
There was a problem hiding this comment.
And then using int64_t instead of size_t because we need to be signed (as opposed to unsigned) for this variable sounds good to me. Great work.
martin-frbg
approved these changes
Jun 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hit this while running Cholesky on a 47000x47000 matrix through OpenBLAS:
LAPACKE_dpotrfsegfaults inLAPACKE_dtr_nancheckbefore the factorization even starts, becausej*ldaoverflows 32-bitlapack_intoncen = lda >= 46341.The
ge/gb/tp/tfhelpers already cast tosize_t, but a few others were missed:?tr_nancheck:a[i+j*lda](this is the potrf path, also used by the po/sy/he checks)?_nancheck: then*incloop bound overflows when called with a large stride (e.g. from the hs/tf checks)?sp/?pp/?pf/?hp/?tp/?tf:len = n*(n+1)/2overflows, the check then silently passes NaNs through?tz_nancheck: the part offsets overflow, widened toint64_tto keep the -1 sentinelThis bug also affects OpenBLAS (it vendors these files) - identical fix in OpenMathLib/OpenBLAS#5835.
Tested with UBSan at n = 47000: the old code reports signed integer overflow and either segfaults (tr, tz) or misses a planted NaN (vector, packed); the fixed code finds the NaN in all cases. Old and new give identical results on a sweep of small sizes covering all layouts/uplo/diag/shapes with a NaN at every position.