use super::{assert_reported, assert_silent_about, codes_of};
use crate::DiagnosticCode;
#[test]
fn a_pointer_read_after_delete_is_reported() {
let codes = codes_of(
"struct T { int x; };\nvoid f() {\n T* p = new T();\n delete p;\n p->x;\n}\n",
);
assert_reported(
&codes,
DiagnosticCode::UseAfterFree,
"`p`'s object was deleted",
);
}
#[test]
fn a_pointer_read_after_a_helper_frees_it_is_reported() {
let codes = codes_of(
"struct T { int x; };\n\
void release(T* p) { delete p; }\n\
void f() {\n T* p = new T();\n release(p);\n p->x;\n}\n",
);
assert_reported(
&codes,
DiagnosticCode::UseAfterFree,
"`release` frees its parameter, and the call site knows it",
);
}
#[test]
fn free_and_reset_are_frees() {
let freed = codes_of("void f() {\n int* p = (int*)malloc(4);\n free(p);\n *p;\n}\n");
assert_reported(
&freed,
DiagnosticCode::UseAfterFree,
"`free` releases the pointee",
);
let reset = codes_of(
"void f() {\n std::unique_ptr<int> u(new int(1));\n u.reset();\n *u;\n}\n",
);
assert_reported(
&reset,
DiagnosticCode::UseAfterFree,
"`reset` releases the pointee",
);
}
#[test]
fn the_safe_free_shapes_are_silent() {
let before = codes_of(
"struct T { int x; };\nvoid f() {\n T* p = new T();\n p->x;\n delete p;\n}\n",
);
assert_silent_about(
&before,
DiagnosticCode::UseAfterFree,
"the read is before the free",
);
let conditional = codes_of(
"struct T { int x; };\n\
void f(int c) {\n T* p = new T();\n if (c) { delete p; }\n p->x;\n}\n",
);
assert_silent_about(
&conditional,
DiagnosticCode::UseAfterFree,
"the free may not run, so the later read is only sometimes wrong",
);
let reassigned = codes_of(
"struct T { int x; };\n\
void f() {\n T* p = new T();\n delete p;\n p = new T();\n p->x;\n}\n",
);
assert_silent_about(
&reassigned,
DiagnosticCode::UseAfterFree,
"`p` was given a fresh value, so the freed object is not the one read",
);
let untouched = codes_of(
"struct T { int x; };\n\
void touch(T* p) { }\n\
void f() {\n T* p = new T();\n touch(p);\n p->x;\n}\n",
);
assert_silent_about(
&untouched,
DiagnosticCode::UseAfterFree,
"the callee's fact does not say it frees",
);
}
#[test]
fn a_dot_is_not_a_read_through_the_pointer() {
let codes = codes_of(
"struct Holder {\n void reset();\n int value;\n};\n\
void f() {\n Holder h;\n h.reset();\n h.value;\n}\n",
);
assert_silent_about(
&codes,
DiagnosticCode::UseAfterFree,
"`h.value` reads the pointer object, not its pointee",
);
}