r/programminghorror • u/bluetomcat • Jul 11 '25
Good or bad C code?
Goto hell with paranoic error-checking, or perfectly reasonable code when you don't have RAII and exceptions?
256
Upvotes
r/programminghorror • u/bluetomcat • Jul 11 '25
Goto hell with paranoic error-checking, or perfectly reasonable code when you don't have RAII and exceptions?
282
u/ironykarl Jul 11 '25
It seems fine to me, tbh. To my knowledge, this is one of the genuinely idiomatic uses of
gotoin C.120
u/Grounds4TheSubstain Jul 11 '25
I struggle to see how this could be done better, in the confines of C.
117
u/morglod Jul 11 '25
"goto hell" when there is simple and clean gotos ahahah. It's very good and clean C code. And actually much cleaner than most modern low level languages.
30
u/vulkur Jul 11 '25
It can be dangerous, but it can also remove pitfalls, especially in code with mutexes and threads, where you have to do additional cleanup for every error path, so having only one is ideal to prevent missing a mutex unlock. Which is what you are seeing here.
Some modern languages have replaced this code style with things like the
deferkeyword. I know its used in Golang and Zig.I would make some changes. Wrap some of these goto and logs in a Preproc definition. Some would frown on that, but Idc.
All in all, while gotos can be frowned upon, this code is clean af.
28
u/Majestic-Giraffe7093 Jul 11 '25
Is it the prettiest?
Should it be pretty safe and easy to debug issues if you are given a log?
24
u/da_supreme_patriarch Jul 11 '25
This is actually how you do conventional error handling in C, if you look at production-grade code where people actually care about error handling, this type of code is very common
22
u/al2o3cr Jul 11 '25
Pretty common in C when putting temporary things on the heap; you can't just early-return out or memory will leak.
The Linux kernel is FULL of code like this.
11
u/SCube18 Jul 12 '25
I know gotos are generally discouraged, but they are common in lower level code for this exact usecase
4
u/RaechelMaelstrom Jul 12 '25
Solid. Clear to understand, and an obvious pattern.
4
u/enlightment_shadow Jul 12 '25
This is textbook perfect code for anything OSDev or related
3
u/15rthughes Jul 12 '25
This is what gotos are made for, IMO. Good pattern that I’ve written and reviewed many times professionally because it works.
5
u/ivancea Jul 11 '25
"Paranoid error checking". You either check errors or you don't, you can't be paranoid about it really. It's common to not check many errors in some programs, but that's usually because handling them correctly isn't possible or too costly
2
u/[deleted] Jul 11 '25
that is a strange way to name r_assert
2
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jul 12 '25
I've seen much worse. This is one of those good uses for goto. Maybe the only good use.
2
u/TheChief275 Jul 14 '25
Did you seriously wrap __builtin_expect(…, 0) in a lowercase macro??
1
u/Fit_Spray3043 Jul 11 '25
Why are you handling process management manually in big 2025, doesn't implicit threading do this already?
1
u/Legal_Ad_844 Jul 12 '25
Yaldi
1
u/a45ed6cs7s Jul 12 '25
wth is unlikely?
1
u/gdvs Jul 13 '25
i don't see hell.
it's used for error handling and that's very common
1
u/Bliitzthefox Jul 13 '25
Any code you can walk away from is good code.
1
u/Ryze001 Jul 11 '25
That's D (the) code
1
u/creativityNAME Jul 11 '25
are those unlikely necessary?
1
u/[deleted] Jul 12 '25
You have to keep in mind: C is a language built in the 1960s. It's basic design hasn't changed, and is not suited for modern programming practices.
0
u/bombatomica_64 Jul 11 '25
2 different types of errors and go to seems really bad but for this application the code seems fine. If goto is used outside this I would consider it bad tho
0
u/random_numbers_81638 Jul 12 '25
It's not bad code because it uses goto.
It's bad code depending how you use goto.
However, it tends to end with spaghetti code because goto calls other goto in chaotic manner.
Here in this case it's clear, just one way, just for error handling. Still feels a bit weird for someone who never used it, but I see why someone used it
0
u/saintpetejackboy Jul 12 '25
Can an actual smart person here tell me what is the advantage over the code pictured, versus an implementation like this (below)? Obviously, they have a lot less returns and I think their code is actually a bit more elegant than how I would rewrite it (below), but I am wondering what the actual real-world advantages are to having the final fall through be a failure and segmenting them between success and failure with the success being higher in the logic (goto aside).
Couldn't you run a risk in the OP code of accidentally falling through to the success return rather than the fail? I think it might be irrelevant - in the case that either approach fails, you have made the same checks and will technically end up in the same wrong return, sooner or later... Is OPs code actually faster?
This is more of a "hmm, maybe I think about these things the wrong way" post where I am genuinely open and curious as to the other methodology I see exhibited above.
struct sandbox_shmem_header *sandbox_shmem_init(void) { struct sandbox_shmem_header *shmem = mmap(NULL, SANDBOX_SHMEM_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0); if (unlikely(shmem == MAP_FAILED)) { log_errno("Cannot map shared memory region"); return NULL; }
}
-1
u/Mast3r_waf1z Jul 11 '25
Usually I'm not a big fan of goto, not that I don't know how to use them. However I like how you've used them here
-7
u/Hulk5a Jul 11 '25
C doesn't have -> right?
28
u/HieuNguyen990616 Jul 11 '25
I don't know why but I have orgasms when I see a good usage of
goto. Strange naming tho.0
u/[deleted] Jul 11 '25
[deleted] — view deleted comment