News for hackers. Stuff that matters.
Re:C Programming Guidelines by asveikau on Wednesday September 02, 2015 @12:24AM (#10157419)
This seems like a pretty good list. Focus on error handling is spot-on, most good C is just simply that.

One of my favorite tricks is to make your success case and your error cases look largely the same.

Say you have N allocations and N things that can fail. Lots of people get into trouble when they do:

    void *p, *q, *r, *s;

    p = a();
    if (!p) { return ERROR; }
    q = b();
    if (!q) { free(p); return ERROR; }
    r = c();
    if (!r) { free(p); free(q); return ERROR; }
    // etc..
And this madness just goes on and on. Add a new allocation? You potentially have to change n cleanup blocks.

I breathe a lot easier when I can do [note we abuse the fact that free(NULL) is a no-op]:

       void *p = NULL;
       void *q = NULL;
       void *r = NULL;

       p = a();
       if (!p) goto cleanup;
       q = b();
       if (!q) goto cleanup;
       r = c();
       if (!r) goto cleanup;

    cleanup:
       free(p);
       free(q);
       free(r);
What if the buffer at "r" needs to return something to the caller? It turns out it's really easy to transfer ownership in this pattern:

       // Success case.  Give `r` to the caller.
       *callerPointer = r;
       r = NULL;

       // Then we fall through to the failure block...
       // r is null now so free(r) is harmless.
    cleanup:
       free(p);
       free(q);
       free(r);
Re:C Programming Guidelines by mieko on Wednesday September 02, 2015 @12:43AM (#10157471)
This is The Way To Do It. I take minor issue to calling free(NULL) abuse, though (I know you get it, but allow me to soapbox). I've known programmers who get squeamish relying on this, as if it's a hack, even though it's been mandated by the standard forever. It's like they believe someone would put together a fully-functional libc and miss that simple requirement somehow.

I think the C standard library would be easier to work with if they would've taken this further, and required, for example, fclose(NULL) to be a no-op.

Re:C Programming Guidelines by nitrogen on Wednesday September 02, 2015 @01:12AM (#10157542)
I think the C standard library would be easier to work with if they would've taken this further, and required, for example, fclose(NULL) to be a no-op.

There's always the counterargument that cleanup code that hides a missing allocation can mask bugs elsewhere, if for example a FILE * debug was never fopen()ed, only gets written to in exceptional cases, and fclose(NULL) ignores.

Re:C Programming Guidelines by mieko on Wednesday September 02, 2015 @01:24AM (#10157574)
I'd personally err on the side of clearer clean-up code, but this a good point (and a view shared with glibc and BSD-derived libcs that intentionally segfault in this case).
Re:C Programming Guidelines by userbinator on Wednesday September 02, 2015 @01:44AM (#10157617)
I wouldn't describe that as an "intentional" segfault, but rather a "natural" one, the default behaviour you get on a system with an MMU. The null case with free() is a special-case extra check explicitly inserted in the code.
Re:C Programming Guidelines by mieko on Wednesday September 02, 2015 @02:30AM (#10157725)
"Intentional" was perhaps the wrong word for glibc, but BSD libc (at least on FreeBSD and OS X) actually document it as intentional:

    NOTES
    
    The fclose() function does not handle NULL arguments; they will result in 
    a segmentation violation.  This is intentional - it makes it easier to make 
    sure programs written under FreeBSD are bug free.  This behaviour is an 
    implementation detail, and programs should not rely upon it.
I thought I remembered similar wording for glibc, but I appear to have been mistaken.
Re:C Programming Guidelines by asveikau on Wednesday September 02, 2015 @12:48AM (#10157488)
If you want to get nitpicky, you could also say there is performance cost to jumping into libc.so for free, then having it do a null check for you, then jump back to your code. Maybe that's worse than doing the null check more "locally".

But I do make a point whenever I'm writing C, if I write a cleanup function for a new type of struct or whatever, cleaning up null is always a no-op. It just makes life slightly less tedious. Having to remember which functions do this and which don't, I'll admit, is annoying.

Re:C Programming Guidelines by lucozade on Wednesday September 02, 2015 @09:54AM (#10159118)
I would venture that, if a few (essentially no-op) .so function calls are likely to affect performance to a point that you care, allocating and de-allocating heap memory under those same conditions might be a bit more of an issue.

Depends on the detail of the use case of course, but assuming that NULL cleanup is more the exception than the rule this is definitely a case of profile it first for me.

Re:C Programming Guidelines by caf on Wednesday September 02, 2015 @02:47AM (#10157763)
On the other hand, replicating

  if (p != NULL)
checks across your codebase will have a higher I$ footprint than sharing a single check inside free(), so the overall performance impact could easily be positive.
Re:C Programming Guidelines by hyc_symas on Monday September 07, 2015 @12:32PM (#10182026)
I think I just heard someone say "Mozilla". NSPR's free() wrapper crashes when given NULL.

http://www.openldap.org/devel/gitweb.cgi?p=openldap.git;a=co...

http://www.openldap.org/its/index.cgi/Software%20Bugs?id=778...

Re:C Programming Guidelines by simula67 on Wednesday September 02, 2015 @02:55AM (#10157784)
Why not put each allocated pointer to a linked list and on cleanup pick each element and call free() ? Same approach can be used for files also.
Re:C Programming Guidelines by userbinator on Wednesday September 02, 2015 @01:39AM (#10157604)
That's a very common pattern in Linux kernel code, where there's also nested cleanups, and is one of the most suitable use-cases for a goto.

Although in your example, if the code really was doing only that much, I'd recognise the ||/&& "ladder" structure and write it like this instead:

    if(!(p=a()) && (q=b()) && (r=c()))) {
     free(p);
     free(q);
     free(r);
    }
Re:C Programming Guidelines by asveikau on Wednesday September 02, 2015 @02:05AM (#10157663)
The code example was meant to illustrate the pattern while still being something that's semi-reasonable to write in a web form. Obviously in real life you'd want to do something with p, q, and r, which could make the chain of ANDs [not a pattern I am averse to in general] a bit more clumsy. YMMV.

I guess the other point I am trying to make is that success and failure can hit mostly the same code. Your snippet only checks for failure.

Re:C Programming Guidelines by stevewepay on Wednesday September 02, 2015 @02:17AM (#10157695)
Not that error handling is unimportant, but my opinion is that C is about focusing on resource usage, and keeping very close track over giving back any resource you take, including memory, etc. If you do that, then the number of hard-to-debug runtime bugs drops significantly.
Re:C Programming Guidelines by pjmlp on Wednesday September 02, 2015 @03:36AM (#10157887)
Many languages of that era were about it and did it safer, C just happens to be the one that won the market.
Re:C Programming Guidelines by VOYD on Wednesday September 02, 2015 @01:35PM (#10160776)
"goto", really?
Re:C Programming Guidelines by asveikau on Wednesday September 02, 2015 @02:35PM (#10161154)
Yes, really. As in "goto" your high level languages and stop bothering me about what anyone working in a large enough code base will tell you is the correct way to do error handling in C.

Hint: grep linux sources for "goto" and re-evaluate your life decisions. :P