Skip to content

Instantly share code, notes, and snippets.

@rcythr
Last active March 21, 2024 15:28
Show Gist options
  • Select an option

  • Save rcythr/a65a022f5b892256c537c6542cf83e34 to your computer and use it in GitHub Desktop.

Select an option

Save rcythr/a65a022f5b892256c537c6542cf83e34 to your computer and use it in GitHub Desktop.
#include <iostream>
int* getIntP(bool return_null) {
if (return_null) return nullptr;
return new int(3);
}
int main(int argc, char** argv) {
if(int* i1 = getIntP(true)) {
std::cout << *i1 << std::endl;
} else if(int* i2 = getIntP(false)) {
std::cout << *i1 << std::endl;
}
}
@rcythr
Copy link
Author

rcythr commented Mar 21, 2024

The output from this program is:

Program terminated with signal: SIGSEGV

The else if branch accidentally uses i1 instead of i2 due to a copy/paste error. The language allows the use of i1 in the else branch. Presumably to allow for checks other than nullptr checks.

IMO, this is a language bug because the benefit of this "feature" is low utility, but it creates an entire class of bugs like this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment