Programming Horror is where developers come together to revel in the idiocy of our peers.

This subreddit is meant for sharing funny programming related stories and strange or straight-up awful code. Make sure to try and make your submissions contain a funny or interesting narrative. Take a look at The Daily WTF for an idea of the kind of content that we're interested in.

For the sake of not being mauled by rabid lawyer bears, please make sure to anonymize your stories - changing the names of people and companies.

For code examples, indent all your lines with 4 spaces to make it more readable:

foo = 'bar'

Sister subreddits

talesfromtechsupport - Not everything that involves a computer is a programming horror story. For IT support related submissions, head on over here.

resolves symbol in the dynamic library pointed to by handle. So, if there's a global variable, you can access it; if there's a global function, you can use it. Except the C99 standard doesn't let you assign a void* to a function pointer.
So the following (obvious) code doesn't work:

int (*fp)(int);
fp = dlsym(handle, "foo");

because you're assigning a void* to an int (*)(). The solution is one of the ugliest casts I've ever come across in serious code:

*(void **) (&fp) = dlsym(handle, "foo");

WHY did the C99 committee think it would be a good idea to prevent casting from void* to a function pointer? I get that it's unnecessary in modern languages, but casting to and from void* is the bread and butter of C's memory system. It should be understood that if you're writing casts in C, you know what you're doing!

It actually makes sense in embedded systems - you put the code in ROM or Flash or something like it, then you can execute in place out of that memory. Variables and stacks and the like obviously need to go in RAM.

Reminds me of casting between generic type parameters and actual types in C#. If I have a variable x of type T, and typeof(T) == typeof(int), why can't I just say (int)x? No, I have to say (int)(object)x!

In C, & is the address-of operator. Perhaps it's better to use a simple example:

int x, *xPtr; //x is an int, xPtr is a pointer to an int.
x = 5; //the number five has now been put in memory wherever x resides.
xPtr = &x; //xPtr is the memory address where x is stored.
*xPtr = 6; //x is now 6.
xPtr=0xFEE1DEAD; //xPtr points to a random memory address
*xPtr= 10; //Segfault. (but x would remain unchanged.)

So, in this case, the cast is * (void ** ) (&fp)
First, I should parenthesize it a bit more: * ((void ** ) (&fp)). So we're derefencing (&fp), which we have cast to (void ** ).
Consider that fp is itself a pointer to a function. So fp is a function * . Then, the address of fp is a pointer to a pointer to a function * , or function ** . I cast this function ** to void ** .

It's completely circuitous, and it would be much simpler to just cast fp to a void * :

*((void*) (fp))

but in C, you can't cast a function * to a void * . (for whatever reason) You can, however, cast a function ** to a void ** , so that's what this code has to do.