This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

A TOT bug in handling volatile vars?


I'm combing through my backlog of Apple/NeXT gcc bugs :-), and
ran into the following long-standing ditty:

#include <stdio.h>
#include <setjmp.h>

jmp_buf jenv;

int main(int ac, char *av[])
{
volatile char *v1 = 0; /* declare volatile so compiler knows we may do
things with this variable that it can not
detect. It should, therefore tread very
lightly when optimizing code that uses this.
*/

printf("v1 (%d) %s\n",
v1, v1);

if (!setjmp(jenv)) {
v1 = new char[25];

printf("v1 (%d) %s\n", v1, "should not be null");

delete (v1);
v1=NULL;
longjmp(jenv,1);
} else {
printf("v1 (%d) %s\n", v1, "should be null");
}
}

When built without optimizations on i686-pc-linux-gnu, the program
produces

v1 (0) (null)
v1 (134935784) should not be null
v1 (0) should be null

as I would expect. But the moment you supply -O, the output changes
to

v1 (0) (null)
v1 (134935784) should not be null
v1 (134935784) should be null

which is somewhat less to my liking (in spite of the fact that
previous gcc releases apparently generated this for many, many years).

So my question for the experts is, is this a bug? Personally, I
think so. Had v1 _not_ been marked volatile, I could see the
compiler deciding that 'v1=NULL;' is dead code (assuming that
it does not recognize the magical powers of longjmp -- does it
have to, anyway?). But since v1 _is_ volatile, I would expect
the store to be emitted anyway. Or am I off in the weeds somewhere?

Thanks in advance, and a good weekend to all,

--Zem
--------------------------------------------------------------
Ziemowit Laski 1 Infinite Loop, MS 301-2K
Mac OS X Compiler Group Cupertino, CA USA 95014-2083
Apple Computer, Inc. +1.408.974.6229 Fax .5477


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]