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?

}
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