Undefined behavior in call to memcpy
The file Src/Components/FBasicParticleContainer.hpp contains two calls
to memcpy()
, where the second parameter (the source pointer) can be
null. This only happens when the third parameter (the byte count) is
zero. This is, however, undefined behavior.
According to cppreference.com:
If either
dest
orsrc
is a null pointer, the behavior is undefined, even if count is zero.
(emphasis mine).
Affected versions: I only tested the version tagged "V1.5.1".
Test: The script
CXX_FLAGS="-std=c++14 -fsanitize=undefined"
cd Tests/Utils/
rm -f testOctreeRearrange
g++ $CXX_FLAGS testOctreeRearrange.cpp -o testOctreeRearrange
./testOctreeRearrange
displays these error messages:
../../Src/Components/FBasicParticleContainer.hpp:117:23: runtime error: null pointer passed as argument 2, which is declared to never be null
../../Src/Components/FBasicParticleContainer.hpp:126:23: runtime error: null pointer passed as argument 2, which is declared to never be null
Solution: Only memcpy
if nbParticles
is non zero:
sed -i 's/memcpy/if (nbParticles != 0) memcpy/' Src/Components/FBasicParticleContainer.hpp