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

            Bug ID: 117063
           Summary: Incorrect stringop-overread warning using
                    std::vector::insert at -O3
           Product: gcc
           Version: 14.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wsf at fultondesigns dot co.uk
  Target Milestone: ---

Standalone code is below and triggers warning

gcc-14.1.0/include/c++/14.1.0/bits/stl_algobase.h:452:30: warning: 'void*
__builtin_memcpy(void*, const void*, long unsigned int)' reading between 9 and
9223372036854775800 bytes from a region of size 8 [-Wstringop-overread]

Compile using: g++ -std=c++14 -O2

No warning with gcc 13.3.0, warning appears with gcc 14.1.0 and 14.2.0.

Also warns, but requires -O3 instead of -O2, with godbolt.org build: g++
(Compiler-Explorer-Build-gcc-e9a213810aa9b87c7c868c93d5a93f3c63e03a3c-binutils-2.42)
15.0.0 20241010 (experimental)

Workarounds:
1) Compile with -O1 instead of -O2
2) Alternative code using #if 1 instead of #if 0 below


#include <iostream>
#include <vector>
using std::vector;
#include <algorithm>

template<class C, class V> inline
typename C::const_iterator container_upper_bound(const C& container, const V&
value)
{
        return upper_bound(container.begin(), container.end(), value);
}

template<class T>
T Next(T val)
{
        ++val;
        return val;
}

template<class T>
T Previous(T val)
{
        --val;
        return val;
}

long m_firstDate = 500;
long m_lastDate = 2400;
vector<long> event_dates { 300, 2000 };

int main() {
    vector<long> relevantDates;

    if (event_dates.size() > 0)
    {
        if (m_firstDate < event_dates.front())
            relevantDates.push_back(0L);
        vector<long>::const_iterator start = ((m_firstDate <
event_dates.front()) ? event_dates.begin() 
            : Previous(container_upper_bound(event_dates, m_firstDate)));
        auto upper_iterator = container_upper_bound(event_dates, m_lastDate);
        vector<long>::const_iterator stop = (upper_iterator !=
event_dates.end()) ?  Next(upper_iterator) : event_dates.end();
#if 1
        // Results in stringop-overread warning
        relevantDates.insert(relevantDates.end(), start, stop);
#else
        // Workaround to remove warning
        std::copy(start, stop, std::back_inserter(relevantDates));
#endif
    }
    std::cout << "dates..." << std::endl;
    for (auto v : relevantDates)
        std::cout << v << std::endl;
    return 0;
}

Reply via email to