I got lost in the opening of this post on reddit.
How can 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 communityI got lost in the opening of this post on reddit.
How can if (sscanf(buf, "%i", &mode) != 1 || TRUE)
be rewritten to if (TRUE)
? Does this assume that the sscanf
never fails?
The author of the code calls sscanf and then ignores its return value assuming it is true. You can replace the code with 'if (TRUE)' provided that you call sscanf first.
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
return -EINVAL
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.
Surely this is a smell? Using 'OR true' in a conditional statement is jarring to read and of no functional value.
if(statement OR true) { ... } would be better written as statement; { ... } ?