Skip to content

Commit 056cd12

Browse files
authored
[clang][bytecode] Don't check returned pointers for liveness (#120107)
We're supposed to let them through and then later diagnose reading from them, but returning dead pointers is fine.
1 parent c6ff809 commit 056cd12

File tree

2 files changed

+6
-22
lines changed

2 files changed

+6
-22
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,18 +318,6 @@ template <PrimType Name, class T = typename PrimConv<Name>::T>
318318
bool Ret(InterpState &S, CodePtr &PC) {
319319
const T &Ret = S.Stk.pop<T>();
320320

321-
// Make sure returned pointers are live. We might be trying to return a
322-
// pointer or reference to a local variable.
323-
// Just return false, since a diagnostic has already been emitted in Sema.
324-
if constexpr (std::is_same_v<T, Pointer>) {
325-
// FIXME: We could be calling isLive() here, but the emitted diagnostics
326-
// seem a little weird, at least if the returned expression is of
327-
// pointer type.
328-
// Null pointers are considered live here.
329-
if (!Ret.isZero() && !Ret.isLive())
330-
return false;
331-
}
332-
333321
assert(S.Current);
334322
assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
335323
if (!S.checkingPotentialConstantExpression() || S.Current->Caller)

clang/test/AST/ByteCode/functions.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,21 +303,17 @@ namespace ReturnLocalPtr {
303303
return &a; // both-warning {{address of stack memory}}
304304
}
305305

306-
/// GCC rejects the expression below, just like the new interpreter. The current interpreter
307-
/// however accepts it and only warns about the function above returning an address to stack
308-
/// memory. If we change the condition to 'p() != nullptr', it even succeeds.
309-
static_assert(p() == nullptr, ""); // ref-error {{static assertion failed}} \
310-
// expected-error {{not an integral constant expression}}
311-
312-
/// FIXME: The current interpreter emits diagnostics in the reference case below, but the
313-
/// new one does not.
306+
/// FIXME: Both interpreters should diagnose this. We're returning a pointer to a local
307+
/// variable.
308+
static_assert(p() == nullptr, ""); // both-error {{static assertion failed}}
309+
314310
constexpr const int &p2() {
315-
int a = 12; // ref-note {{declared here}}
311+
int a = 12; // both-note {{declared here}}
316312
return a; // both-warning {{reference to stack memory associated with local variable}}
317313
}
318314

319315
static_assert(p2() == 12, ""); // both-error {{not an integral constant expression}} \
320-
// ref-note {{read of variable whose lifetime has ended}}
316+
// both-note {{read of variable whose lifetime has ended}}
321317
}
322318

323319
namespace VoidReturn {

0 commit comments

Comments
 (0)