[llvm-bugs] [Bug 51002] New: Warn when saving a pointer to an object with temporary lifetime

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Jul 6 21:58:58 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51002

            Bug ID: 51002
           Summary: Warn when saving a pointer to an object with temporary
                    lifetime
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: josephcsible at gmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

Consider this C code:

typedef struct {
    int x[1];
} foo;

foo f(void);

int g(void) {
    int *p = f().x;
    return *p;
}

The g() function is always UB, since the return value of f() has temporary
lifetime, so doing "return *p;" is dereferencing a pointer to an object whose
lifetime has ended. (This is the case both before and after C11's change to
temporary lifetime.) Since it's obvious at compile time that p can never be
used safely, we should have a warning for it, similar to how we have
-Wreturn-stack-address to catch mistakes like this function:

int *h(void) {
    int x;
    return &x;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210707/6eeedd5a/attachment-0001.html>


More information about the llvm-bugs mailing list