if (daemon && !test_mode) {
int pid;
switch (pid = fork()) {
case -1: /* Error */
fatal_error("Failed to fork");
case 0: /* In child */
break;
default: /* In parent */
write_pid(pid_file, pid, !test_mode);
exit(0);
}
} else {
write_pid(pid_file, getpid(), !test_mode);
}edit: i know it's not totally analogous since removing the exit and not noticing there's no break is a lot more likely than just randomly removing a break, but the "let's prevent someone clumsy from screwing this code up in the future" argument always makes me laugh a little.
Is a break after an exit too far? It may well be, but it's less clearly too far than two breaks are.
(And if you really want to fall through, I could add a `fallthrough` keyword.)
If you don't care about portability, it's an easier call to make.
if ((buf = malloc(buflen)) == NULL)
goto outofmemory; 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. 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.Just surprised by the inconsistency.
I think this just came from being drilled in school on the importance of checking error return values. No matter how unlikely never assume that something can't happen. If it really can't you should at the very least assert on it.
[1] - https://github.com/jervisfm/W4118_HW1/blob/master/shell/shel...