Fun with uninitialized variables and compiler (GCC)

后端 未结 2 1480
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 11:07

The section §3.9.1/6 from the C++ Standard says,

Values of type bool are either true or false.

Now con

2条回答
  •  执笔经年
    2020-12-03 11:21

    Just today I came across a version of this bug. I offer my experience here in case it's enlightening to anyone else.

    I had some code which boiled down to

    if(!(a == b && c.d())) { do_something(); }
    

    The bug I was chasing was that do_something() was happening, wrongly. Yet a was definitely equal to b and c.d() was, it seemed, returning true.

    As I was tracking this down, I temporarily added these test printouts:

    if(  a == b && c.d() ) printf("yes\n"; else printf("no\n");
    if(!(a == b && c.d())) printf("noo\n"; else printf("yess\n");
    

    To my surprise this printed yes and noo, which confirmed both why do_something was happening, and that something very strange was going on.

    It turned out that method d() was something like

    bool whatever::d() {
        return _successful;
    }
    

    But _successful was uninitialized. When I printed out its value, it was 236, which is why earlier I had said "c.d() was, it seemed, returning true."

    I didn't inspect the assembly code, but I'm guessing that under some circumstances, gcc was testing whether it was nonzero or not, but under others, it was just testing the low-order bit.

    Properly initializing _successful made the bug go away. (It had been uninitialized for ten years, since an earlier programmer first wrote method d(). Yet the bug hadn't manifested until a few months ago. This is why, sometimes, Software Is Hard.)

提交回复
热议问题