Skip to content

Instantly share code, notes, and snippets.

@NachoSoto
Last active December 20, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NachoSoto/6170250 to your computer and use it in GitHub Desktop.
Save NachoSoto/6170250 to your computer and use it in GitHub Desktop.
Passing garbage as C++ arguments
$ 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?
#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