Runtime error: load of value 127, which is not a valid value for type 'bool'

Runtime error: load of value 127, which is not a valid value for type 'bool'

我在 Debian 8 上使用 g++ 4.9.2,x86_64。我正在捕获未定义行为消毒剂 (UBsan) (-fsanitize=undefined) 错误:

algebra.cpp:206:8: runtime error: load of value 127,
    which is not a valid value for type 'bool'

代码来自 Crypto++ 库。这是 algebra.cpp:206 处的代码(以及一些相关代码):

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     };

它在几个地方被调用,例如:

$ 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 是一个 bool,所以这应该不是问题。但我添加了 !!InversionIsFast() 以防万一,问题仍然存在。

编辑:这是 InversionIsFast 的 grep。它似乎已初始化。

$ 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;}

我也在ctor里初始化了m_negateNext

这是什么问题,我该如何解决?

博客postTesting libc++ with -fsanitize=undefined里面也提到了类似的错误:

runtime error: load of value 64, which is not a valid value for type 'bool'

表明这可能是由于未初始化的 bool,请参阅末尾的评论:

I had not (in class) initialised the bool [...]

据我所知,m_negateNext 就是这种情况,因为它没有在 WindowSlider 的构造函数中初始化,而其余成员变量是。

未初始化的布尔值将具有不确定的值并且 using an indeterminate value is undefined behavior