Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <cctype> // std::isdigit, std::isalnum, etc
#include <cstring>
#include <exception>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <utility>
Expand Down Expand Up @@ -885,7 +886,8 @@ std::list<SuppressionList::Suppression> polyspace::Parser::parse(const std::stri
if (rangeValue > 0) {
suppr.type = SuppressionList::Type::block;
suppr.lineBegin = line;
suppr.lineEnd = line + rangeValue;
// line can be negative, so limit the lhs; rangeValue > 0 keeps the sum <= INT_MAX
suppr.lineEnd = std::min(line, std::numeric_limits<int>::max() - rangeValue) + rangeValue;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if C++26 was available then we could have replaced with std::saturating_add(line, rangeValue) that would be easier to read .. a saturating_add utility function might be interesting to add; do you have any feeling if there are other signed integer additions/subtractions that we should protect?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, a helper reads better than the explicit std::min. The Makefile still builds with -std=c++11 though, so std::add_sat (that's the C++26 name, in <numeric>) is a long way off. A saturating_add in lib/utils.h with the same signature would at least be swappable later.

On the wider question, my feeling is that these sites split into three groups and only the first one actually wants saturation:

  • Bounded positions and sizes that are only ever compared, like lineEnd here. Block matching just does the two range comparisons at lib/suppressions.cpp:481 and :733, so clamping is harmless and a saturating_add fits cleanly.
  • Value analysis bounds, where a silent clamp would invent a bound that isn't there. The code already detects and bails out instead, via ValueFlow::isSaturated (lib/vf_common.h:48) and the addOverflows/subtractOverflows/multiplyOverflows/shiftOverflows predicates in lib/programmemory.cpp:865.
  • Arithmetic that models the target machine, such as calculate(), where wrapping is the correct result rather than either of the above.

So the thing I'd find more useful than saturating_add on its own is promoting those four *Overflows predicates out of lib/programmemory.cpp. They're file-static today, so nothing else can reach them even though the same checks are wanted elsewhere.

One unguarded spot I noticed while looking: lib/infer.cpp:131 and :141 do minValue->intvalue + 1 and maxValue->intvalue - 1 with no saturation check, which is the same shape as the setConditionalValues call in valueflow.cpp that does have one. I haven't built a reproducer for it, so treat that as inspection only.

Happy to do the utility and those conversions as a separate PR if you want them, and I'd ask for a trac ticket for that one. This PR I'd leave as it stands.

}
else if (polyspaceKind == polyspace::CommentKind::Regular)
suppr.type = SuppressionList::Type::unique;
Expand Down Expand Up @@ -921,7 +923,11 @@ int polyspace::Parser::parseRange(const std::string& comment, std::string::size_
int ret = std::stoi(range);
pos = endpos;
return ret;
} catch (const std::invalid_argument &) {}
} catch (const std::invalid_argument &) {
// not a number
} catch (const std::out_of_range &) {
// does not fit in an int
}
return 0;
}

Expand Down
17 changes: 17 additions & 0 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <cstring>
#include <initializer_list>
#include <limits>
#include <list>
#include <map>
#include <memory>
Expand Down Expand Up @@ -2206,6 +2207,10 @@ class TestSuppressions : public TestFixture {
pos = 0;
ASSERT_EQUALS(0, polyspace::Parser::parseRange(" +A ",pos));
ASSERT_EQUALS(1U, pos);

pos = 0;
ASSERT_EQUALS(0, polyspace::Parser::parseRange(" +99999999999 ",pos));
ASSERT_EQUALS(1U, pos);
}

void polyspaceParseIds() const {
Expand Down Expand Up @@ -2396,6 +2401,18 @@ class TestSuppressions : public TestFixture {
{ "/* polyspace +3 MISRA2012 : 2.7 */", 1 },
{ { "premium-misra-c-2012-2.7", 1, "", SuppressionList::Type::block, 1, 4 } }
);

testPolyspaceSuppression(
"", "--misra-c-2012",
{ "/* polyspace +2147483647 MISRA2012 : 2.7 */", 2 },
{ { "premium-misra-c-2012-2.7", 2, "", SuppressionList::Type::block, 2, std::numeric_limits<int>::max() } }
);

testPolyspaceSuppression(
"", "--misra-c-2012",
{ "/* polyspace +5 MISRA2012 : 2.7 */", -1 },
{ { "premium-misra-c-2012-2.7", -1, "", SuppressionList::Type::block, -1, 4 } }
);
}

void polyspaceBlock() const {
Expand Down
Loading