https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92197

            Bug ID: 92197
           Summary: String Concatenation, Commutativity, and Side-Effect
                    Inducing Functions
           Product: gcc
           Version: 9.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: LouisJenkinsCS at hotmail dot com
  Target Milestone: ---

In GCC version 9.2.1 (and some previous versions as far back as 7.4.0), this
program results in different behavior than what is expected...

```cpp
   #include <string>
   #include <iostream>

   char n = '0';

   std::string g(void) {
       n++;
       return std::string("g") + n;
   }

   std::string f(void) {
       n++;
       return std::string("f") + n;
   }

   int main(void) {
       std::string temp = "";
       temp += "(" + g() + "," + f() + ")";
       std::cout << temp << std::endl;
   }
```

In GCC, it produces `(g2,f1)`, which implies that `g()` was called before
`f()`; in Clang version 8.0.0 _and_ 10.0.1, it produces the desired output of
`(g1,f2)`. This subtle bug can result in some _very_ bad behavior; for context,
I am a TA in a class and a student's submission just looped infinitely without
any reasonable cause. The assignment was on implementing a recursive descent
parser, and the student used a global variable similar to what can be seen
here, and it resulted in it resolving this rule out of order...

SL -> S, SL

Where it originally was to call 'S' and then 'SL', it instead calls 'SL' (which
infinitely recurses) and then 'S'. I'm assuming this is not an intended effect.

Reply via email to