Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out-of-bounds bug in mex code #214

Merged
merged 2 commits into from
Feb 24, 2025
Merged

Fix out-of-bounds bug in mex code #214

merged 2 commits into from
Feb 24, 2025

Conversation

mducle
Copy link
Member

@mducle mducle commented Feb 21, 2025

There was an ancient bug in the parallelised eig and chol code which failed to check the number of output variables nlhs and tries to access a pointer which does not exist.

In older versions of Matlab this seems to work (the routines get a pointer to the actual data using mxGetData given the address of an mxArray but never uses that pointer), but with Matlab 2023a and newer this causes a segmentation fault because the internal behaviour of mxGetData / the internal definition of the opaque mxArray class/struct has changed.

Specifically, the code tries to access the output pointers array plhs[] which has nlhs elements long but the code always assumes that nlhs is at lease 2. Hence when nlhs=1, plhs[1] is a junk pointer. I suspect that in older versions of Matlab, mxGetData just returns the input pointer plus an offset, which will work with a junk input pointer as long as no processing is done on the (junk) output. I think that in newer versions of Matlab, mxGetData actually tries to dereference the mxArray structure to access its pr member (the data pointer), and this will give a segfault because the input pointer is junk.

The PR now checks what nlhs is and guards against this out-of-bounds error.

@codecov-commenter
Copy link

codecov-commenter commented Feb 21, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 43.29%. Comparing base (7e87dee) to head (a2bf25c).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #214   +/-   ##
=======================================
  Coverage   43.29%   43.29%           
=======================================
  Files         243      243           
  Lines       16383    16383           
=======================================
  Hits         7093     7093           
  Misses       9290     9290           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented Feb 21, 2025

Test Results

    4 files  ±0    124 suites  ±0   4m 59s ⏱️ - 1m 32s
  831 tests ±0    813 ✅ ±0  18 💤 ±0  0 ❌ ±0 
2 324 runs  ±0  2 288 ✅ ±0  36 💤 ±0  0 ❌ ±0 

Results for commit a2bf25c. ± Comparison against base commit 7e87dee.

♻️ This comment has been updated with latest results.

Copy link
Collaborator

@RichardWaiteSTFC RichardWaiteSTFC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works, thanks for quick fit. Checked on IDAaaS using MATLAB 2024a

@RichardWaiteSTFC RichardWaiteSTFC merged commit ecfd2c9 into master Feb 24, 2025
12 checks passed
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.

3 participants