Skip to content

Instantly share code, notes, and snippets.

@rachtsingh
Created September 15, 2018 22:14
Show Gist options
  • Save rachtsingh/f2fe71d6f63361e416dc5bed408ec098 to your computer and use it in GitHub Desktop.
Save rachtsingh/f2fe71d6f63361e416dc5bed408ec098 to your computer and use it in GitHub Desktop.
Trying to use stack memory after it's gone
#include <stdio.h>
// void * return type means that it returns a naked pointer, which is just an address.
void *foo(void) {
long bar = 5;
printf("bar's address is: %p, holds %ld bytes\n", &bar, sizeof(long));
long *ret = &bar;
return (void *) ret; // ret is a pointer to a long, but we just turn it into a regular address
// note that the previous line doesn't *do* anything, it just gets the compiler off our back
}
int main(void) {
void *ptr = foo();
printf("about to print what's at %p\n", ptr);
long *l_ptr = (long *) ptr;
printf("%ld\n", *l_ptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment