Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lazy init of multidimensional pointers to const qualified values #620

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions server/src/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,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 @@ -64,6 +64,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 @@ -79,6 +84,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 @@ -2149,7 +2149,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 @@ -2175,7 +2175,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 @@ -21,3 +21,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 @@ -18,4 +18,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