From a4e6efdce73e8b305a0521a2798ab12d710163a0 Mon Sep 17 00:00:00 2001 From: IDKWNTCMF Date: Thu, 10 Aug 2023 12:33:26 +0300 Subject: [PATCH 1/2] Fix lazy initialization of const values --- server/src/Tests.cpp | 3 +++ server/src/printers/HeaderPrinter.cpp | 3 ++- server/src/utils/PrinterUtils.cpp | 7 +++++++ server/src/utils/PrinterUtils.h | 7 +++++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/server/src/Tests.cpp b/server/src/Tests.cpp index 8103bb92b..bbe91c7a3 100644 --- a/server/src/Tests.cpp +++ b/server/src/Tests.cpp @@ -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; } diff --git a/server/src/printers/HeaderPrinter.cpp b/server/src/printers/HeaderPrinter.cpp index b2bc17046..f7b00aa81 100644 --- a/server/src/printers/HeaderPrinter.cpp +++ b/server/src/printers/HeaderPrinter.cpp @@ -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); } diff --git a/server/src/utils/PrinterUtils.cpp b/server/src/utils/PrinterUtils.cpp index 4cdc41b63..a6c5fa30e 100644 --- a/server/src/utils/PrinterUtils.cpp +++ b/server/src/utils/PrinterUtils.cpp @@ -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) { diff --git a/server/src/utils/PrinterUtils.h b/server/src/utils/PrinterUtils.h index 94b4d70b5..b47a01d38 100644 --- a/server/src/utils/PrinterUtils.h +++ b/server/src/utils/PrinterUtils.h @@ -12,6 +12,11 @@ #include namespace PrinterUtils { + const std::string constCast = "template\n" + "T& constCast(const T &val) {\n" + " return const_cast(val);\n" + "}\n"; + const std::string fromBytes = "template\n" "T from_bytes(const char (&bytes)[N]) {\n" " T result;\n" @@ -79,6 +84,8 @@ namespace PrinterUtils { void appendIndicesToVarName(std::string &varName, const std::vector &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); From e583356406506e3fccfc1edc5f158c274173303f Mon Sep 17 00:00:00 2001 From: IDKWNTCMF Date: Thu, 10 Aug 2023 12:39:05 +0300 Subject: [PATCH 2/2] Add tests for this case --- server/test/framework/Server_Tests.cpp | 4 ++-- .../test/suites/server/multi_dim_pointers.c | 22 +++++++++++++++++++ .../test/suites/server/multi_dim_pointers.h | 2 ++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/server/test/framework/Server_Tests.cpp b/server/test/framework/Server_Tests.cpp index 53ebadd92..b546878ab 100644 --- a/server/test/framework/Server_Tests.cpp +++ b/server/test/framework/Server_Tests.cpp @@ -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"); @@ -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); } diff --git a/server/test/suites/server/multi_dim_pointers.c b/server/test/suites/server/multi_dim_pointers.c index 15146509f..540b3d46e 100644 --- a/server/test/suites/server/multi_dim_pointers.c +++ b/server/test/suites/server/multi_dim_pointers.c @@ -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; +} diff --git a/server/test/suites/server/multi_dim_pointers.h b/server/test/suites/server/multi_dim_pointers.h index 7dce8b47d..7e0b0c567 100644 --- a/server/test/suites/server/multi_dim_pointers.h +++ b/server/test/suites/server/multi_dim_pointers.h @@ -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