Last active
December 20, 2015 17:39
-
-
Save NachoSoto/6170250 to your computer and use it in GitHub Desktop.
Passing garbage as C++ arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ clang++ -Wall -Werror -framework Foundation -std=c++11 -stdlib=libc++ garbage.mm -o test && ./test && echo $? | |
0 // MyClass didn't get initialized? | |
0 // method calls don't fail either | |
0 // exit code. No Crash? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <iostream> | |
using namespace std; | |
class MyClass | |
{ | |
public: | |
MyClass() : a(3) {} | |
int getA() const { return a; } | |
static void print(const MyClass &c) | |
{ | |
cout << c.a << endl; // prints 0 | |
cout << c.getA() << endl; // doesn't crash, and prints 0 | |
} | |
const int a; | |
}; | |
@interface A : NSObject | |
- (MyClass)someMethod; | |
@end | |
@implementation A | |
- (MyClass)someMethod | |
{ | |
return MyClass(); | |
} | |
@end | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
A *a = nil; | |
MyClass::print([a someMethod]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment