http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56973



             Bug #: 56973

           Summary: crash when capturing variables in nested lambdas

    Classification: Unclassified

           Product: gcc

           Version: 4.8.0

            Status: UNCONFIRMED

          Severity: normal

          Priority: P3

         Component: c++

        AssignedTo: unassig...@gcc.gnu.org

        ReportedBy: dirkmoerm...@gmail.com





The valid C++11 program below segfaults on my machine:



=====

#include <cassert>

#include <cstring>

#include <iterator>



void test(const char* s)

{

   constexpr /*static*/ struct {const char* s;} ar[]={

      {"e4"},

      {"e5"},

      {"Nf3"},

      {"Nc6"},

      {"Bb5"},

      {"a6"}

   };



   auto it=std::begin(ar);

   auto compare=[&it](const char* s)

      {

         assert(std::strcmp(it->s,s)==0);

      };



   auto test=[&](const char* s)

      {

         it=std::begin(ar);

         compare(s);

      };



   test(s);

}



int main(int argc, const char* argv[])

{

   test(argv[1]);

   test(argv[2]);

   test(argv[3]);

   return 0;

}

=====



I compile with: $ g++ -g -O --std=c++11 bug.cc.

I run as: $ ./a.out e4 e4 e4



My machine is a 64 bit linux intel core duo 2 (T4200).



If I make "ar" static the program passes, so I assume that ar is overwritten

prematurely on the stack.



The error only occurs from optimization-levels "-O" onwards.

Reply via email to