by maldev · 3y ago · 0 comments
BUT, the fact they have multiple goto locations in one function violates this! Only one goto locations ! That goto is goto cleanup, or goto exit. What you do is then check state of each variable you cleanup. Every function should be some variable of this. If anyone writes C in any other style than SESE, you can consider them a subpar C programmer. There's variations like using BOOL and in and out variables. I like them, but there are different styles. But anyone not using a single AND ONLY A SINGLE goto in every function is 100% a subpar C programmer who you should not trust.
BOOL foo()
{
int *allocation;
char *allocation2;
BOOL bRet = FALSE;
const int BUFFSIZE = 10; //NO MAGIC NUMBERS
allocation = resourceallocation(BUFFSIZE); // Malloc, file.open, network open, etc
if(!allocation)
{
DEBUGPRINT("ALLOCAITON FAILED");
bRet = FALSE; //Redundent, but protect against intern
goto cleanup;
}
allocation2 = resourceallocation2(BUFFSIZE); // Malloc, file.open, network open, etc
if(!allocation2)
{
DEBUGPRINT("ALLOCAITON FAILED");
bRet = FALSE; //Redundent, but protect against intern
goto cleanup;
}
...
bRet = TRUE;
cleanup:
//Add error handling if allocation fails
if(allocation)
resourcefree(allocation);
if(allocation2)
resourcefree(allocation2);
return bRet;
}For a big example of substandard coding, see this thread for an egregious wireguard module in BSD. Countless other examples. https://news.ycombinator.com/item?id=33381949
or at least at the time it was written, there werent alternatives that were performant enough.
FYI:
4,879 code results in illumos/illumos-gate for goto
2,587 code results in freebsd/freebsd-src for goto
It's not like any comparable project is immune? Perhaps `goto` says more about how old the code is?