Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Things to commit just before leaving your job
// 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.
//
// In case it's not clear: I am not suggesting you *actually* do this!
// 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 memcpy strncpy
#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
@bturner1273
Copy link

bturner1273 commented Nov 1, 2019

In Python, I'd go with

_t = True
True = False
False = _t

How can you assign a value to a keyword? 'True = False' ????

@CarlosACepeda
Copy link

CarlosACepeda commented Jan 14, 2020

Pure hilarity

@AeroStun
Copy link

AeroStun commented Jan 25, 2020

Can anyone think of something involving setjmp and longjmp?
Trying to figure out something doable

@puschkin
Copy link

puschkin commented Apr 27, 2020

In Python, I'd go with

_t = True
True = False
False = _t

How can you assign a value to a keyword? 'True = False' ????

This works only in python2.

@k26pl
Copy link

k26pl commented May 5, 2020

#define f false
#define false true
#define true f
#define a 1
#define 1 0
#define 0 a

@RisingThumb
Copy link

RisingThumb commented May 12, 2020

This is equal parts terrifying, hilarious and evil.
It's gold

@riking
Copy link

riking commented Jun 15, 2020

One better on the sin/cos trickery:

// By defining one in terms of the other, we can save on binary size.
#define sin(x) cos(x+1.5709763267948966)

The reveal:

>>> (math.pi / 2) - 1.5709763267948966
-0.000180

@VectorKappa
Copy link

VectorKappa commented Aug 6, 2020

Calm down, satan.

@Rub-me
Copy link

Rub-me commented Aug 17, 2020

#define return 0

#define break if((rand() % 11) <= 7) { break; } else { while(1){printf("broke\n"); }

This would be funny too

@hyperair
Copy link

hyperair commented Sep 7, 2020

This reminds me of https://thedailywtf.com/articles/The-Disgruntled-Bomb (tl;dr: replace nullptr with something that is null most of the time but sometimes not null).

@kondratovbr
Copy link

kondratovbr commented Sep 11, 2020

Too easy to catch while writing code in a proper IDE. Doesn't make it less evil though.

@rillig
Copy link

rillig commented Sep 11, 2020

#include <ctype.h>
#undef isspace /* get rid of the char subscript warning from GCC */

Sadly, line 2 already invokes undefined behavior, which is obvious for anyone who has actually read any C standard from the last 30 years.

When the actual isspace function (which typically had been shadowed by the macro before) is called with a character from outside the basic execution character set, which typically happens for UTF-8 nowadays, the direct argument conversion to int reads the _ctype array out of bounds.

Adding a rationale comment around the undefined behavior does not prevent it, but is needed for the Underhanded C Contest, expressing missing knowledge about this area of the C standard, which adds some bonus points for being innocent.

@haunt98
Copy link

haunt98 commented Nov 25, 2020

I need golang version 💯

@Lohkdesgds
Copy link

Lohkdesgds commented Feb 9, 2021

#define assert(x) throw x

maybe?

@erikpa1
Copy link

erikpa1 commented Apr 15, 2021

fcking legend

@mekb-turtle
Copy link

mekb-turtle commented May 30, 2021

#define if( if(rand()<RAND_MAX/2&&

@Theaninova
Copy link

Theaninova commented May 31, 2021

This one is really evil
evil.h

#pragma pack

type.h

typedef struct {
  unit8_t bar;
  uint32_t baz;
} Foo;

function.h

#include "type.h"

Foo combineStruct(uint8_t bar, uint32_t baz);

function.c

#include "function.h"

Foo combineStruct(uint8_t bar, uint32_t baz) {
  ...
}

`d.h

#include "evil.h" // Include the header with #pragma pack
#include "function.h" // Include the function. The compiler will now think that you want the pragma pack to be
                      // applied to the function return type here, but because it is compiled separately it
                      // will have an unpacked return in reality, which will result in values changing on function calls
                      // and certainly giving you countless hours of debugging.

int main() {
  Foo test = combineStruct(1, 2);
}

@bsdunx
Copy link

bsdunx commented Jun 8, 2021

Hard to get a system you can do this on these days but this is kinda cruel:

prctl(PR_SET_TIMERSLACK, 10000000, 0, 0, 0);

@counter185
Copy link

counter185 commented Jun 25, 2021

#define malloc(x) (void*)(rand()*0x121200)

@bjorn3
Copy link

bjorn3 commented Jun 25, 2021

That will immediately crash @counter185 I think. You may as well return a fixed (void*)0x100 or something like that.

@counter185
Copy link

counter185 commented Jun 25, 2021

@bjorn3 just tried it with gcc and it works

EDIT: by works i mean compiles

@thenexxuz-zz
Copy link

thenexxuz-zz commented Jul 27, 2021

You know that feeling you get right before you barf.... your skin goes clammy and you shiver a little bit..... then you have the taste of TV static in the back of your mouth...... then this happens.

@roj1512
Copy link

roj1512 commented Aug 30, 2021

lulz

@adenosinetp10
Copy link

adenosinetp10 commented Sep 1, 2021

How to automate carts using redstone in minecraft ? Any help ?

@counter185
Copy link

counter185 commented Sep 1, 2021

@notPlasticCat Try detector rails attached to powered rails

@adenosinetp10
Copy link

adenosinetp10 commented Sep 1, 2021

@notPlasticCat Try detector rails attached to powered rails

Thank you! I will ask if I get any doubts. ☺️

@mekb-turtle
Copy link

mekb-turtle commented Sep 3, 2021

mhm yes minecraft very related to this post lol

@JosephCatrambone
Copy link

JosephCatrambone commented Oct 21, 2021

#define free(p) realloc(p, 1)

@boomer41
Copy link

boomer41 commented Nov 27, 2021

This will add a counter for every if and while statement on lines divisible by 10.
For each invocation of the if/while, the counter goes up by 1. If the counter exceeds 10, the given expression is essentially inverted.

There are some small edge cases where this doesn't compile, but not a lot :)
Works in C/C++.

#define if(_x) \
    if ( \
        ( \
            (__LINE__%10)==0 && \
            ({ \
                static int ___I_AM_EVIL = 0; \
                ___I_AM_EVIL++; \
                (___I_AM_EVIL > 10); \
            }) \
        ) \
        ^ (_x))

#define while(_x) \
    while ( \
        ( \
            (__LINE__%10)==0 && \
            ({ \
                static int ___I_AM_EVIL = 0; \
                ___I_AM_EVIL++; \
                (___I_AM_EVIL > 10); \
            }) \
        ) \
        ^ (_x))

@dkfellows
Copy link

dkfellows commented May 31, 2022

Can anyone think of something involving setjmp and longjmp? Trying to figure out something doable

Absolutely anything involving them is already excessively evil. Worse, I've seen production uses of them where they were the correct solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment