6 ms·
This fix leaves a variable uninitialized, and depends on funny logic to later give it a value. The general structure should be FILE *fp = stdin; /* sane def
by kazinator 12y ago
This fix leaves a variable uninitialized, and depends on funny logic to later give it a value.
The general structure should be
FILE *fp = stdin; /* sane default */
then override with a new pointer from fopen if necessary.
In fact, looking at this more closely, note how fp is scoped to the body of the for loop. That's where it should be defined:
for (firsttime = 1; ; firsttime = 0) {
FILE *fp = stdin;
/* logic flips it to fp = fopen(...) as necessary */
}
[Note: the C compiler Joy was working with probably didn't support block scope declarations except at the top of the function. This comment applies to merging the fix using the environment of today.]
Note how the fclose at the bottom of the loop ends up closing stdin in the case that fp == stdin! It ensures that this only happens once, but you have to analyze it to see that this is true. I would write this as:
if (fp != stdin) /* defensive! */
fclose(fp);
Making it an infinite loop terminated by calling exit is unnecessary. There is already a continue in the loop, and a break in an inner loop; another break instead of the exit, and a "return status;" at the bottom wouldn't hurt. Better yet:
/* Enter loop whenever there are args left to
process, or even if there are no args if it is
the first time through (to process stdin instead). */
for (firsttime = 1; firsttime || *argv; firsttime = 0) {
FILE *fp = stdin;
if (*argv) {
/* have args; do the fopen to replace stdin */
/* set status = 1 and continue if cannot fopen */
}
/* do head logic */
if (fp != stdin)
fclose(fp);
}
return status;
C itself was only a couple of years old when that code was written originally, and the systems were small. Whereas I've been using it since 1989. Not criticizing the original!
- michael_h 12y agoAs I understand it, you can fclose(stdin) without issue on almost all POSIX systems.
- kazinator 12y agoYes, I know this, and in a throwaway little program where you are sure you are the last one to ever touch stdin (because you're calling exit right there in the loop, for instance) it is okay.
- derefr 12y agoIn old sysvinit-style daemons, closing stdin/stdout/stderr (or redirecting them to /dev/null) was a necessary part of the daemon initialization process.
- cbd1984 12y agoSure, but it's surprising, and in code, 'surprising' usually translates to 'wrong'.
- mnarayan01 12y agoNot sure that scoping to the block + unconditionally doing the initialization actually helps readability, though I do think doing one or the other would. Also the original is super efficient which is pretty cool, and (at least when correct) is arguably more readable (probably not so much re. extendable) since it seems like there's no way to look at the code and think you know what it's doing when you actually do not.
- saurik 12y agoI would argue your insistence on giving the variable a "sane default" is a matter of opinion. I personally am much happier seeing a variable left uninitialized than ever seeing it double-assigned, as to me a fundamental semantic logic error has occurred if you are overriding the value of this kind of variable, especially as part of a conditional later where it might not be clear whether or not it happened: I really would prefer to look at the variable being assigned and think "ok, now we know the value". The compiler is entirely able to determine whether or not the variable has been initialized in all possible control flows for a case like this (at least, once we move the scope of the variable to inside of the for loop, of course), so there is no safety benefit to hoisting that default assignment out, nor is it saving a meaningful amount of code: it only serves to obfuscate the "default" control flow by making what had been an explicit if/else into an implicit if/default :/.
- pierrebai 12y agoAlways initialize your variable. There is not aesthetic argument, but there are multiple software engineering argument against failing to initialize variables. For starter, you don't have prescience. You can't prevent future version from growing new alternate code paths. As the code evolve, checking that every initialization-free variable will still get initialized is a burden and easily overlooked. Then there are more potent evolution that will make it just impossible: the code will get generalized, or moved into a library, or get a a context object to be able to run multiple instances. Or will get a state machine to handle complex combination of options. You're relying on the compiler telling you the variable will get initialized. But the compiler may change. Or the human compiling it might not look at warnings. OTOH, double-iniitalization is mostly a myth. If the compiler can tell the variable may not get any initialization and warn you are exactly the same as the case the optimizer will be able to see the double init and optimize it out. I've looked at generated code and see compilers properly eliminate redundant stores. You're better off betting on optimization and get bitten by occasional double-stores than get bitten by uninitialized variables and give system penetrators yet another free lunch.
- dllthomas 12y agoIf you use static analysis, defensively initializing to a wrong value may mean that your tooling doesn't tell you when you fail to correctly set it from that wrong value.