public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/101358] New: Warn when saving a pointer to an object with temporary lifetime
@ 2021-07-07  5:04 josephcsible at gmail dot com
  2021-07-07 21:59 ` [Bug c/101358] " msebor at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: josephcsible at gmail dot com @ 2021-07-07  5:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101358

            Bug ID: 101358
           Summary: Warn when saving a pointer to an object with temporary
                    lifetime
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: josephcsible at gmail dot com
  Target Milestone: ---

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-local-addr to catch mistakes like this function:

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c/101358] Warn when saving a pointer to an object with temporary lifetime
  2021-07-07  5:04 [Bug c/101358] New: Warn when saving a pointer to an object with temporary lifetime josephcsible at gmail dot com
@ 2021-07-07 21:59 ` msebor at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-07-07 21:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101358

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
             Blocks|                            |87403
                 CC|                            |msebor at gcc dot gnu.org
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-07-07
           Severity|normal                      |enhancement

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
Confirmed as an enhancement request for the front end.  The Gimplifier
introduces a local variable for the return value so the problem is no longer
detectable:

$ cat pr101358.c && /build/gcc-master/gcc/xgcc -B /build/gcc-master/gcc -S
-Wall -fdump-tree-original=/dev/stdout -fdump-tree-gimple=/dev/stdout
pr101358.c
typedef struct {
    int x[1];
} foo;

foo f(void);

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

;; Function g (null)
;; enabled by -tree-original


{
  int * p = (int *) &f ().x;

    int * p = (int *) &f ().x;
  return *p;
}

int g ()
{
  struct 
{
  int x[1];
} D.1952;
  int D.1953;
  int * p;

  D.1952 = f ();
  p = &D.1952.x;
  D.1953 = *p;
  return D.1953;
}


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87403
[Bug 87403] [Meta-bug] Issues that suggest a new warning

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-07-07 21:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-07  5:04 [Bug c/101358] New: Warn when saving a pointer to an object with temporary lifetime josephcsible at gmail dot com
2021-07-07 21:59 ` [Bug c/101358] " msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).