Created
February 2, 2018 05:18
-
-
Save C0deH4cker/676a42001dae23d0946081e5fb1e4dd6 to your computer and use it in GitHub Desktop.
This is bad
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
/* THIS IS VERY BAD */ | |
#define CONCAT(a, b) CONCAT_(a, b) | |
#define CONCAT_(a, b) a##b | |
#define THIS_IS_BAD(test, first, rest, cond, body...) THIS_IS_BAD_2(__COUNTER__, test, first, rest, cond, ##body) | |
#define THIS_IS_BAD_2(uniq, test, first, rest, cond, body...) THIS_IS_BAD_3(CONCAT(this_is_bad_, uniq), test, first, rest, cond, ##body) | |
#define THIS_IS_BAD_3(label, test, first, rest, cond, body...) do { \ | |
if(test) { \ | |
first(); \ | |
goto label; \ | |
} \ | |
do { \ | |
rest(); \ | |
label: \ | |
body \ | |
; /* in case body is empty or doesn't end with a semicolon */ \ | |
} while(cond); \ | |
} while(0) | |
#include <stdio.h> | |
void first_time(void) { | |
printf("first_time()\n"); | |
} | |
void other_times(void) { | |
printf("other_times()\n"); | |
} | |
int main(void) { | |
int i = 0; | |
THIS_IS_BAD((i == 0), first_time, other_times, (i < 4), { | |
printf("Body: i = %d\n", i); | |
++i; | |
}); | |
printf("\nAgain, to show that labels aren't being reused.\n\n"); | |
THIS_IS_BAD((i == 4), first_time, other_times, (i >= 0), { | |
printf("Reverse body: i = %d\n", i); | |
--i; | |
}); | |
return 0; | |
} |
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
#~/tmp$ clang -std=gnu99 -o this_is_bad this_is_bad.c | |
#~/tmp$ ./this_is_bad | |
first_time() | |
Body: i = 0 | |
other_times() | |
Body: i = 1 | |
other_times() | |
Body: i = 2 | |
other_times() | |
Body: i = 3 | |
Again, to show that labels aren't being reused. | |
first_time() | |
Reverse body: i = 4 | |
other_times() | |
Reverse body: i = 3 | |
other_times() | |
Reverse body: i = 2 | |
other_times() | |
Reverse body: i = 1 | |
other_times() | |
Reverse body: i = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment