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

            Bug ID: 104715
           Summary: [12 Regression] false dangling pointer with strstr
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

This code warns about a dangling pointer but there is none as strstr does not
return an offset based on the second argument only the first argument

#include <stddef.h>
#include <string.h>
#include <stdlib.h>

char *
trim_xml_text(char * intxt, char const * pznm)
{
    size_t nm_len = strlen(pznm);
    char * etext;

    {
        char z[64], *pz = z;

        if (nm_len + 4 >= sizeof(z))
            pz = malloc(nm_len + 4);

        pz[0] = '<';
        pz[1] = '/';
        memcpy(pz+2, pznm, nm_len);
        nm_len  += 2;
        pz[nm_len++] = '>';
        pz[nm_len]   = 0;

        *intxt = ' ';
        etext = strstr(intxt, pz);
        if (pz != z) free(pz);
    }

    if (etext == NULL)
        return etext;

    {
        char * result = etext + nm_len;

        *etext = 0;
        return result;
    }
}

Reply via email to