Skip to content

Commit

Permalink
add segfault example when using rc
Browse files Browse the repository at this point in the history
  • Loading branch information
cmazakas committed Sep 12, 2024
1 parent 898ae63 commit ed32316
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions libsafecxx/test/broken/rc_segfault.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#feature on safety

template<class T+>
class manually_drop
{
T t_;

public:
explicit manually_drop(T t) noexcept safe;
~manually_drop() = trivial;

void destroy(self^) noexcept;
T const^ get(self const^) noexcept safe;
};

template<class T+>
class [[unsafe::send(false)]] rc
{
struct rc_inner;
rc_inner* unsafe p_;

struct rc_inner
{
manually_drop<T> data_;
std::size_t strong_;
std::size_t weak_;

explicit
rc_inner(T data) noexcept safe;
};

public:

explicit rc(T t) safe;
rc(rc const^ rhs) safe;
[[unsafe::drop_only(T)]] ~rc() safe;

T const^ operator*(self const^) noexcept safe {
return *self->p_->data_.get();
}
};

template<class T, class U>
void assert_eq(const T^ t, const U^ u) safe
{
if (*t != *u) throw "unequal values";
}

void drop_only() safe
{
{
rc<int^> p;

{
int x = 1234;
p = rc<int^>(^x);
assert_eq(**p, 1234);
}
}
}

int main() safe
{
drop_only();
}

0 comments on commit ed32316

Please sign in to comment.