I have a function like this:
#include <setjmp.h>
jmp_buf buf;
void func2(int g);
extern int some_global;
void func(int x)
{
if (setjmp(buf))
return;
if (some_global)
x += 5;
func2(x);
}
GCC (gcc (Debian 4.4.5-8) 4.4.5) gives a warning:
test.c: In function ‘func’: test.c:5: warning: argument ‘x’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered]
Why???? I mean, obviously I don't care if x
is clobbered or not, because it can't possibly be used after setjmp
returns. Even the compiler should be aware of something so blindingly obvious, given that it has some kind of special knowledge of setjmp
.
My main interest is finding bugs in a code base that I inherited, so, "use this coding style instead" is not advice I am looking for. However, there are a number of bizarre twists here. For example, if x
is a local variable instead of a parameter, then GCC does not complain. Also, GCC will not complain without the if (some_global)
line. Nice. Something is messing up GCC's flow analysis, or maybe GCC knows something I don't.
So,
Is there an easy way to suppress this warning for this function, just the same way you can cast unused parameters to
(void)
?Or do I just suppress the warning project-wide?
Or am I missing something?
Update: Let me share with you a slightly different version that does not generate a warning:
#include <setjmp.h>
jmp_buf buf;
void func2(int g);
extern int some_global;
void func(int y)
{
int x = y;
if (setjmp(buf))
return;
if (some_global)
x += 5;
func2(x);
}