Skip to content
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
36 changes: 36 additions & 0 deletions integration_test/safety/contract_return.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,40 @@ int* get2()
static int x;
static int* y = &x;
return y;
}

struct Dummy {
int m;
};

const int* get3();
const int* get4(const Dummy& d);
const int* get5(const Owner<int>& d);
const int* get6(const Owner<Dummy>& d);
const int* get7(const Owner<Pair<int, int>>& d);

void test_contract_return()
{
__lifetime_contracts(&get3);
// expected-warning@-1 {{pset(Post((return value))) = ((global), (null))}}

__lifetime_contracts(&get4);
// expected-warning@-1 {{pset(Pre(d)) = (*d)}}
// expected-warning@-2 {{pset(Pre((*d).m)) = (*d)}}
// expected-warning@-3 {{pset(Post((return value))) = ((null), *d)}}

__lifetime_contracts(&get5);
// expected-warning@-1 {{pset(Pre(d)) = (*d)}}
// expected-warning@-2 {{pset(Pre(*d)) = (**d)}}
// expected-warning@-3 {{pset(Post((return value))) = ((null), **d)}}

__lifetime_contracts(&get6);
// expected-warning@-1 {{pset(Pre(d)) = (*d)}}
// expected-warning@-2 {{pset(Pre(*d)) = (**d)}}
// expected-warning@-3 {{pset(Post((return value))) = ((null), **d)}}

__lifetime_contracts(&get7);
// expected-warning@-1 {{pset(Pre(d)) = (*d)}}
// expected-warning@-2 {{pset(Pre(*d)) = (**d)}}
// expected-warning@-3 {{pset(Post((return value))) = ((null), **d)}}
}
17 changes: 15 additions & 2 deletions lib/lifetime/LifetimePsetBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ class PSetsBuilder final : public ConstStmtVisitor<PSetsBuilder, void>, public P
}

if (PS.containsParent(V)) {
// WORKAROUND: https://github.com/qqiangwu/cppsafe/issues/82
// BADCASE: https://github.com/qqiangwu/cppsafe/issues/82
//
// when move var with pset {*container}, only invalidate the var itself,
// containers and iterators are not invalidated.
Expand All @@ -851,7 +851,6 @@ class PSetsBuilder final : public ConstStmtVisitor<PSetsBuilder, void>, public P
// for (auto& x : cont) {
// p = std::move(x);
//}

// for (auto& x: cont) {
// use(x);
//}
Expand Down Expand Up @@ -879,6 +878,20 @@ class PSetsBuilder final : public ConstStmtVisitor<PSetsBuilder, void>, public P

auto DerefV = V;
DerefV.deref();
if (Var == DerefV) {
// WORKAROUND
//<code>
// inline void PinnableWideColumns::MoveValue(std::string&& value) {
// std::string* const buf = value_.GetSelf();
// assert(buf);
//
// *buf = std::move(value); // PMap[(*(*this).value_)] = (**(*this).value_)
//
// value_.PinSelf();
//}
//</code>
continue;
}
if (PS.containsParent(DerefV)) {
setPSet(PSet::singleton(Var), PSet::invalid(Reason), Reason.getRange());
}
Expand Down