I'm using g++ 4.9.2 on Debian 8, x86_64. I'm catching a Undefined Behavior sanitizer (UBsan) (-fsanitize=undefined
) error:
algebra.cpp:206:8: runtime error: load of value 127,
which is not a valid value for type 'bool'
The code is from the Crypto++ library. Here is the code at algebra.cpp:206
(and some related code):
206 struct WindowSlider
207 {
208 WindowSlider(const Integer &expIn, bool fastNegate, unsigned int windowSizeIn=0)
209 : m_exp(expIn), m_windowModulus(Integer::One()), m_windowSize(windowSizeIn), m_windowBegin(0), m_fastNegate(fastNegate), m_firstTime(true), m_finished(false)
210 {
...
249 Integer m_exp, m_windowModulus;
250 unsigned int m_windowSize, m_windowBegin;
251 word32 m_expWindow;
252 bool m_fastNegate, m_negateNext, m_firstTime, m_finished;
253 };
Its called in a couple of places, like:
$ grep -I WindowSlider *
...
algebra.cpp: std::vector<WindowSlider> exponents;
algebra.cpp: exponents.push_back(WindowSlider(*expBegin++, InversionIsFast(), 0));
ecp.cpp: std::vector<WindowSlider> exponents;
ecp.cpp: exponents.push_back(WindowSlider(*expBegin++, InversionIsFast(), 5));
InversionIsFast
is a bool
, so that should not be a problem. But I added !!InversionIsFast()
just in case and the issue persists.
EDIT: Here is a grep for InversionIsFast
. It appears it is initialized.
$ grep -I InversionIsFast *
algebra.cpp: exponents.push_back(WindowSlider(*expBegin++, !!InversionIsFast(), 0));
algebra.h: virtual bool InversionIsFast() const {return false;}
ec2n.h: bool InversionIsFast() const {return true;}
ecp.cpp: exponents.push_back(WindowSlider(*expBegin++, !!InversionIsFast(), 5));
ecp.h: bool InversionIsFast() const {return true;}
I also initialized m_negateNext
in the ctor.
What is the issue, and how do I clear it?
The blog post Testing libc++ with -fsanitize=undefined which also mentioned a similar error:
runtime error: load of value 64, which is not a valid value for type 'bool'
suggests it may be due to uninitialized bool, see the comment at the end which says:
I had not (in class) initialised the bool [...]
Which from what I can tell is the case with m_negateNext
since it is not initialized in the constructor of WindowSlider
while the rest of the member variables are.
An uninitialized bool will have indeterminate value and using an indeterminate value is undefined behavior.