opensim-org / opensim-core

SimTK OpenSim C++ libraries and command-line applications, and Java/Python wrapping.
https://opensim.stanford.edu
Apache License 2.0
758 stars 307 forks source link

Bug fix in PiecewiseLinearFunction: coefficient array now properly initialized when number of function points is 1. #3817

Closed fcanderson closed 1 week ago

fcanderson commented 1 week ago

Fixes issue #3816

In class OpenSim::PiecewiseLinearFunction, when the number of function points was equal to one (i.e., when _x.getSize() == 1) initialization of the coefficient array, _b, was getting skipped. In such a situation, when calcValue() was called (sill a valid thing to do!), _b[0] was being accessed when the size of _b was still 0 (an access violation) and would often produce a spurious result.

In a Release build, the above access violation was causing sporadic failures in the testPrescribedForce unit test.

In a Debug build, testPrescribedForce always failed because of an array out-of-bounds error detected by a low-level assert.

Brief summary of changes

Added/Altered ~6 lines of code in PiecewiseLinearFunction::calcCoefficients() to properly size and initialize _b when n == 1. See lines 417 - 425 of PiecewiseLinearFunction.cpp.

Testing I've completed

In both Debug and Release builds, the testPrescribedForce unit test is now passing. In addition, all other unit tests pass in a Release build. (Note that there continue to be quite a few unit tests that do not pass in a Debug build for unrelated reasons.)

CHANGELOG.md (choose one)


This change is Reviewable

fcanderson commented 1 week ago

@nickbianco Thanks for the quick review!

fcanderson commented 1 week ago

@nickbianco C-I runs have completed, all successful. A few file reviews are pending. Otherwise, ready for a merge I think.

nickbianco commented 1 week ago

Thanks @fcanderson!