Re: Segfault only with optimization



jacob navia wrote, On 17/03/08 21:45:
santosh wrote:
jacob navia wrote:

Peter Nilsson wrote:
Jeff Taylor <dev.n...@xxxxxxxx> wrote:
I have a C program that GCC compiles without warnings and
it runs just fine.
Realise that output is not the only measure of correctness.

Interesting interesting...

Can you name any other measure that doesn't rely on
output?

A program that invokes a buffer overrun, but happens to produce expected
output is surely not correct?

How the hell do you know that there is a buffer
overrun if the output is not affected?

To KNOW that there is a buffer overrun the program MUST
do something it should not do, i.e. produce an output
that is different than the expected output

If the output of the program is normal you have NO WAY
to know there is a buffer overrun.

You obviously do not understand what static analysis is. Or what reviews are for that matter! Just to explain...

Static analysis is when you analyse the code *without* running it. Various methods of static analysis are used on safety critical software (in my previous job we were going to contract out the static analysis of the code to a third party, I know this because I was going out with one of out SW QA engineers visiting the companies to evaluate them). Since in static analysis you do not run the code you cannot sensibly consider the report produced (which should certainly include identifying buffer overflows) as an output of the program any more than you can consider 1.1415926536 to be the output of a text file containing only the character "2" in it (it is the output of the program that runs on the file).

And if you hook a debugger and see some buffer being
overrun that *is* output of course.

I suppose you will consider the report by an independent company that never runs the program to be output of the program as well. Also I suppose the minutes of the design review (held before a line of code is written) is an output of the program, and yes bugs *are* found in design reviews if they are held properly.

Using a psychic medium is not allowed.

However, when I compile it using any of the -O options
(-O0, -O1, etc.) it segfaults when executed (still no
compiler warnings or errors).
I'm afraid you are responsible for diagnosing errors in your
code, not your compiler.

Deep thought.

He means semantic errors. Or does your compiler do the programming for
the programmer? :-)

<snip>

What I mean is that such sentences are not helpful to the OP.
And we should try to be less patronizing with newcomers and
people that ask questions. I am sure the OP knows that it is the
programmer that debugs the program. He was asking us to help him
to do that, not to answer him

just do it pal...

The implication of what the OP posted was that he found it surprising that he would get a segfault on code that did not produce a warning, this is a common mistake. So pointing out that the compiler is not responsible for finding the errors is not unhelpful.

Personally I would start by turning up the warnings on the compiler and fixing the issues that throws up (as someone else suggested) and then try running it through valgrind (as someone else suggested).
--
Flash Gordon
.



Relevant Pages

  • Re: How to avoid undesired default values?
    ... are documented in the documentation that comes with your compiler. ... of why static analysis for uninitialized variables does no work. ... This is why the good complete checking for uninitialized variables is done ...
    (comp.lang.fortran)
  • Re: subroutine stack and C machine model
    ... was your canonical non-syntactic distinction. ... What matters to a compiler is what can ... did some static analysis on it and fixed all the memory leaks, ...
    (comp.lang.c)
  • Re: self-confidence of compiler
    ... What you are looking for is static analysis of the code, ... beyond what a compiler is ... Perhaps more importantly than the processing power (at least in my ... must be able to work out - would make the C# language specification ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Runtime optimizations
    ... >> done based on static analysis of the code when it's first compiled. ... > involve file I/O, which is slow relative to JIT compile time. ... > compiler were separated nicely like FLINT, ... ; "There is no dark side of the moon really; matter of fact, ...
    (comp.lang.lisp)