Skip to content

Commit 60ba520

Browse files
committed
fix polyspace +N range overflow in inline suppressions
1 parent 78a38ed commit 60ba520

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎lib/suppressions.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <cstring>
3434
#include <exception>
3535
#include <functional> // std::bind, std::placeholders
36+
#include <limits>
3637
#include <sstream>
3738
#include <stdexcept>
3839
#include <utility>
@@ -850,7 +851,7 @@ std::list<SuppressionList::Suppression> polyspace::Parser::parse(const std::stri
850851
if (rangeValue > 0) {
851852
suppr.type = SuppressionList::Type::block;
852853
suppr.lineBegin = line;
853-
suppr.lineEnd = line + rangeValue;
854+
suppr.lineEnd = (rangeValue > std::numeric_limits<int>::max() - line) ? std::numeric_limits<int>::max() : (line + rangeValue);
854855
}
855856
else if (polyspaceKind == polyspace::CommentKind::Regular)
856857
suppr.type = SuppressionList::Type::unique;
@@ -886,7 +887,8 @@ int polyspace::Parser::parseRange(const std::string& comment, std::string::size_
886887
int ret = std::stoi(range);
887888
pos = endpos;
888889
return ret;
889-
} catch (const std::invalid_argument &) {}
890+
} catch (const std::invalid_argument &) {
891+
} catch (const std::out_of_range &) {}
890892
return 0;
891893
}
892894

‎test/testsuppressions.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include <cstring>
3636
#include <initializer_list>
37+
#include <limits>
3738
#include <list>
3839
#include <map>
3940
#include <memory>
@@ -2175,6 +2176,10 @@ class TestSuppressions : public TestFixture {
21752176
pos = 0;
21762177
ASSERT_EQUALS(0, polyspace::Parser::parseRange(" +A ",pos));
21772178
ASSERT_EQUALS(1U, pos);
2179+
2180+
pos = 0;
2181+
ASSERT_EQUALS(0, polyspace::Parser::parseRange(" +99999999999 ",pos));
2182+
ASSERT_EQUALS(1U, pos);
21782183
}
21792184

21802185
void polyspaceParseIds() const {
@@ -2365,6 +2370,12 @@ class TestSuppressions : public TestFixture {
23652370
{ "/* polyspace +3 MISRA2012 : 2.7 */", 1 },
23662371
{ { "premium-misra-c-2012-2.7", 1, "", SuppressionList::Type::block, 1, 4 } }
23672372
);
2373+
2374+
testPolyspaceSuppression(
2375+
"", "--misra-c-2012",
2376+
{ "/* polyspace +2147483647 MISRA2012 : 2.7 */", 2 },
2377+
{ { "premium-misra-c-2012-2.7", 2, "", SuppressionList::Type::block, 2, std::numeric_limits<int>::max() } }
2378+
);
23682379
}
23692380

23702381
void polyspaceBlock() const {

0 commit comments

Comments
 (0)