[Bug analyzer/104821] RFE: consolidate analyzer leak diagnostics by considering indirect vs direct leaks

dmalcolm at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Mar 7 15:02:05 GMT 2022


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

--- Comment #1 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
Example: https://godbolt.org/z/afvEd99qn


#include <stdlib.h>

struct s
{
  void *m_a;
  void *m_b;
  void *m_c;
};

struct s *
make_s (size_t sz_a, size_t sz_b, size_t sz_c)
{
  struct s *result = calloc (1, sizeof (struct s));
  if (!result)
    return NULL;
  result->m_a = malloc (sz_a);
  if (!result->m_a)
    goto err;
  result->m_b = malloc (sz_b);
  if (!result->m_b)
    goto err;
  result->m_b = malloc (sz_c);
  if (!result->m_c)
    goto err;
  return result;

 err:
  free (result->m_c);
  free (result->m_b);
  free (result->m_a);
  free (result);
  return NULL;
}

void test ()
{
  struct s *ptr = make_s (5, 10, 15);
  /* now leak ptr.  */
}



This emits 4 -Wanalyzer-malloc-leak warnings with almost identical paths,
mostly just varying in the "allocated here" messages.


More information about the Gcc-bugs mailing list