Optimization bug in g++ 3.4?

T

Thorsten Raasch

Hi,

the following C++ program seems to yield a segmentation fault
in g++ 3.4 when compiled with any of the optimization options -O*,
e.g. with "g++ -O2 bug.cpp -o bug".



class TestClass {
public:
TestClass() {
c[0] = 1;
c[1] = 2;
}

int f(const unsigned int i) { // segfault during optimization
// int f(unsigned int i) { // works fine
return c[(i==1) ? 1 : 0];
}

int c[2];
};

int main()
{
TestClass c;
int x = c.f(1);

return 0;
}



I tested the code with g++ 3.4 under Debian, CentOS and SunOS, resulting
in a segmentation fault in each case. Can anyone confirm the bug?
First, the point seems to be that the argument of the method f is a
"const unsigned int". Changing the argument either into a "const int" or
an "unsigned int" indeed yields a compileable code.
Second point: if the return value is just (i==1) ? 1 : 0, the code also
compiles fine.



Best,
Thorsten Raasch
 
V

Victor Bazarov

Thorsten said:
the following C++ program seems to yield a segmentation fault
in g++ 3.4 when compiled with any of the optimization options -O*,
e.g. with "g++ -O2 bug.cpp -o bug".
[...]
I tested the code with g++ 3.4 under Debian, CentOS and SunOS, resulting
in a segmentation fault in each case. Can anyone confirm the bug?

Please post to one of gnu.g++.* newsgroups. Specific compiler behaviour
is off-topic here, especially if there is a newsgroup dedicated to that
compiler.
 
T

Thorsten Raasch

Victor said:
(...)
Please post to one of gnu.g++.* newsgroups. Specific compiler behaviour
is off-topic here, especially if there is a newsgroup dedicated to that
compiler.

Sorry for being OT. Especially the gnu.g++.bug newsgroup is full of
spam, so I hesitated posting there. I did try now, hopefully someone can
help me there.
 
J

John Ratliff

Thorsten said:
Hi,

the following C++ program seems to yield a segmentation fault
in g++ 3.4 when compiled with any of the optimization options -O*,
e.g. with "g++ -O2 bug.cpp -o bug".



class TestClass {
public:
TestClass() {
c[0] = 1;
c[1] = 2;
}

int f(const unsigned int i) { // segfault during optimization
// int f(unsigned int i) { // works fine
return c[(i==1) ? 1 : 0];
}

int c[2];
};

int main()
{
TestClass c;
int x = c.f(1);

return 0;
}



I tested the code with g++ 3.4 under Debian, CentOS and SunOS, resulting
in a segmentation fault in each case. Can anyone confirm the bug?
First, the point seems to be that the argument of the method f is a
"const unsigned int". Changing the argument either into a "const int" or
an "unsigned int" indeed yields a compileable code.
Second point: if the return value is just (i==1) ? 1 : 0, the code also
compiles fine.



Best,
Thorsten Raasch

I get no segfault with mingw/g++ 3.4.2 under Windows XP/msys.

I would post to the gcc-help mailing list if you're getting problems
with g++.

--John Ratliff
 
B

Branimir Maksimovic

John Ratliff said:
Thorsten said:
Hi,

the following C++ program seems to yield a segmentation fault
in g++ 3.4 when compiled with any of the optimization options -O*,
e.g. with "g++ -O2 bug.cpp -o bug".



class TestClass {
public:
TestClass() {
c[0] = 1;
c[1] = 2;
}

int f(const unsigned int i) { // segfault during optimization
// int f(unsigned int i) { // works fine
return c[(i==1) ? 1 : 0];
}

int c[2];
};

int main()
{
TestClass c;
int x = c.f(1);

return 0;
}

I get no segfault with mingw/g++ 3.4.2 under Windows XP/msys.

You didn't get segfault because compiler just silently fails and
does not produce object file.

Greetings, Bane.
 
J

John Ratliff

Branimir said:
Thorsten said:
Hi,

the following C++ program seems to yield a segmentation fault
in g++ 3.4 when compiled with any of the optimization options -O*,
e.g. with "g++ -O2 bug.cpp -o bug".



class TestClass {
public:
TestClass() {
c[0] = 1;
c[1] = 2;
}

int f(const unsigned int i) { // segfault during optimization
// int f(unsigned int i) { // works fine
return c[(i==1) ? 1 : 0];
}

int c[2];
};

int main()
{
TestClass c;
int x = c.f(1);

return 0;
}

I get no segfault with mingw/g++ 3.4.2 under Windows XP/msys.


You didn't get segfault because compiler just silently fails and
does not produce object file.

Greetings, Bane.

You're right. I didn't notice that. I compiled one without optimization
first, and that's what I was running later with optimization turned on.

--John Ratliff
 
T

Thorsten Raasch

John said:
You're right. I didn't notice that. I compiled one without optimization
first, and that's what I was running later with optimization turned on.

--John Ratliff

So can you both confirm the segfault? The exact compiler versions I
tested the code with were 3.4.3 (under CentOS) and 3.4.4 (under SunOS).
Under g++ 4.0.2, the code compiles fine.
I'm currently reporting the bug to bugzilla.

By the way, does anyone know whether the gnu.g++.bug and gnu.g++.help
newsgroups are working at all? I posted my problem there yesterday, but
the posts did not arrive so far.

Best,
Thorsten Raasch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top