Cursed Code Collection

I have a pile of interesting programs, and nowhere to put them. So I'm putting them here.


The shebang for C is just a little bit longer than one line.

#if 0
TMP="$(mktemp -d)"
cc -o "$TMP/a.out" -x c "$0" && "$TMP/a.out" $@
RVAL=$?
rm -rf "$TMP"
exit $RVAL
#endif

#include <stdio.h>
int main() {
  puts("This is such a cool bash script!");
}

Open a file, swap the nibbles of every byte, and write it back. I'm not sure if this error handling macro is big brain or small brain. For sure it wouldn't pass code review.

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

char* b;
size_t i;
int fd;
struct stat st;

#define EH(fn,...) fn(__VA_ARGS__); if (errno) return perror(#fn), 1;
#define sz st.st_size

int main(int argc, char** argv) {
  if (argc != 3) return 1;
  EH(stat, argv[1], &st);
  b = EH(malloc, sz);
  fd = EH(open, argv[1], O_RDONLY);
  EH(read, fd, b, sz);
  for (i = 0; i < sz; ++i)
    b[i] = ((b[i] & 0x0F) << 4) | ((b[i] & 0xF0) >> 4);
  fd = EH(creat, argv[2], S_IRWXU);
  EH(write, fd, b, sz);
}

Annihilate your python runtime.

da, di = delattr, dir
shred_runtime = lambda: [da(__builtins__, a) for a in di(__builtins__)]
shred_runtime()

# print() is no longer defined
print("Good luck debugging me.")

This one is from a hackernews post, but quickly became near and dear to my heart. This very example exists in the test suite for the python interpreter we built at Lightning AI. Python closures don't work in quite the way you think they do. For a more complete explanation, see: Understanding a Python closure oddity.

def loop():
    for number in range(5):
        def closure():
            return number
        yield closure

eagerly = [each() for each in loop()]
print(eagerly) # [0, 1, 2, 3, 4]

lazily = [each() for each in list(loop())]
print(lazily) # [4, 4, 4, 4, 4]

Use generics to figure out what version of strerror_r you have. This is less cursed code, and more just the most sane way of doing things. But the fact that _Generic is actually the best solution to any problem is baffling, and cursed in its own right.

#include <string.h>
#include <stdio.h>

#define STRERR_R _Generic(strerror_r,                          \
                          int(*)(int, char*, size_t): "XSI",   \
                          char*(*)(int, char*, size_t): "GNU", \
                          default: "UNK")
int main(void) {
  puts(STRERR_R);
}

On a related note, no such collection would be complete without trigraphs. Here's one from my friend Morwenn. This was originally a meme, but somehow it made it onto the wikipedia page!

int trigraphsavailable()
{
    // are trigraphs available??/
    return 0;
    return 1;
}

Hello World Programs

Why write C when you can just write out an ELF file directly?

#define _GNU_SOURCE
#include <sys/mman.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
    unsigned char elf[] = {
        0x7f, 0x45, 0x4c, 0x46, 0xfe, 0xc0, 0xbe, 0x49, 0x80, 0x02, 0x00, 0x83,
        0xf7, 0x01, 0xeb, 0x04, 0x02, 0x00, 0x3e, 0x00, 0xb2, 0x0e, 0xeb, 0x10,
        0x04, 0x80, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x0f, 0x05, 0xb0, 0x3c, 0xeb, 0xdd, 0x00, 0x00,
        0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x38, 0x00, 0x01, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c,
        0x64, 0x21, 0x0a, 0x00, 0x00, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x0a, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };
    int fd = memfd_create("foo", MFD_CLOEXEC);
    write(fd, elf, sizeof(elf));
    fexecve(fd, argv, environ);
}

The eval() function is a classic. It takes code as a string and runs it. The function exists in many dynamic programming languages, like Javascript, Python, Perl, Ruby, and Lisp. Not in C though, because C is a statically compiled language. But what if I told you it existed in C as well?

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <unistd.h>

void eval(char* program, char* symbol) {
  FILE* tmpf = fopen("/tmp/libtmp.c", "w");
  fprintf(tmpf, program);
  fclose(tmpf);
  system("cc /tmp/libtmp.c -shared -o /tmp/libtmp.so");
  ((void(*)(void))dlsym(dlopen("/tmp/libtmp.so", RTLD_LAZY), symbol))();
}

int main() {
  char* hello_world = (char*)"#include <stdio.h>\n"
                             "void hello(void) {\n"
                             "  puts(\"Hello World!\");\n"
                             "}";
  eval(hello_world, "hello");
}

My favorite hello world program. The compiler replaces the while loop with a call to memset(), and it calls our memset rather than the one from the stdlib, because that one hasn't been included. To disable this behavior, you can use the compiler flag -fno-builtin.

#include <stdio.h>

void* memset(void* s, int c, size_t n) {
  puts("Hello world");
}

int main(int argc, char** argv) {
  while (argc--) argv[argc] = 0;
}

Melt your compiler. GCC does the right thing and tries to read /dev/urandom forever. For some reason Clang does not, doesn't even error, and compiles a functional hello world program. Absolutely wild.

#include </dev/urandom>
#include <stdio.h>

int main() {
  puts("Hello world!");
}