snl-dakota / dakota-packages

The core TPL repo
1 stars 5 forks source link

Incomplete switch statement in approxnn may trigger undefined behavior #2

Open StefanBruens opened 1 year ago

StefanBruens commented 1 year ago

This code and similar below may trigger undefined behavior:

https://github.com/snl-dakota/dakota-packages/blob/95dbce9d761abbeb84cc86dda56ed5d8c7e29380/approxnn/include/ANN/ANNnormselect.h#L52-L59

This triggers the following GCC warning/error:

[  112s] In file included from /home/abuild/rpmbuild/BUILD/dakota-6.17.0-release-public-src-cli/packages/external/approxnn/src/../include/ANN/ANN.h:133,
[  112s]                  from /home/abuild/rpmbuild/BUILD/dakota-6.17.0-release-public-src-cli/packages/external/approxnn/src/../include/ANN/ANNx.h:40,
[  112s]                  from /home/abuild/rpmbuild/BUILD/dakota-6.17.0-release-public-src-cli/packages/external/approxnn/src/ANN.cpp:30:
[  112s] /home/abuild/rpmbuild/BUILD/dakota-6.17.0-release-public-src-cli/packages/external/approxnn/src/../include/ANN/ANNnormselect.h: In member function 'T approxnn::normSelector::pow(const T&) [with T = double]':
[  112s] /home/abuild/rpmbuild/BUILD/dakota-6.17.0-release-public-src-cli/packages/external/approxnn/src/../include/ANN/ANNnormselect.h:60:3: error: control reaches end of non-void function [-Werror=return-type]
[  112s]    60 |   }
[  112s]       |   ^

Although semantically the method can only be either L2_NORM or LINF_NORM, the underlying type of the enum may have any integer value in range according to the relevant C and C++ standards.

Possible fixes are either throwing an exception, or adding a default label to the L2_NORM handling.