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

            Bug ID: 109737
           Summary: [13/14] Hitting unreachable code when using
                    std::string::assign with iterators
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: enrico.seiler+gccbugs at outlook dot com
  Target Milestone: ---

The following emits "runtime error: execution reached an unreachable program
poin" when compiling with `-g -fsanitize=undefined -std=c++20`:

```

#include <cassert>
#include <sstream>
#include <string>

void fits_in_local_buffer()
{
    std::stringstream source{"123457890123456"};
    std::string str;
    str.assign(std::istreambuf_iterator<char>{source},
std::istreambuf_iterator<char>{});
    assert(str == "123457890123456");
}

void does_not_fit_in_local_buffer()
{
    std::stringstream source{"1234578901234567"};
    std::string str;
    str.assign(std::istreambuf_iterator<char>{source},
std::istreambuf_iterator<char>{});
    assert(str == "1234578901234567");
}

int main()
{
    fits_in_local_buffer(); // OK
    does_not_fit_in_local_buffer(); // Not OK
    return 0;
}

```

Compiler Explorer: https://godbolt.org/z/6obr7afon

* Must be at least compiled with CPP20
* Using std::string's constructor with the iterators works.
* Unreachable was introduced with PR109299
https://github.com/gcc-mirror/gcc/commit/bf78b43873b0b7e8f9a430df38749b8b61f9c9b8
* The std::istreambuf_iterator is used as an example, but the same happens when
using, for example, a std::views::iota and using its iterators
  • [Bug libstdc++/10973... enrico.seiler+gccbugs at outlook dot com via Gcc-bugs

Reply via email to