Files
clang-p2996/clang/test/Analysis/stack-addr-ps.cpp
Aaron Ballman 0f1c1be196 [clang] Remove rdar links; NFC
We have a new policy in place making links to private resources
something we try to avoid in source and test files. Normally, we'd
organically switch to the new policy rather than make a sweeping change
across a project. However, Clang is in a somewhat special circumstance
currently: recently, I've had several new contributors run into rdar
links around test code which their patch was changing the behavior of.
This turns out to be a surprisingly bad experience, especially for
newer folks, for a handful of reasons: not understanding what the link
is and feeling intimidated by it, wondering whether their changes are
actually breaking something important to a downstream in some way,
having to hunt down strangers not involved with the patch to impose on
them for help, accidental pressure from asking for potentially private
IP to be made public, etc. Because folks run into these links entirely
by chance (through fixing bugs or working on new features), there's not
really a set of problematic links to focus on -- all of the links have
basically the same potential for causing these problems. As a result,
this is an omnibus patch to remove all such links.

This was not a mechanical change; it was done by manually searching for
rdar, radar, radr, and other variants to find all the various
problematic links. From there, I tried to retain or reword the
surrounding comments so that we would lose as little context as
possible. However, because most links were just a plain link with no
supporting context, the majority of the changes are simple removals.

Differential Review: https://reviews.llvm.org/D158071
2023-08-28 12:13:42 -04:00

164 lines
5.5 KiB
C++

// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s -Wno-undefined-bool-conversion
typedef __INTPTR_TYPE__ intptr_t;
const int& g() {
int s;
return s; // expected-warning{{Address of stack memory associated with local variable 's' returned}} expected-warning{{reference to stack memory associated with local variable 's' returned}}
}
const int& g2() {
int s1;
int &s2 = s1; // expected-note {{binding reference variable 's2' here}}
return s2; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{reference to stack memory associated with local variable 's1' returned}}
}
const int& g3() {
int s1;
int &s2 = s1; // expected-note {{binding reference variable 's2' here}}
int &s3 = s2; // expected-note {{binding reference variable 's3' here}}
return s3; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{reference to stack memory associated with local variable 's1' returned}}
}
void g4() {
static const int &x = 3; // no warning
}
int get_value();
const int &get_reference1() { return get_value(); } // expected-warning{{Address of stack memory associated with temporary object of type 'int' returned}} expected-warning {{returning reference to local temporary}}
const int &get_reference2() {
const int &x = get_value(); // expected-note {{binding reference variable 'x' here}}
return x; // expected-warning{{Address of stack memory associated with temporary object of type 'int' lifetime extended by local variable 'x' returned to caller}} expected-warning {{returning reference to local temporary}}
}
const int &get_reference3() {
const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}}
const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
return x2; // expected-warning{{Address of stack memory associated with temporary object of type 'int' lifetime extended by local variable 'x1' returned to caller}} expected-warning {{returning reference to local temporary}}
}
int global_var;
int *f1() {
int &y = global_var;
return &y;
}
int *f2() {
int x1;
int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
return &x2; // expected-warning{{Address of stack memory associated with local variable 'x1' returned}} expected-warning {{address of stack memory associated with local variable 'x1' returned}}
}
int *f3() {
int x1;
int *const &x2 = &x1; // expected-note {{binding reference variable 'x2' here}}
return x2; // expected-warning {{address of stack memory associated with local variable 'x1' returned}} expected-warning {{Address of stack memory associated with local variable 'x1' returned to caller}}
}
const int *f4() {
const int &x1 = get_value(); // expected-note {{binding reference variable 'x1' here}}
const int &x2 = x1; // expected-note {{binding reference variable 'x2' here}}
return &x2; // expected-warning{{Address of stack memory associated with temporary object of type 'int' lifetime extended by local variable 'x1' returned to caller}} expected-warning {{returning address of local temporary}}
}
struct S {
int x;
};
int *mf() {
S s1;
S &s2 = s1; // expected-note {{binding reference variable 's2' here}}
int &x = s2.x; // expected-note {{binding reference variable 'x' here}}
return &x; // expected-warning{{Address of stack memory associated with local variable 's1' returned}} expected-warning {{address of stack memory associated with local variable 's1' returned}}
}
void *lf() {
label:
void *const &x = &&label; // expected-note {{binding reference variable 'x' here}}
return x; // expected-warning {{returning address of label, which is local}}
}
template <typename T>
struct TS {
int *get();
int *m() {
int *&x = get();
return x;
}
};
int* f5() {
int& i = i; // expected-warning {{Assigned value is garbage or undefined}} expected-warning{{reference 'i' is not yet bound to a value when used within its own initialization}}
return &i;
}
void *radar13226577() {
void *p = &p;
return p; // expected-warning {{stack memory associated with local variable 'p' returned to caller}}
}
namespace rdar13296133 {
class ConvertsToBool {
public:
operator bool() const { return this; }
};
class ConvertsToIntptr {
public:
operator intptr_t() const { return reinterpret_cast<intptr_t>(this); }
};
class ConvertsToPointer {
public:
operator const void *() const { return this; }
};
intptr_t returnAsNonLoc() {
ConvertsToIntptr obj;
return obj; // expected-warning{{Address of stack memory associated with local variable 'obj' returned to caller}}
}
bool returnAsBool() {
ConvertsToBool obj;
return obj; // no-warning
}
intptr_t returnAsNonLocViaPointer() {
ConvertsToPointer obj;
return reinterpret_cast<intptr_t>(static_cast<const void *>(obj)); // expected-warning{{Address of stack memory associated with local variable 'obj' returned to caller}}
}
bool returnAsBoolViaPointer() {
ConvertsToPointer obj;
return obj; // no-warning
}
}
void write_stack_address_to(char **q) {
char local;
*q = &local;
// expected-warning@-1 {{Address of stack memory associated with local \
variable 'local' is still referred to by the stack variable 'p' upon \
returning to the caller}}
}
void test_stack() {
char *p;
write_stack_address_to(&p);
}
struct C {
~C() {} // non-trivial class
};
C make1() {
C c;
return c; // no-warning
}
void test_copy_elision() {
C c1 = make1();
}