Skip to content

Instantly share code, notes, and snippets.

@C0deH4cker
Created February 2, 2018 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save C0deH4cker/676a42001dae23d0946081e5fb1e4dd6 to your computer and use it in GitHub Desktop.
Save C0deH4cker/676a42001dae23d0946081e5fb1e4dd6 to your computer and use it in GitHub Desktop.
This is bad
/* 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;
}
#~/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