J
jameskuyper
Barry Schwarz wrote:
....
Incorrect. There's many possible return values. There are only two
legal return values from fgets(), but laws get broken. fgets() can be
incorrectly implemented, or undefined behavior in some other part of
the code might have leaked over into my code which calls fgets(). Even
if the first of those two possibilities is pretty remote, the second
is not. Given the negligible performance difference between !=NULL
and ==line, I favor the choice that copes with those possibilities in
a more robust manner.
It might seem that there's nothing I can do if the standard library is
defective or a program had undefined behavior, but that conclusion is
based upon seeing everything is absolutes. In reality, implementations
that are only slightly defective are commonplace; I suspect that they
outnumber perfectly correct implementations, possibly by an infinite
factor. Undefined behavior is, in principle, completely unconstrained;
but in reality, it's commonplace for code with undefined behavior to
behave in a fashion that is, at least initially, only slightly
different from the way it was intended to behave. Defensive
programming techniques can catch such problems earlier than they would
otherwise would be, reducing the amount of damage they can do.
....
I'm confused. There are only two possible return values from fgets:
NULL or the first argument (line in this case). How is
Incorrect. There's many possible return values. There are only two
legal return values from fgets(), but laws get broken. fgets() can be
incorrectly implemented, or undefined behavior in some other part of
the code might have leaked over into my code which calls fgets(). Even
if the first of those two possibilities is pretty remote, the second
is not. Given the negligible performance difference between !=NULL
and ==line, I favor the choice that copes with those possibilities in
a more robust manner.
It might seem that there's nothing I can do if the standard library is
defective or a program had undefined behavior, but that conclusion is
based upon seeing everything is absolutes. In reality, implementations
that are only slightly defective are commonplace; I suspect that they
outnumber perfectly correct implementations, possibly by an infinite
factor. Undefined behavior is, in principle, completely unconstrained;
but in reality, it's commonplace for code with undefined behavior to
behave in a fashion that is, at least initially, only slightly
different from the way it was intended to behave. Defensive
programming techniques can catch such problems earlier than they would
otherwise would be, reducing the amount of damage they can do.