This is the mail archive of the gcc-bugs@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]

Re: incorrect static char * assumption(?)


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


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