Skip to content

Instantly share code, notes, and snippets.

@andermoran
Created August 22, 2019 23:24
Show Gist options
  • Save andermoran/aa219e345c806ccc4913cde0e2d7110a to your computer and use it in GitHub Desktop.
Save andermoran/aa219e345c806ccc4913cde0e2d7110a to your computer and use it in GitHub Desktop.
Weird clang behavior
// https://twitter.com/zneakr/status/1164651753993715712
/* So @zneakr tweeted about this weird behavior and I decided to tinker with his example. In order to optimize, clang
assigns fun_ptr to leak_all_my_secrets no matter what. This leads to "I have 9 toes" being printed no matter the
result of the if statement. Super weird behavior from clang and I just wanted to make a note of it :)
To reproduce this result:
clang funkyClang.c -O1 -o funkyClang; ./funkyClang
*/
#include <stdlib.h>
#include <stdio.h>
static void (*fun_ptr)(void);
void leak_all_my_secrets () {
printf("I have 9 toes\n");
}
int main() {
if ((random() % 1000000) == 12321) {
fun_ptr = leak_all_my_secrets;
}
fun_ptr();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment