Skip to content

Commit 018417d

Browse files
authored
Fix #14577 (Checkers report: unsigned integer overflow can lead to huge string) (danmar#8306)
1 parent 04af809 commit 018417d

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ TESTOBJ = test/fixture.o \
294294
test/testbufferoverrun.o \
295295
test/testcharvar.o \
296296
test/testcheck.o \
297+
test/testcheckersreport.o \
297298
test/testclangimport.o \
298299
test/testclass.o \
299300
test/testcmdlineparser.o \
@@ -757,6 +758,9 @@ test/testcharvar.o: test/testcharvar.cpp lib/addoninfo.h lib/check.h lib/checker
757758
test/testcheck.o: test/testcheck.cpp lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/utils.h test/fixture.h
758759
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testcheck.cpp
759760

761+
test/testcheckersreport.o: test/testcheckersreport.cpp lib/addoninfo.h lib/check.h lib/checkers.h lib/checkersreport.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
762+
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testcheckersreport.cpp
763+
760764
test/testclangimport.o: test/testclangimport.cpp lib/addoninfo.h lib/check.h lib/checkers.h lib/clangimport.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h
761765
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testclangimport.cpp
762766

lib/checkersreport.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,19 @@ std::string CheckersReport::getReport(const std::string& criticalErrors) const
209209
fout << title << std::endl;
210210
fout << std::string(title.size(), '-') << std::endl;
211211

212+
maxCheckerSize = 0;
213+
for (const auto& checkReq: addonInfo.checkers) {
214+
const std::string& checker = checkReq.first;
215+
maxCheckerSize = std::max(checker.size(), maxCheckerSize);
216+
}
217+
212218
for (const auto& checkReq: addonInfo.checkers) {
213219
const std::string& checker = checkReq.first;
214220
const bool active = mActiveCheckers.count(checkReq.first) > 0;
215221
const std::string& req = checkReq.second;
216222
fout << (active ? "Yes " : "No ") << checker;
217223
if (!active && !req.empty())
218-
fout << std::string(maxCheckerSize + 4 - checker.size(), ' ') << "require:" + req;
224+
fout << std::string(maxCheckerSize + 4 - checker.size(), ' ') << "require:" << req;
219225
fout << std::endl;
220226
}
221227
}

test/testcheckersreport.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2025 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
20+
#include "checkersreport.h"
21+
#include "fixture.h"
22+
#include "helpers.h"
23+
#include "settings.h"
24+
25+
#include <cstddef>
26+
27+
class TestCheckersReport : public TestFixture {
28+
public:
29+
TestCheckersReport() : TestFixture("TestCheckersReport") {}
30+
31+
32+
void run() final {
33+
// AddonInfo::checkers
34+
TEST_CASE(addonInfoCheckers);
35+
}
36+
37+
void addonInfoCheckers() const {
38+
AddonInfo a;
39+
a.name = "test";
40+
a.checkers["abcdefghijklmnopqrstuvwxyz::abcdefghijklmnopqrstuvwxyz"] = "123";
41+
Settings s;
42+
s.addonInfos.emplace_back(a);
43+
const std::set<std::string> activeCheckers;
44+
CheckersReport r(s, activeCheckers);
45+
const std::string report = r.getReport("");
46+
const auto pos = report.rfind("\n\n");
47+
ASSERT(pos != std::string::npos);
48+
49+
const char expected[] =
50+
"test checkers\n"
51+
"-------------\n"
52+
"No abcdefghijklmnopqrstuvwxyz::abcdefghijklmnopqrstuvwxyz require:123\n";
53+
54+
ASSERT_EQUALS(expected, report.substr(pos+2));
55+
}
56+
};
57+
58+
REGISTER_TEST(TestCheckersReport)

test/testrunner.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<ClCompile Include="testbufferoverrun.cpp" />
5454
<ClCompile Include="testcharvar.cpp" />
5555
<ClCompile Include="testcheck.cpp" />
56+
<ClCompile Include="testcheckersreport.cpp" />
5657
<ClCompile Include="testclangimport.cpp" />
5758
<ClCompile Include="testclass.cpp" />
5859
<ClCompile Include="testcmdlineparser.cpp" />

0 commit comments

Comments
 (0)