Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created February 22, 2019 23:23
Show Gist options
  • Save kaityo256/1d0e448459fc33fbf262e38b9e9ac911 to your computer and use it in GitHub Desktop.
Save kaityo256/1d0e448459fc33fbf262e38b9e9ac911 to your computer and use it in GitHub Desktop.
Undefined Behavior Sample
#include <iostream>
int foo() {
std::cout << "foo" << std::endl;
}
void bar() {
std::cout << "bar" << std::endl;
foo();
}
int main() {
foo();
}
@kaityo256
Copy link
Author

$ g++ --version
g++ (Homebrew GCC 8.2.0) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -O1 test.cpp
test.cpp: In function 'int foo()':
test.cpp:5:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^

$ ./a.out
foo
bar
(snip)
foo
bar
foo
zsh: segmentation fault  ./a.out

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