This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Today I can not see very well, Where is the fail?


Why printf writes wrong stream? Really evaluate all arguments?

> glibc-2.3.4-2

> #include <stdio.h> 
> #include <stdlib.h> 
> 
> #define SIZE_MARCA (256 +62) 
> 
> unsigned char
> marca (const unsigned char *page, const unsigned int id, unsigned char *str_out)
> {
>   unsigned long long p;
>   unsigned char str[SIZE_MARCA];
>   p = 1;
>   if (sprintf (str, "%s%llu%u%u", page, p, 0, (unsigned int) id) <= 0)
>     {
>       printf ("Fail of sprintf\n");
> return 0;
>     };
>   memcpy (str_out, str, SIZE_MARCA - 1);
>   printf ("Marca (%s,%u,)=%s\n", page, id, str_out);
>   return 1;
> }
> 
> 
> 
> 
> int
> main ()
> {
>   unsigned char stre[SIZE_MARCA];
> 
> marca ("test", 1, stre);
> /*A:*/
> printf("%s\n",stre);
> marca ("test", 2, stre);
> printf("%s\n",stre);
> marca ("test", 3, stre);
> printf("%s\n",stre);
> 
> /*B:*/
>   printf ("\"1=%s\"\n \"2=%s\"\n \"3=%s\"\n",
>           (unsigned char *)(marca ("test", 1, stre) == 1 ? (unsigned char *) stre : (const unsigned char *) "Fail"),
>           (unsigned char *) (marca ("test", 2, stre) == 1 ? (unsigned char *) stre : (const unsigned char *) "Fail"),
> (unsigned char *)(marca ("test", 3, stre) == 1 ? (unsigned char *) stre : (const unsigned char *) "Fail"));
> }
> 
> 
> 
I think that output of A: and B: block should be the same but is not the same output.

> cc -Wall -O3 -m32 -march=pentium3 -mcpu=pentium3 --ansi    test.c   -o test
> Press any key to continue...
> ./test
> Marca (test,1,)=test101
> test101
> Marca (test,2,)=test102
> test102
> Marca (test,3,)=test103
> test103
> Marca (test,3,)=test103
> Marca (test,2,)=test102
> Marca (test,1,)=test101
> "1=test101"
>  "2=test101"
>  "3=test101"
> 


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