Re: gcc bug?

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 11/07/03


Date: Fri, 7 Nov 2003 15:48:02 -0500 (EST)


On Fri, 7 Nov 2003, Victor Irzak wrote:
>
> This program causes seg fault on gcc, but executes fine on icc and VC7.
> Is there a reason for it or is it a bug?

Yes. It's a bug in your program.

> Note: if "char * const str" is changed to "char * str",
> the gcc problem disappears.

The bug is still present even with the 'const' removed.

> #include "stdio.h"

Should be
  #include <stdio.h>

>
> int main() {
> char * const str="ya";

'str' points to the string "ya", which is stored somewhere
off in memory, possibly in ROM (think: a segment to which your
program cannot write).

> char *first = &str[0], *second =&str[1];

'first' and 'second' also point into that same string, in
read-only memory. By the way, do you understand that

    char *first = str, *second = str+1;

would be equivalent to the line above?

> char tmp;
>
> printf("%s\n", str);

Prints the string "ya". This is fine, so far.

> tmp = *first;
> *first = *second;

Here's your bug. You try to assign a new value to
the char object pointed to by 'first' -- and that
object is off in read-only memory. You can't modify
string literals in C.
  This is where the program segfaults with GCC.

> *second = tmp;

A second bug.

>
> printf("%s\n", str);
>
> return 0;
> }

To make your program work as expected, and remove
the undefined behavior, you could create an array
local to 'main' in which to store your string:

    char str[] = "ya";

(then proceed as above). See this newsgroup's
FAQ for more information.

-Arthur



Relevant Pages

  • Re: Gnat GPL 2010 available soon
    ... it hasn't been hard to find the first bug. ... in String) return String is ... gcc -c -gnat12 validate.adb ... | Include the exact gcc or gnatmake command that you ...
    (comp.lang.ada)
  • Re: FAQ Topic - How can I create a Date object from a String? (2010-07-27)
    ... include something explicitly on writing& reading JSON date strings. ... ECMA-5 doesn't say that a Date string is parsed in UTC. ... The downside of browsers being released with a bug is that even after the browser vendor releases an update with a fix, there will still be some users that have an older browser with the broken functionality. ...
    (comp.lang.javascript)
  • SSE vs. stack alignment vs. pthread
    ... I'm not sure yet if this is an application bug, a gcc ... audacity is a threaded program. ... suspicious or low-level enough to affect the stack alignment. ...
    (freebsd-hackers)
  • Re: Implementing strstr
    ... Worst kind of bug: a bug that doesn't cause your code to fail. ... you use a scripting language with builtin string ... that pattern is a device for the user to abstract the name. ... fraud and a thief, Dweebach, and this issue is not going away. ...
    (comp.lang.c)
  • kernel + gcc 4.1 = several problems
    ... Could you reproduce the bug by the new kernel, ... This is with the GCC recompile, so it's not a distro problem. ... using this compiler, and will report back if it ...
    (Linux-Kernel)