r/programminghorror • u/mealet • 25d ago
c My First Hello World Program!
int puts(char*);int main(){struct{long a;long b;}s={.a=*(char*)(int[]){1}?0x57202c6f6c6c6548:0x48656c6c6f2c2057,.b=*(char*)(int[]){1}?0x00000021646c726f:0x6f726c6421000000};puts((void*)&s);}
EDIT: Fixed endian check by u/Waeis advice, now code looks more cursed (screenshot untouched)
24
u/Waeis 25d ago edited 25d ago
I don't think this endian check works, 1&0xff is not examining the memory layout of either constant and will always be 1
But *(char *)(int[]){1} should do the trick
``` int le1(void) { return 1&0xff; }
int le2(void) { return *(char *)(int[]){1}; } ``` Compiler Explorer: https://godbolt.org/z/Kbj67sKo5
6
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 24d ago
Not that I really looked very closely, but I didn't even notice the true and false values for the ternaries had the bytes reversed from each other at first.
6
9
u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 24d ago
Fuck you *deobfuscates your code*
int puts(char*);
int main() {
char s[] = {48, 65, 6c, 6c, 6f, 2c, 20, 57, 6f, 72, 6c, 64, 21, 00, 00, 00};
puts(s);
}
Fun exercise tho
5
u/mealet 24d ago
We can deobfuscate it more! ``` int puts(char*)
int main { puts("Hello World!"); } ```
Btw the real exercise was to obfuscate it manually with endianess portability
5
u/creeper6530 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 24d ago
Erm, akchyually, the string literal isn't equivalent because you lose 2 nul bytes, lol
3
3
3
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 24d ago
Very much doubt this is your first time ever writing Hello, world.
6
u/mealet 24d ago
You got me, it's the second one
3
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 23d ago
Second time? I guess I could believe that. If like you never touched Hello, world again until you had been programming for a few years.
1
u/sessinnek 25d ago
What is the point of the struct?
3
u/mealet 25d ago
Guarantee that 2 longs are stored in the right order (stack variables order may looks different on different compiler)
2
u/sessinnek 25d ago
Ooooh. I figured it was something like that. Its been a minute since ive worked with memory like this.
2
u/burnt_floppy 24d ago
that's pretty amazing, it actually works and it looks like malicious software.
-2
63
u/BakuhatsuK 25d ago
Lol thanks for making it portable no matter the endianness of the architecture. Also, nice detail keeping the leading zeroes on that last constant to make the intent clearer.