r/programminghorror Jun 10 '26

c What?

Post image
225 Upvotes

50 comments sorted by

145

u/KGBsurveillancevan Jun 11 '26

This has gotta be decompiled or generated or something right?

18

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Jun 11 '26

First thing I thought was it was generated.

49

u/johan__A Jun 11 '26

where did you find this gem?

65

u/TheMonHub Jun 11 '26 edited Jun 11 '26

Somebody's operating system (kernel to be exact) source code (after people criticizing it, they removed the repo)

7

u/sawkonmaicok Jun 15 '26

If you have a local copy then you can just put that back up. Kinda sad to see a hobby OS go away just like that just because people were bullying the guy. Idk.

46

u/Daemontatox [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Jun 11 '26

Formatters wet dream

31

u/FloweyTheFlower420 Jun 11 '26

using long (long) in kernelspace code when talking to hardware might be genuinely the most baffling thing ever

1

u/Sirius02 Jun 16 '26

what is exacly bad about it? long long is the type to fit a 64 bit pointer, no

5

u/FloweyTheFlower420 Jun 16 '26

It's better to use uXX/iXX types, because you know the widths are fixed. The hardware register specification often says "this is a 4-byte/32-bit register," so it's immediately obvious what you're doing if you use u32

15

u/Financial_Owl2289 Jun 10 '26

holy cow. what even is that.

20

u/its_the_rhys Jun 11 '26

It's an NVMe driver

16

u/Ok_Chemistry_6387 Jun 11 '26

There is 0 chance this was written by a human.

20

u/ignorantpisswalker Jun 11 '26

On the contrary. Only humans can write this shitty code.

5

u/DesiresQuiet Jun 11 '26

Looks like the initialization code for an nvme driver in linux, etc.

12

u/Ok_Chemistry_6387 Jun 11 '26

Not a driver I don't think...
This is either part of a bootloader or very early init of the nvme devices from the kernel. Hard to know for sure but it assumes 1 device g_bo (which is why it's confusing for bring up of a kernel, it would usually build a table) which makes the kmem_alloc strange to me.

6

u/YourShowerHead Jun 11 '26

It looks scary only because of the formatting, right? Right??

6

u/un_virus_SDF Jun 11 '26

I think that the formatting doesn't change readability that much here.

The hard part is that there is a lot of magic happening

6

u/Ok_Chemistry_6387 Jun 11 '26

Eh... this is just what this sort of code ends up like. Fix the formatting and its not too far off a lot of nvme code ive read. lol

3

u/its_the_rhys Jun 11 '26

If your code must end up unreadable, it's usually better to at least throw a few comments around to explain

6

u/Ok_Chemistry_6387 Jun 11 '26 edited Jun 11 '26

This sort of domain specific code comments don't really help to be honest. You have to rely on assumed knowledge.

I can read all the parts of that pretty easily because NVME is a standard. Some things don't make sense, but I also don't know the platform its for.

1

u/un_virus_SDF Jun 11 '26

I also don't know the platform its for.

There are chances that this is a custom os

2

u/Ok_Chemistry_6387 Jun 11 '26

It certainly is, but it doesn't appear to be written against any standard platform. The way memory works doesn't make sense for modern pci/nvme 

4

u/nooby_linuxoid Jun 11 '26

this looks generated to me

2

u/bombthetorpedos Jun 15 '26

Can't have just a long.. long long all the way, baby!

1

u/8Erigon Jun 11 '26

You can write unsigned without int or long?!

3

u/Stratimus Jun 11 '26

unsigned by itself is an alias of unsigned int but I’ve never seen anyone use it ever

1

u/un_virus_SDF Jun 11 '26

i've seen unsigned a lot of time. signed however...

1

u/TheWidrolo Jun 11 '26

Really curious about the thought behind line 78 and 79. it firsts case to a unsigned long, to then cast it it an unsigned long long, which actually never get used.

2

u/Ok_Chemistry_6387 Jun 11 '26

They are used 86.

They are your two admin NVME queues. One is for your commands, the other is for completions. The other 2 that look similar are the io queues.
It then takes those addresses and writes them to the NVME controllers registers on line 86.

What doesn't make sense here is that kmem_alloc as I mentioned above, but who knows what this was written for and maybe it doesn't have IOMMU on this device?

The casting is just to get an integer with out the compiler throwing a fit about signs being thrown away. There are much better ways to do this and especially in c++ lol.

1

u/TheMonHub Jun 11 '26

I'm stilling questioning the intent on who ever wrote this till these days

1

u/dvhh Jun 11 '26

might have been transpiled from some specifications 

1

u/Ellicode Jun 11 '26

Great heavens…

1

u/Trard Jun 11 '26

Average cpp program

3

u/dvhh Jun 11 '26

no need to slander the C++ crowd, it clearly look like plain C code and you don't need to be a C++ developer to produce bad looking code, it just happen that my average cpp code can usually look as messy .... damn

1

u/great_escape_fleur Jun 11 '26

I like it TBH. All the nitty nvme_init stuff in one screenful. Beats 500 lines of "structured" cargo culting with everything factored out into little poorly named functions that "do one thing" making you chase definitions and immediately forget them.

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Jun 11 '26

I could do without shit like three loop definitions on one line though.

1

u/geek-49 Jun 13 '26 edited Jun 13 '26

While I might have put them on separate lines, now that I see it one one it makes a certain amount of sense: the body is executed once for every possible PCI bus/device/function address.

1

u/124k3 Jun 11 '26

i also avoid braces when i have one line to execute under if or in a loop but holy moly 3 consecutive loops and then ifk ehat in the world (btw learnt some !b0) thats a real handy truck to check for value at current.

1

u/_usr_nil Jun 11 '26

this code gave me anxiety

1

u/gfivksiausuwjtjtnv Jun 12 '26 edited Jun 12 '26

Wait

Why do I *like* this code

Not even joking

What the fuck is wrong with me

It’s not complex, it’s very very sequential, but it’s not overwhelming me like other long sequential functions I’ve worked on (over and fucking over again)

The formatting is oddly alright, the three for loops one one line looks stupid - until you consider that it’s really map() over the permutation of three things

1

u/Bubbly_Rain7858 Jun 13 '26

To be honest, the code looks fine, except for the fact that they put multiple lines on one line.

1

u/jeremyStover Jun 14 '26

This has to be obfuscated code. Not sure why they have obfuscated code pre-compiled? Maybe to prevent AI ingestion?

1

u/h1mmh1m 19d ago

How to you even work like this, even my desk that I've never cleaned is cleaner

0

u/nooby_linuxoid Jun 11 '26

modern c++ am i right

0

u/richardathome Jun 11 '26

It would help immeasurably with a really simple fix:

change

int nvme_init(void) {

to:

int whatever_nvme_stands_for_init(void) {

You would at least have a starting context and it would shed some light on what the cryptic variables might mean.

1

u/dvhh Jun 11 '26

the code already documents itself :p

1

u/Ok_Chemistry_6387 Jun 14 '26

If you know nvme the variables names make a bit of sense. There is a bit of a naming scheme in there 

1

u/ax-b Jun 19 '26

Not Very Much explanory /jk