Re:fork() can fail
by cperciva on Wednesday August 20, 2014 @05:23PM
(#8204725)
If it creates warnings, you can silence them by adding extra parentheses, e.g.,
[ Parent
| on HN ]
Re:fork() can fail
by loup-vaillant on Wednesday August 20, 2014 @05:57PM
(#8204895)
Careful with that example. The parentheses here don't just remove the warning. They remove a bug. This code:
if (buf = malloc(buflen) == NULL)
goto outofmemory;
is actually equivalent to that code: if (buf = (malloc(buflen) == NULL))
goto outofmemory;
So, malloc gives you a pointer, which is compared to the null pointer, giving you either 0 or 1. And that is assigned to buf. Hopefully your compiler will warn you about the type error that spawns from such dark magic.Oops, quite right. I was originally thinking of
if ((rc = pthread_mutex_lock(mtx)))
err("mutex_lock failed: %s", strerror(rc));
but decided to switch to a better-known function at the last minute and completely lost the point.I was originally going to comment saying exactly that — under gcc and clang, at least, just the extra parens will disable the warning; no comparison necessary — but thought to test that the warning is generated in case of a switch statement (and is then disabled by the extra set of parens), which led to realizing the behavior is different.
Just surprised by the inconsistency.