Skip to content

Commit 8058482

Browse files
authored
Fix lazy init of multidimensional pointers to const qualified values (#620)
* Fix lazy initialization of const values
1 parent 28b9102 commit 8058482

File tree

7 files changed

+45
-3
lines changed

7 files changed

+45
-3
lines changed

server/src/Tests.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,9 @@ size_t KTestObjectParser::getOffsetInStruct(Tests::TypeAndVarName &objTypeAndNam
725725
size_t sizeInBits = typesHandler.typeSize(objTypeAndName.type);
726726
size_t offset = offsetInBits / sizeInBits;
727727
PrinterUtils::appendIndicesToVarName(objTypeAndName.varName, sizes, offset);
728+
if (objTypeAndName.type.isConstQualifiedValue()) {
729+
PrinterUtils::appendConstCast(objTypeAndName.varName);
730+
}
728731
offsetInBits %= sizeInBits;
729732
return offsetInBits;
730733
}

server/src/printers/HeaderPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace printer {
1414
ss << NL;
1515
ss << PrinterUtils::redirectStdin << NL;
1616
ss << PrinterUtils::writeToFile << NL;
17-
ss << PrinterUtils::fromBytes;
17+
ss << PrinterUtils::fromBytes << NL;
18+
ss << PrinterUtils::constCast;
1819
headerCode += ss.str();
1920
FileSystemUtils::writeToFile(testHeaderFilePath, headerCode);
2021
}

server/src/utils/PrinterUtils.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ namespace PrinterUtils {
6868
varName += indices;
6969
}
7070

71+
void appendConstCast(std::string &varName) {
72+
if (varName.empty()) {
73+
return;
74+
}
75+
varName = StringUtils::stringFormat("constCast(%s)", varName);
76+
}
77+
7178
std::string initializePointer(const std::string &type,
7279
const std::string &value,
7380
size_t additionalPointersCount,

server/src/utils/PrinterUtils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
#include <string>
1313

1414
namespace PrinterUtils {
15+
const std::string constCast = "template<typename T>\n"
16+
"T& constCast(const T &val) {\n"
17+
" return const_cast<T&>(val);\n"
18+
"}\n";
19+
1520
const std::string fromBytes = "template<typename T, size_t N>\n"
1621
"T from_bytes(const char (&bytes)[N]) {\n"
1722
" T result;\n"
@@ -81,6 +86,8 @@ namespace PrinterUtils {
8186

8287
void appendIndicesToVarName(std::string &varName, const std::vector<size_t> &sizes, size_t offset);
8388

89+
void appendConstCast(std::string &varName);
90+
8491
std::string getKleePrefix(bool forKlee);
8592

8693
std::string wrapUserValue(const testsgen::ValidationType &type, const std::string &value);

server/test/framework/Server_Tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,7 +2120,7 @@ namespace {
21202120
auto testGen = FileTestGen(*request, writer.get(), TESTMODE);
21212121
Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get());
21222122
ASSERT_TRUE(status.ok()) << status.error_message();
2123-
EXPECT_GE(testUtils::getNumberOfTests(testGen.tests), 2);
2123+
EXPECT_GE(testUtils::getNumberOfTests(testGen.tests), 4);
21242124

21252125
fs::path testsDirPath = getTestFilePath("tests");
21262126

@@ -2146,7 +2146,7 @@ namespace {
21462146
auto resultsMap = coverageGenerator.getTestResultMap();
21472147
auto tests = coverageGenerator.getTestsToLaunch();
21482148

2149-
StatusCountMap expectedStatusCountMap{ { testsgen::TEST_PASSED, 2 } };
2149+
StatusCountMap expectedStatusCountMap{ { testsgen::TEST_PASSED, 4 } };
21502150
testUtils::checkStatuses(resultsMap, tests);
21512151
}
21522152

server/test/suites/server/multi_dim_pointers.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,25 @@ int func_with_multi_dim_pointer(struct MainStruct **str) {
2424
}
2525
return sz;
2626
}
27+
28+
int func_with_multi_dim_pointer_to_const(const struct MainStruct **str) {
29+
if (!str) {
30+
return 0;
31+
}
32+
str++;
33+
struct MainStruct *ptr = *str;
34+
int sz = 0;
35+
if (ptr) {
36+
struct ElementStruct *e = ptr->list.head;
37+
struct ElementStruct *n;
38+
for (int i = 0; i < 5; i++) {
39+
if (e) {
40+
n = e->next;
41+
sz++;
42+
} else {
43+
break;
44+
}
45+
}
46+
}
47+
return sz;
48+
}

server/test/suites/server/multi_dim_pointers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ struct MainStruct {
1919

2020
int func_with_multi_dim_pointer(struct MainStruct **str);
2121

22+
int func_with_multi_dim_pointer_to_const(const struct MainStruct **str);
23+
2224
#endif // UNITTESTBOT_MULTI_DIM_POINTERS_H

0 commit comments

Comments
 (0)