Skip to content

Commit

Permalink
Merge pull request microsoft#46 from trebconnell/fixmaybenull
Browse files Browse the repository at this point in the history
Fix issue microsoft#45: comparing two maybe_null_dbg's can cause fail_fast
  • Loading branch information
Neil MacIntosh committed Sep 24, 2015
2 parents 0a88570 + 996aa06 commit 8ae77b1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/gsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ class maybe_null_dbg

bool operator==(const T& rhs) const { tested_ = true; return ptr_ == rhs; }
bool operator!=(const T& rhs) const { return !(*this == rhs); }
bool operator==(const maybe_null_dbg& rhs) const { tested_ = true; rhs.tested_ = true; return ptr_ == rhs.ptr_; }
bool operator!=(const maybe_null_dbg& rhs) const { return !(*this == rhs); }

T get() const {
fail_fast_assert(tested_);
Expand Down
52 changes: 52 additions & 0 deletions tests/maybenull_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,58 @@ SUITE(MaybeNullTests)
CHECK(q.present());
CHECK(q->foo());
}

TEST(TestMaybeNullCompare)
{
int i1 = 1;
int i2 = 2;

maybe_null_dbg<int*> p1 = &i1;
maybe_null_dbg<int*> p1_2 = &i1;
maybe_null_dbg<int*> p2 = &i2;

CHECK_THROW(p1.get(), fail_fast);
CHECK_THROW(p1_2.get(), fail_fast);
CHECK_THROW(p2.get(), fail_fast);

CHECK(p1 != p2);
CHECK(!(p1 == p2));
CHECK(p1 == p1);
CHECK(p1 == p1_2);

// Make sure we no longer throw here
CHECK(p1.get() != nullptr);
CHECK(p1_2.get() != nullptr);
CHECK(p2.get() != nullptr);
}

TEST(TestMaybeNullCopy)
{
int i1 = 1;
int i2 = 2;

maybe_null_dbg<int*> p1 = &i1;
maybe_null_dbg<int*> p1_2 = &i1;
maybe_null_dbg<int*> p2 = &i2;

CHECK(p1 != p2);
CHECK(p1 == p1_2);

// Make sure we no longer throw here
CHECK(p1.get() != nullptr);
CHECK(p2.get() != nullptr);

p1 = p2;

// Make sure we now throw
CHECK_THROW(p1.get(), fail_fast);

CHECK(p1 == p2);
CHECK(p1 != p1_2);

// Make sure we no longer throw here
CHECK(p1.get() != nullptr);
}
}

int main(int, const char *[])
Expand Down

0 comments on commit 8ae77b1

Please sign in to comment.