I got lost in the opening of this post on reddit.
if (sscanf(buf, "%i", &mode) != 1 || TRUE) be rewritten to
if (TRUE)? Does this assume that the
sscanf never fails?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.Sign up to join this community
Using truth tables, you can show it will always return -EINVAL, thus, don't even need the
if (TRUE) return -EINVAL;
it could be just
FWIW, since I don't have the context, this is almost certainly a bug. The
(mode != 2 || mode != 1)
probably should have been
(mode != 2 && mode != 1)
I think it's assuming that even if sscanf does fail, it doesn't care.
Since the result is being ignored, I don't see the point of having this within the "if" at all. I would guess that the sscanf call was originally on its own, then duff data started arriving (in buf) and messing up the sscanf call, so the "|| TRUE" was added to suppress the error. Classic, Quick and Dirty fix.
Personally, I'd prefer to see this reworked:
sscanf( buf, "%i", &mode ); /* buf may or may not contain a valid mode */
Of course, the data item addressed by &mode might well be left in an inconsistent state - possibly a value parsed from buf, possibly not - but this code doesn't care either way.