Skip to content

Commit a3cb101

Browse files
authored
fixed #13485 - symbol names in error messages were not serialized (danmar#7142)
1 parent 7db4566 commit a3cb101

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

lib/errorlogger.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ std::string ErrorMessage::serialize() const
284284

285285
serializeString(oss, saneShortMessage);
286286
serializeString(oss, saneVerboseMessage);
287+
serializeString(oss, mSymbolNames);
287288
oss += std::to_string(callStack.size());
288289
oss += " ";
289290

@@ -311,9 +312,9 @@ void ErrorMessage::deserialize(const std::string &data)
311312
callStack.clear();
312313

313314
std::istringstream iss(data);
314-
std::array<std::string, 9> results;
315+
std::array<std::string, 10> results;
315316
std::size_t elem = 0;
316-
while (iss.good() && elem < 9) {
317+
while (iss.good() && elem < 10) {
317318
unsigned int len = 0;
318319
if (!(iss >> len))
319320
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - invalid length");
@@ -339,7 +340,7 @@ void ErrorMessage::deserialize(const std::string &data)
339340
if (!iss.good())
340341
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - premature end of data");
341342

342-
if (elem != 9)
343+
if (elem != 10)
343344
throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - insufficient elements");
344345

345346
id = std::move(results[0]);
@@ -362,6 +363,7 @@ void ErrorMessage::deserialize(const std::string &data)
362363
certainty = Certainty::inconclusive;
363364
mShortMessage = std::move(results[7]);
364365
mVerboseMessage = std::move(results[8]);
366+
mSymbolNames = std::move(results[9]);
365367

366368
unsigned int stackSize = 0;
367369
if (!(iss >> stackSize))

test/cli/other_test.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2672,4 +2672,50 @@ def test_duplicate_suppressions_mixed(tmp_path):
26722672
assert stdout.splitlines() == [
26732673
"cppcheck: error: suppression 'uninitvar' already exists"
26742674
]
2675-
assert stderr == ''
2675+
assert stderr == ''
2676+
2677+
2678+
@pytest.mark.xfail(strict=True)
2679+
def test_xml_builddir(tmp_path): # #13391 / #13485
2680+
build_dir = tmp_path / 'b1'
2681+
os.mkdir(build_dir)
2682+
2683+
test_file = tmp_path / 'test.cpp'
2684+
with open(test_file, 'wt') as f:
2685+
f.write("""
2686+
void f(const void* p)
2687+
{
2688+
if(p) {}
2689+
(void)*p; // REMARK: boom
2690+
}
2691+
""")
2692+
2693+
args = [
2694+
'-q',
2695+
'--enable=style',
2696+
'--cppcheck-build-dir={}'.format(build_dir),
2697+
'--xml',
2698+
str(test_file)
2699+
]
2700+
exitcode_1, stdout_1, stderr_1 = cppcheck(args)
2701+
assert exitcode_1 == 0, stdout_1
2702+
assert stdout_1 == ''
2703+
# TODO: handle version
2704+
assert (stderr_1 ==
2705+
'''<?xml version="1.0" encoding="UTF-8"?>
2706+
<results version="2">
2707+
<cppcheck version="2.17 dev"/>
2708+
<errors>
2709+
<error id="nullPointerRedundantCheck" severity="warning" msg="Either the condition &apos;p&apos; is redundant or there is possible null pointer dereference: p." verbose="Either the condition &apos;p&apos; is redundant or there is possible null pointer dereference: p." cwe="476" file0="{}" remark="boom">
2710+
<location file="{}" line="5" column="12" info="Null pointer dereference"/>
2711+
<location file="{}" line="4" column="8" info="Assuming that condition &apos;p&apos; is not redundant"/>
2712+
<symbol>p</symbol>
2713+
</error>
2714+
</errors>
2715+
</results>
2716+
'''.format(test_file, test_file, test_file))
2717+
2718+
exitcode_2, stdout_2, stderr_2 = cppcheck(args)
2719+
assert exitcode_1 == exitcode_2, stdout_2
2720+
assert stdout_1 == stdout_2
2721+
assert stderr_1 == stderr_2

test/testerrorlogger.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class TestErrorLogger : public TestFixture {
6464
TEST_CASE(DeserializeInvalidInput);
6565
TEST_CASE(SerializeSanitize);
6666
TEST_CASE(SerializeFileLocation);
67-
TEST_CASE(SerializeAndDeserializeRemark);
67+
TEST_CASE(SerializeAndDeserialize);
6868

6969
TEST_CASE(substituteTemplateFormatStatic);
7070
TEST_CASE(substituteTemplateLocationStatic);
@@ -353,6 +353,7 @@ class TestErrorLogger : public TestFixture {
353353
"1 1"
354354
"17 Programming error"
355355
"17 Programming error"
356+
"0 "
356357
"0 ", msg_str);
357358

358359
ErrorMessage msg2;
@@ -397,6 +398,7 @@ class TestErrorLogger : public TestFixture {
397398
"8 test.cpp"
398399
"17 Programming error"
399400
"17 Programming error"
401+
"0 "
400402
"0 ";
401403
ErrorMessage msg;
402404
ASSERT_THROW_INTERNAL_EQUALS(msg.deserialize(str), INTERNAL, "Internal Error: Deserialization of error message failed - invalid CWE ID - not an integer");
@@ -412,6 +414,7 @@ class TestErrorLogger : public TestFixture {
412414
"8 test.cpp"
413415
"17 Programming error"
414416
"17 Programming error"
417+
"0 "
415418
"0 ";
416419
ErrorMessage msg;
417420
ASSERT_THROW_INTERNAL_EQUALS(msg.deserialize(str), INTERNAL, "Internal Error: Deserialization of error message failed - invalid hash - not an integer");
@@ -427,6 +430,7 @@ class TestErrorLogger : public TestFixture {
427430
"8 test.cpp"
428431
"17 Programming error"
429432
"17 Programming error"
433+
"0 "
430434
"0 ";
431435
ErrorMessage msg;
432436
ASSERT_THROW_INTERNAL_EQUALS(msg.deserialize(str), INTERNAL, "Internal Error: Deserialization of error message failed - invalid CWE ID - out of range (limits)");
@@ -448,6 +452,7 @@ class TestErrorLogger : public TestFixture {
448452
"1 0"
449453
"33 Illegal character in \"foo\\001bar\""
450454
"33 Illegal character in \"foo\\001bar\""
455+
"0 "
451456
"0 ", msg_str);
452457

453458
ErrorMessage msg2;
@@ -475,6 +480,7 @@ class TestErrorLogger : public TestFixture {
475480
"1 1"
476481
"17 Programming error"
477482
"17 Programming error"
483+
"0 "
478484
"1 "
479485
"27 654\t33\t[]:;,()\t:/,;\tabcd:/,", msg_str);
480486

@@ -487,12 +493,24 @@ class TestErrorLogger : public TestFixture {
487493
ASSERT_EQUALS("abcd:/,", msg2.callStack.front().getinfo());
488494
}
489495

490-
void SerializeAndDeserializeRemark() const {
491-
ErrorMessage msg({}, emptyString, Severity::warning, emptyString, "id", Certainty::normal);
496+
void SerializeAndDeserialize() const {
497+
ErrorMessage msg({}, emptyString, Severity::warning, "$symbol:var\nmessage $symbol", "id", Certainty::normal);
492498
msg.remark = "some remark";
499+
493500
ErrorMessage msg2;
494501
ASSERT_NO_THROW(msg2.deserialize(msg.serialize()));
495-
ASSERT_EQUALS("some remark", msg2.remark);
502+
503+
ASSERT_EQUALS(msg.callStack.size(), msg2.callStack.size());
504+
ASSERT_EQUALS(msg.file0, msg2.file0);
505+
ASSERT_EQUALS_ENUM(msg.severity, msg2.severity);
506+
ASSERT_EQUALS(msg.shortMessage(), msg2.shortMessage());
507+
ASSERT_EQUALS(msg.verboseMessage(), msg2.verboseMessage());
508+
ASSERT_EQUALS(msg.id, msg2.id);
509+
ASSERT_EQUALS_ENUM(msg.certainty, msg2.certainty);
510+
ASSERT_EQUALS(msg.cwe.id, msg2.cwe.id);
511+
ASSERT_EQUALS(msg.hash, msg2.hash);
512+
ASSERT_EQUALS(msg.remark, msg2.remark);
513+
ASSERT_EQUALS(msg.symbolNames(), msg2.symbolNames());
496514
}
497515

498516
void substituteTemplateFormatStatic() const

0 commit comments

Comments
 (0)