Skip to content

Commit 144c3e9

Browse files
committed
Use klee_assume(s[N - 1] == '\0') call for the last element of string
1 parent 4ec9145 commit 144c3e9

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

server/src/printers/KleeConstraintsPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void KleeConstraintsPrinter::genConstraintsForMultiPointerOrArray(const Constrai
120120
std::vector<std::string> charSizes(indexes.begin(), indexes.end() - 1);
121121
const auto charElement = constrMultiIndex(state.curElement, charSizes);
122122
ss << TAB_N() << "if (" << indexes.back() << PrinterUtils::EQ_OPERATOR << sizes.back() - 1 << ")" << LB();
123-
ss << TAB_N() << charElement << "[" << sizes.back() - 1 << "]" << PrinterUtils::ASSIGN_OPERATOR << "'\\0'" << SCNL;
123+
ss << TAB_N() << PrinterUtils::KLEE_ASSUME << "(" << charElement << "[" << sizes.back() - 1 << "]" << PrinterUtils::EQ_OPERATOR << "'\\0'" << ")" << SCNL;
124124
ss << TAB_N() << "break" << SCNL;
125125
ss << RB();
126126
}

server/test/framework/Regression_Tests.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,34 @@ namespace {
238238

239239
ASSERT_TRUE(status.ok()) << status.error_message();
240240
}
241+
242+
TEST_F(Regression_Test, Hash_Of_String) {
243+
fs::path source = getTestFilePath("GH215.c");
244+
auto [testGen, status] = createTestForFunction(source, 2);
245+
246+
ASSERT_TRUE(status.ok()) << status.error_message();
247+
248+
auto predicate = [](const tests::Tests::MethodTestCase &testCase) {
249+
auto s = testCase.paramValues[0].view->getEntryValue(nullptr);
250+
s = s.substr(1, s.length() - 2);
251+
auto actual = testCase.returnValue.view->getEntryValue(nullptr);
252+
auto expected = std::to_string(std::accumulate(s.begin(), s.end(), 0));
253+
return actual == expected;
254+
};
255+
256+
checkTestCasePredicates(
257+
testGen.tests.at(source).methods.begin().value().testCases,
258+
std::vector<TestCasePredicate>(
259+
{ [&predicate](const tests::Tests::MethodTestCase &testCase) {
260+
// empty string
261+
return testCase.paramValues[0].view->getEntryValue(nullptr).length() == 2 &&
262+
predicate(testCase);
263+
},
264+
[&predicate](const tests::Tests::MethodTestCase &testCase) {
265+
// non-empty string
266+
return testCase.paramValues[0].view->getEntryValue(nullptr).length() > 2 &&
267+
predicate(testCase);
268+
} }),
269+
"hash");
270+
}
241271
}

server/test/suites/regression/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ add_library(PR124 PR124.c)
3838

3939
add_library(PR153 PR153.c)
4040

41-
set_target_properties(regression PROPERTIES LINK_WHAT_YOU_USE TRUE)
41+
add_library(GH215 GH215.c)
42+
43+
set_target_properties(regression PROPERTIES LINK_WHAT_YOU_USE TRUE)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int hash(const char *s) {
2+
int h = 0, i = 0;
3+
while (s[i] != '\0') {
4+
h = (h + s[i]);
5+
i++;
6+
}
7+
return h;
8+
}

0 commit comments

Comments
 (0)