Skip to content
This repository has been archived by the owner. It is now read-only.

Fix UB in add_*_mconstraint #1

Merged
merged 1 commit into from Feb 25, 2019

Conversation

ijackson
Copy link

@ijackson ijackson commented Feb 24, 2019

NLopt's nlopt_add_*_mconstraint functions take a pointer to an array
of tolerances, one for each constraint; they copy that array.

rust-nlopt mistakenly passed a pointer to a single f64. As a result
NLopt would make out-of-bounds array accesses. Often the values read
would be negative, giving NLOPT_INVALID_ARGS.

There are two possible ways to fix this: 1. simply fix the rust-nlopt
API to take an appropriate slice; 2. make the existing functions copy
the single provided toleranc value and provide 2 new functions for
when the tolerances are not all the same.

I have chosen (1) because it is less work and because the declared
version of this crate is 0.x, so this would not break semver.

Two more arguments that could be made in support of this decision:

Anyone calling these functions is currently experiencing UB,
including (in my experience) random invalid argument errors. So
probably few people are using these entrypoints.

The new API corresponds to the NLopt API.

Signed-off-by: Ian Jackson ijackson@chiark.greenend.org.uk

NLopt's nlopt_add_*_mconstraint functions take a pointer to an array
of tolerances, one for each constraint; they copy that array.

rust-nlopt mistakenly passed a pointer to a single f64.  As a result
NLopt would make out-of-bounds array accesses.  Often the values read
would be negative, giving NLOPT_INVALID_ARGS.

There are two possible ways to fix this: 1. simply fix the rust-nlopt
API to take an appropriate slice; 2. make the existing functions copy
the single provided toleranc value and provide 2 new functions for
when the tolerances are not all the same.

I have chosen (1) because it is less work and because the declared
version of this crate is 0.x, so this would not break semver.

Two more arguments that could be made in support of this decision:

Anyone calling these functions is currently experiencing UB,
including (in my experience) random invalid argument errors.  So
probably few people are using these entrypoints.

The new API corresponds to the NLopt API.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
@jesskfullwood jesskfullwood merged commit 7ec43ca into jesskfullwood:master Feb 25, 2019
@jesskfullwood
Copy link
Owner

jesskfullwood commented Feb 25, 2019

Nice catch, thanks for the fix!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants