[Bug c++/79707] New: missing -Wunused-result on an unused new expression

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Feb 24 16:47:00 GMT 2017


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

            Bug ID: 79707
           Summary: missing -Wunused-result on an unused new expression
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The result of the new expression in the return statement in the following
example is unused, leaking the memory it allocates.  GCC doesn't point it out
by issuing a warning on the code (such as -Wunused-result).

There may be cases where the warning may be considered a false positive: for
example, a class whose constructor stores the this pointer in some registry so
the unused return value of the new expression need not be stored to prevent the
memory and the object from leaking.  Those are likely rare and may be an
acceptable trade-off.  For POD types the warning should in practice always be a
true positive.  I.e., in 'void f () { new int; }' GCC should not only warn but
can also eliminate the new expression.

$ cat t.C && gcc -O2 -S -Wall -Wextra -Wpedantic -Wunused-result
-fdump-tree-optimized=/dev/stdout t.C
struct S {
  S (int, S * = 0);
};

S* f (S *p)
{
  // typo: 'return new S (1, p)' intended
  return new S (1), p;
}

;; Function S* f(S*) (_Z1fP1S, funcdef_no=0, decl_uid=2290, cgraph_uid=0,
symbol_order=0)

S* f(S*) (struct S * p)
{
  void * D.2324;
  void * _3;

  <bb 2> [100.00%]:
  _3 = operator new (1);
  S::S (_3, 1, 0B);

  <bb 3> [100.00%]:
  return p_5(D);

<L1> [0.00%]:
  operator delete (_3, 1);
  _7 = __builtin_eh_pointer (1);
  __builtin_unwind_resume (_7);

}


More information about the Gcc-bugs mailing list