Last active
October 18, 2016 03:58
-
-
Save piaoger/46c1096319fcba07461c4c1cfb2dcaa5 to your computer and use it in GitHub Desktop.
memory unsafe in c
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
// https://en.wikipedia.org/wiki/Dangling_pointer | |
void f() { | |
int *x = malloc(sizeof(int)); | |
*x = 1024; | |
printf("%d\n", *x); | |
// free in some place | |
{ | |
free(x); | |
x = 0; | |
} | |
// try to use it again | |
printf("%d\n", *x); | |
} | |
void f2() { | |
// stack | |
int x = 5; | |
int* y = &x; | |
*y += 1; | |
// free at some place | |
if(y) { | |
free(y); | |
} | |
} | |
void f3() { | |
int *x = malloc(sizeof(int)); | |
*x = 3; | |
printf("%p\n", x); | |
{ | |
int y = 100; | |
x = &y; | |
printf("%p\n", x); | |
} | |
printf("%p\n", x); | |
} | |
void f4() { | |
unsigned long a[1]; | |
a[3] = 0x7ffff7b36cebUL; | |
} | |
int main(int argc, char **argv) { | |
printf("f >>\n"); | |
f(); | |
printf("f2 >>\n"); | |
f2(); | |
printf("f3 >>\n"); | |
f3(); | |
printf("f3 >>\n"); | |
f3(); | |
printf("f4 >>\n"); | |
f4(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment