Summary: Similar to use-after-`delete`. Reviewed By: jeremydubreil Differential Revision: D7032893 fbshipit-source-id: a974dc3master
parent
b41a059609
commit
4485e97bee
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018 - present Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct S {
|
||||||
|
int f;
|
||||||
|
|
||||||
|
~S() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// destructor called at end of function, no issues
|
||||||
|
void normal_scope_destructor_ok() { S s{1}; }
|
||||||
|
|
||||||
|
void nested_scope_destructor_ok() {
|
||||||
|
{ S s{1}; }
|
||||||
|
}
|
||||||
|
|
||||||
|
int reinit_after_explicit_destructor_ok() {
|
||||||
|
S s{1};
|
||||||
|
s.~S();
|
||||||
|
s = S{2};
|
||||||
|
return s.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
void placement_new_explicit_destructor_ok() {
|
||||||
|
char buf[sizeof(S)];
|
||||||
|
{
|
||||||
|
S* s = new (buf) S;
|
||||||
|
s->~S();
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// this use of [buf] shouldn't be flagged
|
||||||
|
S* s = new (buf) S;
|
||||||
|
s->~S();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void double_destructor_bad() {
|
||||||
|
S s{1};
|
||||||
|
s.~S();
|
||||||
|
// destructor will be called again after S goes out of scope, which is
|
||||||
|
// undefined behavior
|
||||||
|
}
|
||||||
|
|
||||||
|
// frontend does the right thing here, but we need to propagate permissions from
|
||||||
|
// tmp to s
|
||||||
|
int FN_use_after_scope1_bad() {
|
||||||
|
S s;
|
||||||
|
{
|
||||||
|
S tmp{1};
|
||||||
|
s = tmp;
|
||||||
|
} // destructor runs here
|
||||||
|
return s.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int FN_use_after_scope2_bad() {
|
||||||
|
S s;
|
||||||
|
{
|
||||||
|
s = S{1};
|
||||||
|
} // destructor runs here, but our frontend currently doesn't insert it
|
||||||
|
return s.f;
|
||||||
|
}
|
Loading…
Reference in new issue