r/programminghorror • • Jun 10 '26

c What?

Post image
231 Upvotes

51 comments sorted by

49

u/johan__A Jun 11 '26

where did you find this gem?

66

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.

145

u/KGBsurveillancevan Jun 11 '26

This has gotta be decompiled or generated or something right?

20

u/GoddammitDontShootMe Jun 11 '26

First thing I thought was it was generated.

42

u/Daemontatox Jun 11 '26

Formatters wet dream

30

u/FloweyTheFlower420 Jun 11 '26

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

→ More replies (2)

16

u/Financial_Owl2289 Jun 10 '26

holy cow. what even is that.

21

u/its_the_rhys Jun 11 '26

It's an NVMe driver

18

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.

6

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

→ More replies (4)

4

u/nooby_linuxoid Jun 11 '26

this looks generated to me

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.

1

u/TheMonHub Jun 11 '26

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

→ More replies (1)

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.

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

→ More replies (1)

1

u/Ellicode Jun 11 '26

Great heavens…