62 points · by ingve · 5y ago · 62 comments
Well, the function should be called somewhere, even if it's ignored when null. I can't think of a legitimate case for an unused parameter - it might be #ifdef-ed out in some cases, but it should still be referenced somewhere.
https://xkcd.com/1172/ in action
“In order to get a warning about an unused function parameter, you must either specify -Wextra -Wunused (note that -Wall implies -Wunused), or separately specify -Wunused-parameter.”
So, it seems one can do
-Wunused-parameterIt's a bad idea to program an analyzer so that it just issues a warning to unused arguments. Such analyzer would produce many false positives, which is why many developers don't look at (or disable) these warnings in their compilers/analyzers.
The PVS-Studio analyzer implements sort of empirical "magic". PVS-Studio relies on the fact that there are arguments of the same type and some of them are not used, while the other ones are used several times. At the same time, there are a number of exceptions to the rule. For example, the diagnostic is not triggered if the number of unused arguments exceeds two.
All this allows the V751 diagnostic to issue few false positives, which makes the tool surpass its competitors. To be exact, when developing PVS-Studio, we do not implement rules if we cannot make them better than those of the compilers - https://pvs-studio.com/en/blog/posts/0802/ . Thanks to the diagnostic I described above, one can find interesting errors - https://pvs-studio.com/en/blog/examples/v751/ .
P.S. The PVS-Studio analyzer also provides a "stupid" version of this diagnostic - V2537 https://pvs-studio.com/en/docs/warnings/v2537/ . It was developed to check code against MISRA C and MISRA C++ standards. But the case above was special and by default this diagnostic was disabled - same as the other ones related to MISRA.
int cmp(int a, int b) {
return a - b;
}
This can easily overflow or underflow. For example, if a = INT_MAX and b < 0.I don't count it against the person interviewing, because its common and arguably reasonable if your values are all < INT_MAX/2.
Regarding the OpenSSL example presented here, wouldn't any decent IDE catch an unused parameter in a function? Why is a separate static analyzer necessary for this
Because this is a nail this co-founder of PVS-Studio saw while holding the hammer he wants to sell.
By the way, it seems to me this error does not have much to do with comparison functions, it is a mistake that can be made in many kinds of functions.
Unused parameters are warned against in C (hence all the UNUSED macro hacks) and no static analyzers are going to save you if you don't address compiler warnings, they are just going to add more warnings to that pile of warnings you already have (or maybe don't get because the right warnings are not enabled).
(I'm sure their blog is full of cases a static analyzer would handle which compilers won't warn you for though)
Still surprising to see such an error in OpenSSL and like others here I would be interested to know why it was not caught.
I'm a bit skeptical of the claim "The code quality is excellent", if it was this would be a lone warning emitted by the compiler, and surely it would have been fixed then.
That said, I believe the two mainstream C compilers (GCC and Clang) can both be configured to emit a warning here.
That is, committing this code should have had the same impact as if it was missing the semi-colon, the library doesn't build, tree is on fire, fix before you do new work.
Of course if your code is in sufficiently bad state, you might find it's frustrating to have say 500 bugs to solve before you can get the CI pipeline to output artefacts. I think that means you didn't have good software and you must fix those bugs first, but if you're convinced the software is good it can be tempting to instead disable the diagnostics telling you otherwise and press on.
You have 178 seconds to live:
He was generally cranky ["curmudgeonly"? -Ed.] about the code, and tried submitting quality fixes, where possible; but this was years ago.
PVS-Studio looks great. I wish it worked on Swift. SwiftLint has its limitations.
If I recall, the claim was that OpenSSL prioritized issues paid for by companies, and the alot was left to rot. I'm curious if your friend saw the same things?
He didn't speculate as to "why," but he said the project was very buggy, and rather "messy."
To be fair, we worked for a company (he was one of my employees) that was anal about Quality, and hard to please. It rubbed off on us.
This is just a typo, not really a quality ad for some code quality monitor.
It blows my mind sometimes what IntelliJ, in particular, catches... like code branches that can never execute due to intricate conditional blocks in the same block of code... forgetting to use a method argument is not even worth a mention :D
Look at just how many issues IntelliJ can detect for Java: https://www.jetbrains.com/help/idea/list-of-java-inspections...
I.e. a poorly written comparison function compares multiple expressions and the use of && short-circuit the logic, result in a timing attack.
Would love to read an article about this topic, i.e. how to write a function whose execution time is constant regardless of the inputs, and thus not leak any side-channel information to the attacker.
I was expecting to see an example of structure comparisons such as struct date {int year; int month; int day;} which are easy to get wrong unless you've internalized the pattern (or use modern C++ where it'll generate the comparison operators automatically for you).
I suppose that a problem with C++ (and the more feature-filled source control systems) is that it's dangerous to mix different levels of programmer sophistication in a team. The people are reflected in the code particularly as the obfuscation increases.
I don't know where this started (more, when it got popular) but it's such a cop out and lazy argument to anything safety-related.
It adds nothing to the discussion and provides no insight or substance, yet people parrot it often, especially around "language lawyer"-type HN articles.
Moreover, it's wrong. Not every function has a bug. Yes, there are ways to prove this. No, not every function needs to protect against the computer being struck by lightning, or any other fault, in every case. It's such a weird and wrong argument and it's always said with such confidence.
This doesn't excuse writing buggy code, same as you should always handle a gun with care, but it promotes good behaviour around it.
> Yes you can throughly check for it in some place and be rather sure
Look up formal verification. You can prove, mathematically, that code is bug free.
Ok, you're right. Every non-trivial function has a bug.