6 ms·
C No Evil
- Que 15y agoInteresting header, but why would anyone want to cause someone else so much trouble trying to release a product?
- burgerbrain 15y agoIt's just a throwaway scenerio to wrap around an interesting problem...
- jakelear 15y agoAn interesting, albeit not that valuable, challenge. It's a fun little question, but it's not even valuable as a black hat exercise. It's just a non-sensical hypothetical.
- barrkel 15y agoI would prefer: #define assert(x) ((void)(x)) Most of these would cause an immediate flood of hints and / or warnings; but an assert that did nothing but evaluate its argument could lurk for some days, especially if the developers in question don't spelunk with a debugger very often.
- cperciva 15y agoI don't get it. In (non-wildly-buggy) code evaluating the argument to assert() is required to (a) succeed, and (b) have no side effects, so what would such a definition accomplish?
- barrkel 15y agoAssumptions: * asserts are used in a defensive programming style to state invariants * the enemy developers will be reasoning about the code assuming that asserts which didn't fire were true * when debugging, they'll read the code and prematurely dismiss valid theories as to why the bug exists based on what they read in the assertions
- cperciva 15y agoSure, but I don't think this matches the problem statement -- if the product is within a week or two of shipping, it should be long past the point where developers are tracking down bugs by using asserts to exclude possible code paths. If the problem was to slow down early development I'd absolutely agree with you.
- tptacek 15y agoAn easier way to define the same thing: #define NDEBUG 1
- losethos 15y agoI made LoseThos with a clean slate. It never occurred to me to make my compiler standard. Why make a standard compiler? They exist. I'm a good guy, not a devious bone in my body. God is just. Nobody can rob me. For what it's worth. Ain't going anywhere but soon everyone will have no job and money... just like me. I got no money to lose and have the benefit of God's conmfort.
- wulczer 15y agoI think #define volatile is by far the sneakiest. The rest would probably be found quite quickly, but omitting volatiles could lead to hard to detect bugs that will only manifest themselves under specific circumstances.
- huhtenberg 15y agovolatile is tied to a fairly specific usage and does not appear in that many projects. I'd personally go with while -> if, that's very sneaky.
- ricree 15y agoI'm rusty on my C preprocessor, but how would this work out for do {} while blocks?
- psykotic 15y agoIt will cause compiler errors, so it's not all that sneaky: int main(void) { while (1) break; return 0; }
- huhtenberg 15y agoIt may rather than will. This ^ is a rather odd, syntax abuse case.
- psykotic 15y agoWhat do you mean? You will get a compiler error in any case where there is a break or continue in an outermost while block. There's nothing odd or uncommon about that. Here's something closer to a real program: int main(void) { while (1) { char c = getchar(); if (c == 'q') break; process(c); } return 0; }
- huhtenberg 15y agoUgh. You are right of course. I had a brain spasm.
- tedunangst 15y ago#define continue break There's my choice. It's one of the few that will actually compile and the runtime effect is not super immediately detectable.
- cpeterso 15y agoor break switch statements with: #define break
- lukesandberg 15y agoThis is pretty much straight out of the daily wtf: http://thedailywtf.com/Articles/The-Disgruntled-Bomb.aspx http://thedailywtf.com/Articles/The-Disgruntled-Bomb.aspx
- raldi 15y agoWe have a winner: #define if(x) if(rand()<0.0001 && (x))
- iam 15y agoif(rand()>=0.0001 && (x)) that way it will fail extremely rarely and be even harder to detect!
- raldi 15y agoYou're right, they either need your change or a switch to ||
- reitzensteinm 15y agoDon't forget the #ifndef DEBUG around it - that would make it ten times harder to track down.
- nitrogen 15y agoC's rand() function returns an int >= 0, so rand() < 0.0001 will be true only when rand() returns zero. Assuming a pseudorandom number generator that uses all its possible values before repeating, you'll get a 0 approximately every 1 in RAND_MAX times you call rand().
- deleted 15y ago[deleted]
- tptacek 15y ago#define free(x) free(x + 1) #define malloc(x) malloc(random())
- tedunangst 15y agoThe free one at least will be detected immediately with a good libc, but nice idea. It gives me another idea though: #define free(x) do { free(x); x = NULL; } while (0) Assuming you can turn it off just before shipping, this will hide many forms of double free during development which then show up when the evil line is deleted.
- 1amzave 15y agoThat assumes 'x' is an lvalue, which it may very well not be (thus causing compilation errors, which would presumably get caught pretty quickly).
- tedunangst 15y agoI'm having a very hard time thinking of an example where you'd free an rvalue. Function return, maybe, but without using the value? It (almost) had to be an lvalue in order to get the malloc into it. Ah, an unconst cast. free((void *)constx), but lots of compilers don't catch that. Is there something else you were thinking of? There's also people doing stupid shit like free(&x), but those people don't need evil macros to dick up their code. :)
- copper 15y agoFunction calls seem right - particularly if they're badly written. Also, this macro might give curious results when passed in ptr++ . While this is C++, I've actually seen this particular construct: delete new SomeClass(...); used as, basically, an eye-poppingly clever way of initializing something, doing work, and then cleaning up after.
- tedunangst 15y ago
- floppydisk 15y agoSome buddies and I were messing around with ways we could sneakily inject one hidden #define somewhere in a C project to make it look like a segfault. We ended up with #define printf to print out "segfault" but GCC always threw warnings, so it didn't have much practicality for use in a joke.
- tedunangst 15y agoPre C99? Cheap hack using variadic macros: #define printf(...) printf(__VA_ARGS__); puts("\n\n\n\nFAIL!")
- floppydisk 15y agoYes, pre-C99. I'm going to have to try that now.
- bryanallen22 15y ago#define int char I did this one to a friend (briefly) during school.
- CPlatypus 15y agoI think redefining volatile wouldn't be harmful enough because so few people use volatile correctly anyway. By contrast, the break/continue or double-free suggestions would show up too quickly. I don't think I can come up with anything as insidious as some of the "one in a million" suggestions, but I like this one for its brevity. #define do You could even slip that into a makefile (-Ddo=) and it might not be noticed.
- epidemian 15y agoI think that would cause an infinite loop on most usages of do/while, thus making the problem (probably not the cause hehe) quickly noticeable.
- afhof 15y agoObviously library dependent, but I think this would make me pull my hair out: #define pthread_mutex_lock(x) #define pthread_mutex_unlock(x)
- bdonlan 15y ago#define free(x) Most tests still pass, but once the program's been running for a while you'll get a nice memory leak. Fails if your mortal enemy runs leak checks as part of their standard test suite or other QA, or has a high enough allocation rate to quickly turn up such issues.
- Someone 15y agoImprovement: include some static counter, and only skip the free after a few million (billion?) calls. If possible, also make the thing random, and limit skipping free to a limited range of block sizes. The latter provides a nice false trail for investigators (hm, it is leaking only struct foo...) Problem of course is to find a nice size range for the program at hand.
- dschoon 15y agoSome of the most frustrating problems I've seen are due to misunderstanding or rare semantics rather than mistakes. Examples might include the specifics of floating-point math, or exactly how numeric coercion works. This is especially bad when the result isn't easy to eyeball as right or wrong. For example, we could force a double-valued transcendental function to float and back for a small loss of precision in normal cases: #define sin(d) ((double)sinf(d)) Which yields... double i = 10 --> i = 10.0000000000 sin(i) = -0.5440211109 sinf(i) = -0.5440211296 delta = 0.0000000187 double i = 0.25 --> i = 0.2500000000 sin(i) = 0.2474039593 sinf(i) = 0.2474039644 delta = -0.0000000051 double i = 9999999.999999 --> i = 9999999.9999989998 sin(i) = 0.4205487007 sinf(i) = 0.4205478132 delta = 0.0000008875 (Format string was %25.10f, fwiw.) Another idea: Let's say we know the application relies on rand() to produce the correct distribution for testing, or maybe to generate session keys. Changing the semantics of the RNG won't result in a compiler bug, but it might result in the tests failing to cover all cases, or a horrible security breach.
- leif 15y ago#define rand() (rand() % 100)
- killerswan 15y agoReminds me of Sony... Perhaps they had this someplace: #define rand(x) 4
- elevenE 15y agoCan anyone explain what effect #define goto would have? It doesn't looks valid, because if I have myLabel: /* some code*/ goto myLabel; would produce myLabel; Which, as far as I know shouldn't compile because it expects either a normal expression or a definition/declaration and myLabel; doesn't appears to be any of them. (Maybe I'm missing something)
- seclorum 15y agoHere's mine: __attribute__((constructor)) void __maiin(void) { if (rand() < 0.05) exit(); //if (rand() < 0.05) close(1); //if (rand() < 0.05) main(); // and so on .. } The program will randomly quit before main() is executed.
- cpeterso 15y ago#define memmove memcpy #define default default: break; case 0xfeceface
- Turing_Machine 15y ago#define fopen(A,B) ((rand() % 1000 == 0) ? fopen(A,"w") : fopen(A,B))