Skip to content

Commit

Permalink
Fix lazy init of multidimensional pointers to const qualified values (#…
Browse files Browse the repository at this point in the history
…620)

* Fix lazy initialization of const values
  • Loading branch information
IDKWNTCMF authored Sep 4, 2023
1 parent 28b9102 commit 8058482
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 3 deletions.
3 changes: 3 additions & 0 deletions server/src/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,9 @@ size_t KTestObjectParser::getOffsetInStruct(Tests::TypeAndVarName &objTypeAndNam
size_t sizeInBits = typesHandler.typeSize(objTypeAndName.type);
size_t offset = offsetInBits / sizeInBits;
PrinterUtils::appendIndicesToVarName(objTypeAndName.varName, sizes, offset);
if (objTypeAndName.type.isConstQualifiedValue()) {
PrinterUtils::appendConstCast(objTypeAndName.varName);
}
offsetInBits %= sizeInBits;
return offsetInBits;
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/printers/HeaderPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace printer {
ss << NL;
ss << PrinterUtils::redirectStdin << NL;
ss << PrinterUtils::writeToFile << NL;
ss << PrinterUtils::fromBytes;
ss << PrinterUtils::fromBytes << NL;
ss << PrinterUtils::constCast;
headerCode += ss.str();
FileSystemUtils::writeToFile(testHeaderFilePath, headerCode);
}
Expand Down
7 changes: 7 additions & 0 deletions server/src/utils/PrinterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ namespace PrinterUtils {
varName += indices;
}

void appendConstCast(std::string &varName) {
if (varName.empty()) {
return;
}
varName = StringUtils::stringFormat("constCast(%s)", varName);
}

std::string initializePointer(const std::string &type,
const std::string &value,
size_t additionalPointersCount,
Expand Down
7 changes: 7 additions & 0 deletions server/src/utils/PrinterUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#include <string>

namespace PrinterUtils {
const std::string constCast = "template<typename T>\n"
"T& constCast(const T &val) {\n"
" return const_cast<T&>(val);\n"
"}\n";

const std::string fromBytes = "template<typename T, size_t N>\n"
"T from_bytes(const char (&bytes)[N]) {\n"
" T result;\n"
Expand Down Expand Up @@ -81,6 +86,8 @@ namespace PrinterUtils {

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

void appendConstCast(std::string &varName);

std::string getKleePrefix(bool forKlee);

std::string wrapUserValue(const testsgen::ValidationType &type, const std::string &value);
Expand Down
4 changes: 2 additions & 2 deletions server/test/framework/Server_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2120,7 +2120,7 @@ namespace {
auto testGen = FileTestGen(*request, writer.get(), TESTMODE);
Status status = Server::TestsGenServiceImpl::ProcessBaseTestRequest(testGen, writer.get());
ASSERT_TRUE(status.ok()) << status.error_message();
EXPECT_GE(testUtils::getNumberOfTests(testGen.tests), 2);
EXPECT_GE(testUtils::getNumberOfTests(testGen.tests), 4);

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

Expand All @@ -2146,7 +2146,7 @@ namespace {
auto resultsMap = coverageGenerator.getTestResultMap();
auto tests = coverageGenerator.getTestsToLaunch();

StatusCountMap expectedStatusCountMap{ { testsgen::TEST_PASSED, 2 } };
StatusCountMap expectedStatusCountMap{ { testsgen::TEST_PASSED, 4 } };
testUtils::checkStatuses(resultsMap, tests);
}

Expand Down
22 changes: 22 additions & 0 deletions server/test/suites/server/multi_dim_pointers.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,25 @@ int func_with_multi_dim_pointer(struct MainStruct **str) {
}
return sz;
}

int func_with_multi_dim_pointer_to_const(const struct MainStruct **str) {
if (!str) {
return 0;
}
str++;
struct MainStruct *ptr = *str;
int sz = 0;
if (ptr) {
struct ElementStruct *e = ptr->list.head;
struct ElementStruct *n;
for (int i = 0; i < 5; i++) {
if (e) {
n = e->next;
sz++;
} else {
break;
}
}
}
return sz;
}
2 changes: 2 additions & 0 deletions server/test/suites/server/multi_dim_pointers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ struct MainStruct {

int func_with_multi_dim_pointer(struct MainStruct **str);

int func_with_multi_dim_pointer_to_const(const struct MainStruct **str);

#endif // UNITTESTBOT_MULTI_DIM_POINTERS_H

0 comments on commit 8058482

Please sign in to comment.