Re: Doubts regarding the issue (bug 62181)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 3 Feb 2022 at 18:46, Krishna Narayanan via Gcc-help
<gcc-help@xxxxxxxxxxx> wrote:
>
> Respected Sir/Madam,
> I have been working on an issue (bug 62181) [C/C++] Expected new warning:
> "adding 'char' to a string does not append to the string"
> [-Wstring-plus-int].This is asking for a warning being added to gcc for
> such a case.Not using concat and indexing which were mentioned earlier as a
> solution to the warning I tried the example given in  https://godbolt.org/
> and was trying different permutations of addresses and pointers in clang
> and gcc (trunk), I am not sure but I tried '-w' in compiler flags and it
> worked fine for both clang and gcc ,it did not show any warning.I am not
> sure what exactly did the flag do but the warning arises in other
> optimizations.
> I had a doubt regarding the output of a slightly modified code in which I
> have taken a character instead of int ,
> #include <stdio.h>
>  char ch ='o';
> char bar()  {
> return 1;
>  }
> int foobar()  {
>  return ch; }
>  int main()
> {   const char* a = "aa";
>  const char *b = "bb" + bar();
> const char *c = "cc" + foobar();
>   printf("%s, %s, %s", a, b, c);
>  return 0 ;
> }
> In the output I get zR when I did it with gcc and get unicode when done
> with g++10.1 .I did not understand how does this output arise.Can you
> please help me out with this?

What do you mean you get "unicode"?

"bb" + bar() is equivalent to "bb" + (int)bar()
which is equivalent to "bb" + 1

"cc" + foobar() is equivalent to "cc" + (int)'o'
which has undefined behaviour, because it produces a pointer that is
outside the array of three chars "cc\0".
It will print whatever happens to be in memory approximately 100 bytes
after the "cc" string literal. That is random garbage of some kind.

The precise behaviour you get from this undefined behaviour is not
important, the point is that it's undefined behaviour, and that's why
a warning would be useful.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux