incorrect static char * assumption(?)

Dima Volodin dvv@dvv.org
Tue Mar 7 11:29:00 GMT 2000


Richard wrote:

> egcs-2.91.66 appears to be making an erroneous assumption about the
> contents of a static char * within a single printf() call.
>
> SYMPTOM:
>
>         #include <stdio.h>
>         #include <string.h>
>
>         char newstring[5];
>
>         int main(void);
>         char *output(char *);
>
>         int main()
>         {
>                 printf ("%s %s\n", output("this"), output("that"));
>                 printf ("%s\n", output("this"));
>                 printf ("%s\n", output("that"));
>
>                 return 0;
>         }
>
>         char *output(char *string)
>         {
>                 strcpy(newstring, string);
>                 return newstring;
>         }
>
> Output from the program:
>
>         this this
>         this
>         that
>
> Obviously the first line of output should read:
>
>         this that

Obviously, it shouldn't. When printf () starts, the contents of newstring
is whatever the last call to output () put there, that is either "this" or
"that" depending on the order of arguments' evaluation. Strictly speaking,
you cannot even rely on any particular value of newstring as the target CPU
has all the right to evaluate the arguments in parallel, in which case
you're screwed even more.

> Richard

Dima



More information about the Gcc-bugs mailing list