match/wcmatch needs an at()
It would ever so nice if the regex match result had an alternative at() to use instead of the index operator. Some linting/analysis tools assume that this is an unchecked array operation and complain about it. This requires either lots of suppressions, or a lot of bogus warnings that hide real indexing operations that might be an issue.
It would be trivial to provide an at() that just turned around called the operator. Those who have this concern could avoid it and there would approximately zero cost to the implementors.
Posted Aug 25, 2020, 4:22 PM UTC
Or fixing of poorly implemented linters.
But yeah, adding
atwouldn't be a bad thing in my opinion.Do we really require them to know whether a [] operation is range checked or not? They aren't in the standard containers, so just because it's an overridden operator doesn't mean it's safe.
Given that anyone can write a
[]operator it's the linters fault for not checking what type the thing[]is being called on is. Yes, it's a poorly written linter that produces false positives based on bad assumptions.But it cannot possibly know if it's safe or not, so it has to assume the worst. As I pointed out, the standard containers (one of the biggest uses of the index operator in a lot of code bases probably) are explicitly not range checked.
The linter cannot possibly know that any operation is safe. The default is unsafe. It should therefore only warn about functions it knows for a fact are unsafe where a safe alternative exists.
Again, this is a linter problem, not an API problem.
If the tool is going to issue a warning, then we should require them to know that there is cause to be warned, yes.
Thus it may be reasonable for a tool to warn about unchecked access in that case. But that is not the case here, and the diagnostic is a false positive.
What might be a good addition to the standard are function attributes
[[checked]]and[[unchecked]]- maybe[[checked(bool)]]- which could be used to by tools to make assumptions about safety of those functions. A problem with this is that safety is a rather abstract and subjective concept and this may lead to misunderstandings in more complex cases.There are quite a few annotations that could (and should) be added to the language to make analysis tools vastly more effective. I've argued for this a lot in the past.
Yes. "More warnings" != "better tool".
[] is unchecked in std containers - ok then, warn about std containers. It's not rocket science to compare the class name with "vector".
If there are both [] and at() in a user class - ok then, warn about it, "because the convention". It's not rocket science to check the class definition.
But if the analyzer's logic is simply "all [] operators are unconditionally unholy" - it's not an analyzer, it's rubbish. Even if it's Microsoft's.
Sigh... The point is not unconditionally unholy, the point is 'potentially a problem', which it often is because index errors are common sources of bad things happening. You want it to warn you about them, but it's useless if it's lost in noise from things that we know aren't a problem but it doesn't. And putting in a bunch of special cases is not really optimal either. As others have pointed out, this does nothing for non-std stuff.
It's C++. Everything is "potentially a problem", including integer addition and division.
Used to be. I've only seen a few during the last 10 years, presumably due to the industry shift to iterators & ranges, but YMMV or course.
As I've already mentioned above, a tool can warn if a non-std thing has both [] and at(), and it would make sense. But what's the point to complain about [] if there are no alternatives?