DEV Community

Discussion on: What was the weirdest bug you ever encountered?

Collapse
 
dwd profile image
Dave Cridland

Personally, the weirdest and hardest to solve bugs for me are when the C++ optimizer takes a shortcut that ends up ignoring my code. These are usually cases where, for example, a pointer cannot be NULL according to the standard and therefore my NULL check is ignored.

I know of two other bugs which are gloriously Byzantine. I don't know Trey Harris, but his bug is legendary: The 500 Mile Email

I do know (and used to work for) Dave Baggett, and have heard this story first-hand. My Hardest Bug Ever

Collapse
 
metalmikester profile image
Michel Renaud

Those optimizers can be sneaky. I don't know how long you've been doing this, but I remember when the first C and C++ optimizing compilers for PC came out (late '80s, roughly) that they did a lot of nasty things and you had to be very careful with what optimizations you picked. Let's just say it was a rocky start.

I was on a project with some C++ just three years ago and it's a good thing my supervisor was borderline genius because he caught VC++ doing some really strange things. The ways he figured it out was beyond the skills of pretty much everyone else I know.

Collapse
 
dwd profile image
Dave Cridland

I've been using C/C++ since around 1990 or so. The old problems were usually weird compiler bugs when optimizations went wrong dramatically, from what I can recall. But I was thinking more of cases like this, which tripped me up recently - I'll simplify, of course:

int * p = nullptr; // Start with a NULL pointer.
int & r = *p; // Later on, convert to a reference, probably in a function call.
int * i = &r; // Convert the reference back to a pointer.
if (i != nullptr) { // If it's not NULL
  std::cout << "i is now " << *i << std::endl; // Print it.
}

That code crashes with a segfault trying to deference a NULL pointer. This is because the standard says that if the address of a reference is the NULL pointer, it's Undefined Behaviour. So most modern compilers now choose to optimize away the test, even without optimization turned on...

Thread Thread
 
metalmikester profile image
Michel Renaud

"Undefined behaviour" is hell.

Collapse
 
devmount profile image
Andreas

Thank you so much for sharing! The 500 mile email is indeed legendary! 😂