Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ranged for-loop inside lambda crashes #238

Closed
hessbe opened this issue Sep 29, 2019 · 6 comments
Closed

ranged for-loop inside lambda crashes #238

hessbe opened this issue Sep 29, 2019 · 6 comments
Labels
bug Something isn't working

Comments

@hessbe
Copy link

hessbe commented Sep 29, 2019

this example crashes the conversion:
void f() { auto lambda = [](auto container) { for(auto test : container) { } }; }

also with a template instantiation:
`#include
void f() {
auto lambda = [](auto container) {
for(auto test : container) { }
};

std::vector vec{1,2,3,4};
lambda(vec);
}`

[0x13449ff] ... [0x4055aa] Segmentation fault (core dumped)

@andreasfertig andreasfertig added the bug Something isn't working label Sep 30, 2019
@andreasfertig
Copy link
Owner

Hello @hessbe,

thank you! Nice finding. Here we have a range-based for-loop in an unevaluated context. Hence, not all the necessary information is present. My fix would be to show the range-based for-loop as it is in this cases. What do you think?

Andreas

@andreasfertig
Copy link
Owner

On a second thought, it is easier to transform it. This is more consistent and requires less special cases. Your initial example would then look like this:

void f()
{

  class __lambda_1_26
  {
    public:
    template<class type_parameter_0_0>
    inline /*constexpr */ auto operator()(type_parameter_0_0 container) const
    {
      {
        auto && __range1 = container;
        for(; ; )
        {
          auto test;
        }

      }
    }
    private:
    template<class type_parameter_0_0>
    static inline auto __invoke(type_parameter_0_0 container)
    {
      {
        auto && __range1 = container;
        for(; ; )
        {
          auto test;
        }

      }
    }

  };

  __lambda_1_26 lambda = __lambda_1_26{};
}

Andreas

@hessbe
Copy link
Author

hessbe commented Sep 30, 2019

In my opinion that's good enough as long as the instantiation is correct.

Or how difficult is it to show everything with auto? Like this:
{ auto& __range1 = container; auto __begin1 = __range1.begin(); auto __end1 = __range1.end(); for(; __gnu_cxx::operator!=(__begin1, __end1); __begin1.operator++()) { auto test = __begin1.operator*(); // content of loop } }

@andreasfertig
Copy link
Owner

Hello @hessbe,

difficult as at this point it is unknown. It can also be that std::begin or std::end is called. Same goes for the operators. Plus it requires special treatment.
I prefer to transform what is there.

Andreas

@hessbe
Copy link
Author

hessbe commented Sep 30, 2019

Hi Andrea,

I'm fully content with your proposed solution. I was preparing some examples for internal training and this crash popped up so I wanted to make sure you know about it. There was also a template instantiation In the example so the full solution would be shown there and that's all I wanted to show the team.

Ben

andreasfertig added a commit that referenced this issue Sep 30, 2019
… context.

In case of a range-based for-loop in something like a template the types
of range statements are not known. In this case just omit them as they
are not there.
@andreasfertig
Copy link
Owner

Hello @hessbe,

excellent, and sorry for the trouble before your training. A fix is on its way.

Andreas

andreasfertig added a commit that referenced this issue Sep 30, 2019
… context.

In case of a range-based for-loop in something like a template the types
of range statements are not known. In this case just omit them as they
are not there.
andreasfertig added a commit that referenced this issue Sep 30, 2019
… context.

In case of a range-based for-loop in something like a template the types
of range statements are not known. In this case just omit them as they
are not there.
andreasfertig added a commit that referenced this issue Sep 30, 2019
Fixed #238: Prevent crash of a range-based for-loop in an unevaluated context.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants