Bogus -Wrestrict in builtin_memcpy from std::string & C++20 on kinetic

Bug #1990811 reported by Chris Halse Rogers
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gcc-defaults (Ubuntu)
Confirmed
Low
Unassigned

Bug Description

The attached cpp file, when built with `g++ -O3 -S --std=c++20 -o test.o gcc-bug-test.cpp -Wall -Werror` on g++12, fails to compile with the following error. Notably, this error requires *both* --std=c++20 and -O3 - it compiles correctly at -O2, and it compiles correctly at -O3 with --std=c++17.

In file included from /usr/include/c++/12/string:40,
                 from gcc-bug-test.cpp:1:
In static member function ‘static constexpr std::char_traits<char>::char_type* std::char_traits<char>::copy(char_type*, const char_type*, std::size_t)’,
    inlined from ‘static constexpr void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_S_copy(_CharT*, const _CharT*, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:423:21,
    inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Allocator>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_replace(size_type, size_type, const _CharT*, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.tcc:532:22,
    inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::replace(size_type, size_type, const _CharT*, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:2171:19,
    inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::insert(size_type, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:1858:29,
    inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&) [with _CharT = char; _Traits = char_traits<char>; _Alloc = allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:3531:35,
    inlined from ‘std::string to_camel_case(const std::string&)’ at gcc-bug-test.cpp:6:45:
/usr/include/c++/12/bits/char_traits.h:431:56: error: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ accessing 9223372036854775810 or more bytes at offsets [2, 9223372036854775807] and 1 may overlap up to 9223372036854775813 bytes at offset -3 [-Werror=restrict]
  431 | return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
      | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors

[[Testcase]]
$ cat gcc-bug-test.cpp

#include <string>

std::string to_camel_case(std::string const& name)
{
    std::string camel_cased_name;
    camel_cased_name = std::string{name[0]} + name.substr(1);
    return camel_cased_name;
}

$ g++ -O3 -S --std=c++20 -o test.o gcc-bug-test.cpp -Wall -Werror

ProblemType: Bug
DistroRelease: Ubuntu 22.10
Package: g++ 4:12.2.0-1ubuntu1
ProcVersionSignature: Ubuntu 5.19.0-15.15-generic 5.19.0
Uname: Linux 5.19.0-15-generic x86_64
NonfreeKernelModules: zfs zunicode zavl icp zcommon znvpair
ApportVersion: 2.23.0-0ubuntu2
Architecture: amd64
CasperMD5CheckResult: pass
Date: Mon Sep 26 16:02:09 2022
InstallationDate: Installed on 2021-06-26 (456 days ago)
InstallationMedia: Ubuntu 21.10.0 2021.05.28 amd64 "bcachefs" (20210622)
SourcePackage: gcc-defaults
UpgradeStatus: Upgraded to kinetic on 2022-09-13 (13 days ago)

Revision history for this message
Chris Halse Rogers (raof) wrote :
Michał Sawicz (saviq)
Changed in gcc-defaults (Ubuntu):
status: New → Confirmed
Olivier Gayot (ogayot)
description: updated
Olivier Gayot (ogayot)
summary: - Bogus -Wrestrict in builtin_memcpy from std::string & C++20
+ Bogus -Wrestrict in builtin_memcpy from std::string & C++20 on kinetic
Dave Jones (waveform)
tags: added: foundations-todo
Nick Rosbrook (enr0n)
Changed in gcc-defaults (Ubuntu):
importance: Undecided → Low
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.