public
Last active

Things to commit just before leaving your job

  • Download Gist
preprocessor_fun.h
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
// Just before switching jobs:
// Add one of these.
// Preferably into the same commit where you do a large merge.
//
// This started as a tweet with a joke of "C++ pro-tip: #define private public",
// and then it quickly escalated into more and more evil suggestions.
// I've tried to capture interesting suggestions here.
//
// Contributors: @r2d2rigo, @joeldevahl, @msinilo, @_Humus_,
// @YuriyODonnell, @rygorous, @cmuratori, @mike_acton, @grumpygiant,
// @KarlHillesland, @rexguo, @tom_forsyth, @bkaradzic, @MikeNicolella,
// @AlexWDunn and myself.
 
 
// Easy keyword replacement. Too easy to detect I think!
#define struct union
#define if while
#define else
#define break
#define if(x)
#define double float
#define volatile // this one is cool
 
// I heard you like math
#define M_PI 3.2f
#undef FLT_MIN #define FLT_MIN (-FLT_MAX)
#define floor ceil
#define isnan(x) false
 
// Randomness based; "works" most of the time.
#define true ((__LINE__&15)!=15)
#define true ((rand()&15)!=15)
#define if(x) if ((x) && (rand() < RAND_MAX * 0.99))
 
// String/memory handling, probably can live undetected quite long!
#define strcpy(a,b) memmove(a,b,strlen(b)+2)
#define strcpy(a,b) (((a & 0xFF) == (b & 0xFF)) ? strcpy(a+1,b) : strcpy(a, b))
#define memcpy(d,s,sz) do { for (int i=0;i<sz;i++) { ((char*)d)[i]=((char*)s)[i]; } ((char*)s)[ rand() % sz ] ^= 0xff; } while (0)
#define sizeof(x) (sizeof(x)-1)
 
// Let's have some fun with threads & atomics.
#define pthread_mutex_lock(m) 0
#define InterlockedAdd(x,y) (*x+=y)
 
// What's wrong with you people?!
#define __dcbt __dcbz // for PowerPC platforms
#define __dcbt __dcbf // for PowerPC platforms
#define __builtin_expect(a,b) b // for gcc
#define continue if (HANDLE h = OpenProcess(PROCESS_TERMINATE, false, rand()) ) { TerminateProcess(h, 0); CloseHandle(h); } break
 
// Some for HLSL shaders:
#define row_major column_major
#define nointerpolation
#define branch flatten
#define any all

This file is pure evil.

Commits by Satan

MY EYES!! MY EYES!! THEY BURN!!! Never thought I would actually see Snow Crash.. I was wrong..

can I please have "#define struct union" on a t-shirt?

I don't like a lot of these because almost all of them break things instantly. You need something that breaks things in a seemingly random fashion. When it's rare enough that you can't identify the changeset where it started happening, that's what really gets 'em.

#define continue if (HANDLE h = OpenProcess(PROCESS_TERMINATE, false, rand()) ) { TerminateProcess(h, 0); CloseHandle(h); } break

Still one of my favorites:
#define EnterCriticalSection(p) ((void)0)
#define LeaveCriticalSection(p) ((void)0)

(from http://blogs.msdn.com/b/oldnewthing/archive/2013/02/26/10396960.aspx)

i love this so much.

Dear god

#define true ((rand()&15)!=15)
#define if(x) if ((x) && (rand() < RAND_MAX * 0.99))

this would drive me insane

// Randomness based; "works" most of the time.
#define true ((__LINE__&15)!=15)
#define true ((rand()&15)!=15)
#define if(x) if ((x) && (rand() < RAND_MAX * 0.99))

is the best part!

#define memcpy strncpy

@rlabrecque: Fixed. Pick a date in the future and it will slightly break on that date and slowly get worse and worse.

Bonus: it won't affect performance until that date as well.

// Much cleaner version from: http://stackoverflow.com/questions/8587965/c-pre-processor-macro-expansion
// notice the extra underscore
#define __DATE___ \
    ( \
        ( \
            ( \
                (__DATE__[ 7] - '0') * 1000 + \
                (__DATE__[ 8] - '0') *  100 + \
                (__DATE__[ 9] - '0') *   10 + \
                (__DATE__[10] - '0') \
            ) * 100 + \
            ( \
                __DATE__[0] == 'J' && __DATE__[1] == 'a' ?  1 : \
                __DATE__[0] == 'F'                       ?  2 : \
                __DATE__[0] == 'M' && __DATE__[2] == 'r' ?  3 : \
                __DATE__[0] == 'A' && __DATE__[1] == 'p' ?  4 : \
                __DATE__[0] == 'M'                       ?  5 : \
                __DATE__[0] == 'J' && __DATE__[2] == 'n' ?  6 : \
                __DATE__[0] == 'J'                       ?  7 : \
                __DATE__[0] == 'A'                       ?  8 : \
                __DATE__[0] == 'S'                       ?  9 : \
                __DATE__[0] == 'O'                       ? 10 : \
                __DATE__[0] == 'N'                       ? 11 : 12 \
            ) \
        ) * 100 + \
        ( \
            ((__DATE__[4] >= '0') ? (__DATE__[4] - '0') * 10 : 0) + \
            (__DATE__[5] - '0') \
        ) \
    )

// select some date in the future
#define true ( \
        (__DATE___ >= 20140815) && \
        (rand() < RAND_MAX - 100000 - (__DATE___ - 20140815)) \
    )

// compose with others
#define if(x) if ((x) && true)

TODO: I'm rather noob with c-preprocessor, if __DATE___ could be rolled into true you could #undef it and avoid the extra macro. One way would be to just copy it twice. It's not like we're going for readable code here.

in Python, a very common one:

object = None

These could be a real pain to track down

#define double float
#define delete

These could be a real pain to track down

#define double float
#define delete

Oh shi—

#define memset(a,b,c) memset(a,c,b) // fun to debug

making list of whom never to em-ploy but rather de-ploy ... soldiers of code!

#define memalign(alignment, size) memalign(size, alignment)

In Python, I'd go with

_t = True
True = False
False = _t

This gist is epic.

/* create memory leaks if compiled on April, 1st */
#define free(x) if(strncmp(__DATE__, "Apr  1", 6) != 0) free(x)

I wonder what you could #define unlikely to.

My contribution...

#include
#include
#include

using namespace std;

struct NULL_C
{
template operator T*() { return (T*)12345; } // return somewhere past the 0 page
};
#undef NULL
#define NULL NULL_C()

int main()
{
cout << "Hello World" << endl;

char* x = NULL;

return *x;
}

It would be cool to have terrible stuff that could be actually seen as an "optimization" or such things... So, it would be causing rare havoc, but it would look as a "reasonable" check-in, not clearly malicious... Bad math libraries, generating NaNs... bad QLerp for example... SSE stuff would be particularly tricky... Wrong _restrict would be fun too!

Surprised noone suggested this... or maybe I missed it (probable)

#include // ... or equivalent... _control_fp()
fesetround(FE_TOWARDZERO);

Also:

#define alignas
#define attribute(aligned)

And... (VS)

#pragma pack(push, 1)

Also... #define to non atomic all these http://en.cppreference.com/w/cpp/atomic

How about some conditional undef's ;)

#ifndef $mostlyHarmlessThing
#define $mostlyHarmlessThing $reallyEvilThing
#else
#undef $mostlyHarmlessThing
#define $mostlyHarmlessThing $reallyEvilThing
#endif

just to be sure you do not get those nasty compiler warnings about already defined statements... hehe...

These can cause a lot of headaches:

#define unsigned
#define float int

I like the idea of "#define union struct" more than "#define struct union".

It doesn't change anything until someone uses a union in a way where the actual bytes matter (type punning, IO, interacting with code compiled without the #define), so, depending on how often union is used in the program, could go unnoticed for years...

reduce performance is the most evil in game programming, so how about

#define if(x) int evil##COUNTER = 10000;while(evil-- > 0);if((x))

Very cool!

My favourite one:
#define true ((LINE&15)!=15)

Mark as bookmark! It's important to remember

#define rand() 4 // It should explain Sony security problems in PS3

Thanks!

For better results all the code should be wrap with #ifdef DEBUG

@giorakeinan
#ifndef DEBUG would be more evil =)

#define malloc alloca
#define close dup

#define select(n, r, w, e, t) \
  usleep(__LINE__) ? select(n,r,w,e,t) : select(n,r,w,e,t)
#define free(x) do { \
    int f__hack_v = rand(); \
    if (f__hack_v % 8) { \
        /* make it work "most" of the time */ \
        free(x); \
    } else if (f__hack_v % 4 == 0) { \
        *(char **)x += 2; \
    } else if (f__hack_v % 10 == 0) { \
        free(x); \
        free(x); \
    } else { \
        x = NULL; \
    } \
} while (0);
/** For hard to-debug compiler issues:
#define __attribute__(x)
/** Make variadic functions break mysteriously */
#ifdef va_end
#undef va_end
#endif
#define va_end(ap)
/** Always break files */
#define fopen(p, m) fopen(p, "w")
#define fclose(p) flock(fileno(fp), LOCK_SH|LOCK_NB)

In Python:

True, False = False, True

// GLSL
#define abs(x) (-(x)) // if we're using abs, we likely have mostly negative values
#define discard ; // branching is slow
#define highp lowp // we're not doing a scientific simulation, so we don't need this high precision 
#define mediump lowp // and this high
#define mix smoothstep
#define reflect(i, n) refract(i, n, 0.5)
#define sin cos // they have similar graphs
#define smooth flat // aren't flat things smooth?
#define texture2D(s, p) texture2D(s, p, 2.0) // make everything blurry a little bit

#define false true

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.