Created
April 15, 2015 18:45
-
-
Save msullivan/fc7517efa94b70d31ae5 to your computer and use it in GitHub Desktop.
Copying values to a tmp with __typeof__ "doesn't" work
This file contains hidden or 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
// Copying variables into a tmp using __typeof__ is a common | |
// pattern in hacky gcc C macros, but it can backfire and | |
// allow the value to be evaluated twice! | |
// This is because __typeof__ will evaluate its argument if | |
// the value has a variably sized type! | |
// gcc added __auto_type to get around this problem in its stdatomic.h, but I'm | |
// not losing any sleep over this. | |
// See http://patchwork.ozlabs.org/patch/290802/ | |
#include <stdio.h> | |
int wtf() { printf("wtf\n"); return 0; } | |
int s = 5; | |
// Copy a value to the variable tmp | |
#define TMP_THING(x) __typeof__(x) tmp = (x) | |
int main(void) { | |
int vla[s][s]; | |
int (*p)[s] = &vla[0]; | |
// wtf printed twice! | |
TMP_THING(p + wtf()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment