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

            Bug ID: 87982
           Summary: No error for std::generate_n(ptr, ptr, f)
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

#include <algorithm>

int main()
{
  int a[2];
  std::generate_n(a, a+2, []{ return 0;});
}

This compiles without error, but crashes at runtime.

The code should use std::generate(a, a+2, f) or std::generate_n(a, 2, f).

With -Wextra -Wsystem-headers there's a clue something is wrong, but it doesn't
make the problem clear:

In file included from /usr/include/c++/7/algorithm:62:0,
                 from gn.cc:1:
/usr/include/c++/7/bits/stl_algo.h: In instantiation of ‘_OIter
std::generate_n(_OIter, _Size, _Generator) [with _OIter = int*; _Size = int*;
_Generator = main()::<lambda()>]’:
gn.cc:6:41:   required from here
/usr/include/c++/7/bits/stl_algo.h:4467:13: warning: ordered comparison of
pointer with integer zero [-Wextra]
     __niter > 0; --__niter, ++__first)
     ~~~~~~~~^~~

Reply via email to