1 // Copyright 2016 Google Inc. All rights reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
17 #include "zcpointer.h"
26 #define EXPECT(expr) do { if (!(expr)) { throw std::logic_error(#expr); } } while(0)
28 #define EXPECT_UAF(expr) do { \
31 throw std::logic_error("Expected use-after-free: " #expr); \
32 } catch (zc::UseAfterFreeError) {} \
36 zc
::owned
<C
> c(new C());
37 zc
::ref
<C
> owned
= c
.get();
38 zc
::ref
<C
> owned2
= owned
;
40 EXPECT_UAF(owned2
->DoThing());
45 zc
::owned
<T
> t(new T());
46 //T* unwrap = t.get();
48 zc
::ref
<T
> ref
= t
.get();
53 zc
::owned
<C
> c(new C());
54 zc
::ref
<C
> owned
= c
.get();
56 zc
::owned
<C
> c2(std
::move(c
));
60 EXPECT_UAF(owned
->DoThing());
63 void PtrHelper(zc
::ref
<C
>* out
) {
64 zc
::owned
<C
> c(new C());
71 EXPECT_UAF(ref
->DoThing());
74 #define TEST_FUNC(fn) { #fn , Test##fn }
87 for (const auto& test
: kTests
) {
88 std
::cout
<< "=== BEGIN " << test
.name
<< " ===" << std
::endl
;
91 std
::cout
<< "=== PASS " << test
.name
<< " ===" << std
::endl
;
92 } catch (const std
::logic_error
& e
) {
94 std
::cout
<< "=== FAIL " << test
.name
95 << ": Assertion failure: " << e
.what() << " ===" << std
::endl
;
99 return passed ?
0 : 1;