News for hackers. Stuff that matters.
Re:fork() can fail by spudlyo on Wednesday August 20, 2014 @04:01PM (#8204245)
If a function be advertised to return an error code in the event of difficulties, thou shalt check for that code, yea, even though the checks triple the size of thy code and produce aches in thy typing fingers, for if thou thinkest "it cannot happen to me", the gods shall surely punish thee for thy arrogance. [0]

[0]: http://www.lysator.liu.se/c/ten-commandments.html

Re:fork() can fail by quotemstr on Wednesday August 20, 2014 @04:53PM (#8204552)
Counterexample: pthread_mutex_unlock. That function returns an error code, but it cannot possibly fail in a well-formed program. Checking for an error for mutex unlock is pointless: what would you do in response?
Re:fork() can fail by syncsynchalt on Wednesday August 20, 2014 @06:50PM (#8205104)
In the late 90s I had an app ported to multiple unixes. We were having intermittent problems with our HP/UX port which was caused by a mutex being unlocked by a different thread than which had locked it. In that case (which only happened under heavy worker contention) unlock happily returned an error and the mutex was left locked.

Be careful about "can't possibly fail".

Re:fork() can fail by cpeterso on Wednesday August 20, 2014 @05:11PM (#8204661)
> cannot possibly fail in a well-formed program

I think that's your answer. A mutex error return probably indicates an application bug, such as double unlock. You should probably assert or abort on "can't happen" mutex errors.

Programmers are lazy. If they take the time to document an error return value, then you should probably heed their warnings. :)

Re:fork() can fail by quotemstr on Wednesday August 20, 2014 @05:17PM (#8204701)
Indeed. I like using a VERIFY macro:

    #ifdef NDEBUG
    # define VERIFY(x) ((x), 1)
    #else
    # define VERIFY(x) assert((x))
    #endif
Then you can write

    VERIFY(pthread_mutex_unlock(&lock) == 0);
You don't need, however, to consider the possibility of your program continuing to run after pthread_mutex_unlock fails.
Re:fork() can fail by unwind on Thursday August 21, 2014 @03:35AM (#8206255)
Huh? You do realize that the standard assert() macro already is compiled out if NDEBUG is defined, right?

Your code could just as well be written as

    assert(pthread_mutex_unlock(&lock) == 0);
which of course has the added benefit of not inventing anything new, i.e. being standard and immediately understood by anyone who knows the language and its libraries reasonably well.
Re:fork() can fail by unwind on Thursday August 21, 2014 @06:08AM (#8206626)
Replying to self since I can't edit: d'oh. Yes, I totally mis-read the original code. I should have relized why the other comment questioning this practice had been down-voted, heh.

Of course not unlocking the mutex in non-debug builds would be a problem.

Thanks, and sorry.

Re:fork() can fail by screwt on Thursday August 21, 2014 @04:05AM (#8206344)
As per jacquesm's comment below[0], your example would, by default, be compiled out if NDEBUG is defined. So in production you'd never release the lock.

[0] https://news.ycombinator.com/item?id=8206258

Re:fork() can fail by quotemstr on Thursday August 21, 2014 @04:09AM (#8206362)
You're misreading the code. When NDEBUG is defined, we don't use assert, but instead always evaluate the expression. The overall effect is that we always evaluate VERIFY's argument, but only check it in debug builds.
Re:fork() can fail by baruch on Thursday August 21, 2014 @04:05AM (#8206343)
Be very careful with that. When compiled under NDEBUG and the assert gone, the mutex unlock will be gone too and you'll wonder why the application stops working.
Re:fork() can fail by jacquesm on Thursday August 21, 2014 @03:36AM (#8206258)
Never ever put actual code in asserts, not even through clever macros. Stick the return value in a temp var, then check the contents of the temp var in your assertion.
Re:fork() can fail by Hello71 on Wednesday August 20, 2014 @08:29PM (#8205406)
man 3 assert
Re:fork() can fail by thrownaway2424 on Wednesday August 20, 2014 @05:16PM (#8204692)
If you ever think "this can't possibly happen" then you should go right ahead and add an assertion to that effect.
Re:fork() can fail by anon4 on Thursday August 21, 2014 @02:52AM (#8206170)
When "cannot happen" happens:

1. stop everything

2. coredump (if applicable)

3. return -1 from main

Re:fork() can fail by leni536 on Wednesday August 20, 2014 @04:12PM (#8204301)
Note that wasn't the real reason. At the bottom you can see an edit which reads:

"I was wrong about why malloc finally failed! @GodmarBack observes, in the comments, that x64 systems only have an address space of 48 bits, which comes out to about 131000 GB. So, on my machine at least, the malloc finally failed because of address space exhaustion."

Re:fork() can fail by eshyong on Wednesday August 20, 2014 @04:34PM (#8204443)
FYI, I think you replied to the wrong message :)
Re:fork() can fail by samspot on Wednesday August 20, 2014 @04:57PM (#8204569)
Annoyingly, that article refers to the One True Brace Style but doesn't bother to define it or link to it!
Re:fork() can fail by klodolph on Wednesday August 20, 2014 @05:00PM (#8204589)
The "One True Brace Style" is actually a specific style with that name. It is based on the K&R style, with the additional stipulation that all `if`, `else`, `for`, and `while` statements use braces.
Re:fork() can fail by loup-vaillant on Wednesday August 20, 2014 @05:50PM (#8204866)
Re:fork() can fail by samspot on Thursday August 21, 2014 @08:12AM (#8206925)
Thanks. I googled and found a page saying OTBS was different from K&R, which was pretty confusing!