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

            Bug ID: 65041
           Summary: Improve -Wclobbered
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mpolacek at gcc dot gnu.org

Compiling the following with -Wclobbered -O results in
warning: variable ‘a2’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered]

-Wclobbered should warn when
       The values of automatic variables  are  unspecified  after  a  call  to
       longjmp() if they meet all the following criteria:
       *  they are local to the function that made the corresponding setjmp(3)
          call;
       *  their  values  are  changed  between  the  calls  to  setjmp(3)  and
          longjmp(); and
       *  they are not declared as volatile.

Supposedly we should warn about 'fd' here, not 'a2'.

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <fcntl.h>
#include <unistd.h>

extern void somefunc(int x);
extern void someotherfunc(int x);

void
testsetjmp(int a1)
{
    jmp_buf jbuf;
    int fd;
    int a2;

    if (a1 > 0)
        a2 = 42;
    else
        a2 = 43;

    fd = open("mytmpfile", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
    if (fd < 0)
    {
        perror("mytmpfile");
        exit(1);
    }

    if (setjmp(jbuf) == 0)
    {
        somefunc(a2);
        close(fd);
        fd = -1;
        someotherfunc(a2);
    }
    else
    {
        /* we get here on longjmp out of somefunc or someotherfunc */
        if (fd >= 0)
            close(fd);
    }
}

Reply via email to