* [Bug tree-optimization/109299] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
@ 2023-03-27 15:38 ` xry111 at gcc dot gnu.org
2023-03-27 20:57 ` redi at gcc dot gnu.org
` (10 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-03-27 15:38 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
Xi Ruoyao <xry111 at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |xry111 at gcc dot gnu.org
--- Comment #1 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
You are reducing it wrong. The reduced example should indeed trigger the
warning, as it's copying 5 wchar's into a buffer with length 4.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug tree-optimization/109299] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
2023-03-27 15:38 ` [Bug tree-optimization/109299] " xry111 at gcc dot gnu.org
@ 2023-03-27 20:57 ` redi at gcc dot gnu.org
2023-03-27 21:14 ` benni.buch at gmail dot com
` (9 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2023-03-27 20:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Are you really using GCC 12.0? If not, please fix the Version field.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug tree-optimization/109299] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
2023-03-27 15:38 ` [Bug tree-optimization/109299] " xry111 at gcc dot gnu.org
2023-03-27 20:57 ` redi at gcc dot gnu.org
@ 2023-03-27 21:14 ` benni.buch at gmail dot com
2023-03-28 7:11 ` [Bug libstdc++/109299] [12/13 Regression] " rguenth at gcc dot gnu.org
` (8 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: benni.buch at gmail dot com @ 2023-03-27 21:14 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
Benjamin Buch <benni.buch at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Version|12.0 |12.1.0
--- Comment #3 from Benjamin Buch <benni.buch at gmail dot com> ---
You are right, I use GCC 12.1.0, sorry!
I also ran reproduce it via godbolt.org starting from version 12.1 until
current trunk.
https://godbolt.org/z/Y9frhGr86
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug libstdc++/109299] [12/13 Regression] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
` (2 preceding siblings ...)
2023-03-27 21:14 ` benni.buch at gmail dot com
@ 2023-03-28 7:11 ` rguenth at gcc dot gnu.org
2023-03-28 9:28 ` redi at gcc dot gnu.org
` (7 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-03-28 7:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Known to fail| |12.2.0, 13.0
Target Milestone|--- |12.3
Priority|P3 |P2
Last reconfirmed| |2023-03-28
Known to work| |11.3.0
Version|12.1.0 |12.2.0
Summary|wrong warning on |[12/13 Regression] wrong
|std::wstring with -O2 |warning on std::wstring
|-std=c++20 |with -O2 -std=c++20
|-D_FORTIFY_SOURCE=2 |-D_FORTIFY_SOURCE=2
Component|tree-optimization |libstdc++
Status|UNCONFIRMED |NEW
--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed for the unreduced testcase. Works for GCC 11.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug libstdc++/109299] [12/13 Regression] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
` (3 preceding siblings ...)
2023-03-28 7:11 ` [Bug libstdc++/109299] [12/13 Regression] " rguenth at gcc dot gnu.org
@ 2023-03-28 9:28 ` redi at gcc dot gnu.org
2023-03-28 9:37 ` redi at gcc dot gnu.org
` (6 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2023-03-28 9:28 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is not a library bug. The library code is:
basic_string(basic_string&& __str) noexcept
: _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
{
if (__str._M_is_local())
{
traits_type::copy(_M_local_buf, __str._M_local_buf,
__str.length() + 1);
}
else
{
_M_data(__str._M_data());
_M_capacity(__str._M_allocated_capacity);
}
The warning is coming from the call to traits_type::copy which only happens if
the string fits in the local buffer. Warning that it overflows the buffer is
not helpful when we don't take that branch BECAUSE IT WOULD OVERFLOW THE
BUFFER.
These warnings need to be removed from the compiler.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug libstdc++/109299] [12/13 Regression] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
` (4 preceding siblings ...)
2023-03-28 9:28 ` redi at gcc dot gnu.org
@ 2023-03-28 9:37 ` redi at gcc dot gnu.org
2023-03-28 9:47 ` jakub at gcc dot gnu.org
` (5 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2023-03-28 9:37 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This change to bits/basic_string.h seems to shut the compiler up:
--- include/bits/basic_string.h 2023-02-27 13:42:21.295513537 +0000
@@ -271,7 +271,15 @@
_GLIBCXX20_CONSTEXPR
bool
_M_is_local() const
- { return _M_data() == _M_local_data(); }
+ {
+ if (_M_data() == _M_local_data())
+ {
+ if (_M_string_length > _S_local_capacity)
+ __builtin_unreachable();
+ return true;
+ }
+ return false;
+ }
// Create & Destroy
_GLIBCXX20_CONSTEXPR
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug libstdc++/109299] [12/13 Regression] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
` (5 preceding siblings ...)
2023-03-28 9:37 ` redi at gcc dot gnu.org
@ 2023-03-28 9:47 ` jakub at gcc dot gnu.org
2023-03-28 10:03 ` redi at gcc dot gnu.org
` (4 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-03-28 9:47 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org
--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Do we also get better generated code with your above patch? If so, we should
go with it.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug libstdc++/109299] [12/13 Regression] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
` (6 preceding siblings ...)
2023-03-28 9:47 ` jakub at gcc dot gnu.org
@ 2023-03-28 10:03 ` redi at gcc dot gnu.org
2023-03-28 10:05 ` redi at gcc dot gnu.org
` (3 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2023-03-28 10:03 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Yes, it looks better:
--- 109299-orig.s 2023-03-28 11:01:09.886119878 +0100
+++ 109299-fixed.s 2023-03-28 11:01:21.361187289 +0100
@@ -235,21 +235,14 @@
xorl %esi, %esi
call wmemset
movq (%rsp), %rax
- leaq 48(%rsp), %rdi
- movq $42, 8(%rsp)
- movq %rdi, 32(%rsp)
+ leaq 32(%rsp), %rdi
+ movq %rbx, (%rsp)
+ movq $42, 40(%rsp)
movl $0, 168(%rax)
- cmpq %rbx, %rax
- je .L35
movq %rax, 32(%rsp)
movq 16(%rsp), %rax
- movq %rax, 48(%rsp)
- movl $42, %eax
-.L36:
- leaq 32(%rsp), %rdi
- movq %rax, 40(%rsp)
- movq %rbx, (%rsp)
movq $0, 8(%rsp)
+ movq %rax, 48(%rsp)
movl $0, 16(%rsp)
call
_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE10_M_disposeEv
movq %rsp, %rdi
@@ -261,17 +254,10 @@
popq %rbx
.cfi_def_cfa_offset 8
ret
-.L35:
+.L36:
.cfi_restore_state
- movl $4, %ecx
- movl $43, %edx
- movq %rbx, %rsi
- call __wmemcpy_chk
- movq 8(%rsp), %rax
- jmp .L36
-.L38:
movq %rax, %rbx
- jmp .L37
+ jmp .L35
.globl __gxx_personality_v0
.section .gcc_except_table,"a",@progbits
.LLSDA1811:
@@ -282,7 +268,7 @@
.LLSDACSB1811:
.uleb128 .LEHB0-.LFB1811
.uleb128 .LEHE0-.LEHB0
- .uleb128 .L38-.LFB1811
+ .uleb128 .L36-.LFB1811
.uleb128 0
.LLSDACSE1811:
.section .text.startup
@@ -294,7 +280,7 @@
.type main.cold, @function
main.cold:
.LFSB1811:
-.L37:
+.L35:
.cfi_def_cfa_offset 80
.cfi_offset 3, -16
movq %rsp, %rdi
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug libstdc++/109299] [12/13 Regression] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
` (7 preceding siblings ...)
2023-03-28 10:03 ` redi at gcc dot gnu.org
@ 2023-03-28 10:05 ` redi at gcc dot gnu.org
2023-03-28 20:14 ` cvs-commit at gcc dot gnu.org
` (2 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2023-03-28 10:05 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
And the reason I put the hint in _M_is_local() not in the function giving the
warning is that it might help in other functions too.
I haven't tested it yet though.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug libstdc++/109299] [12/13 Regression] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
` (8 preceding siblings ...)
2023-03-28 10:05 ` redi at gcc dot gnu.org
@ 2023-03-28 20:14 ` cvs-commit at gcc dot gnu.org
2023-03-28 23:33 ` [Bug libstdc++/109299] [12 " cvs-commit at gcc dot gnu.org
2023-03-28 23:34 ` redi at gcc dot gnu.org
11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-03-28 20:14 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
--- Comment #10 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:
https://gcc.gnu.org/g:bf78b43873b0b7e8f9a430df38749b8b61f9c9b8
commit r13-6915-gbf78b43873b0b7e8f9a430df38749b8b61f9c9b8
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Mar 28 10:50:40 2023 +0100
libstdc++: Tell GCC what basic_string::_M_is_local() means [PR109299]
This avoids a bogus warning about overflowing a buffer, because GCC
can't tell that we don't copy into the buffer unless it fits. By adding
a __builtin_unreachable() hint we inform the compiler about the
invariant that the buffer is only used when it's big enough.
This can also improve codegen, by eliminating dead code that GCC
couldn't tell was unreachable.
libstdc++-v3/ChangeLog:
PR libstdc++/109299
* include/bits/basic_string.h (basic_string::_M_is_local()): Add
hint for compiler that local strings fit in the local buffer.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug libstdc++/109299] [12 Regression] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
` (9 preceding siblings ...)
2023-03-28 20:14 ` cvs-commit at gcc dot gnu.org
@ 2023-03-28 23:33 ` cvs-commit at gcc dot gnu.org
2023-03-28 23:34 ` redi at gcc dot gnu.org
11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-03-28 23:33 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
--- Comment #11 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:
https://gcc.gnu.org/g:00ac6fa3f2a54fb9cc17b7b7f51eae3c6bf7a1bd
commit r12-9330-g00ac6fa3f2a54fb9cc17b7b7f51eae3c6bf7a1bd
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Mar 28 10:50:40 2023 +0100
libstdc++: Tell GCC what basic_string::_M_is_local() means [PR109299]
This avoids a bogus warning about overflowing a buffer, because GCC
can't tell that we don't copy into the buffer unless it fits. By adding
a __builtin_unreachable() hint we inform the compiler about the
invariant that the buffer is only used when it's big enough.
This can also improve codegen, by eliminating dead code that GCC
couldn't tell was unreachable.
libstdc++-v3/ChangeLog:
PR libstdc++/109299
* include/bits/basic_string.h (basic_string::_M_is_local()): Add
hint for compiler that local strings fit in the local buffer.
(cherry picked from commit bf78b43873b0b7e8f9a430df38749b8b61f9c9b8)
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug libstdc++/109299] [12 Regression] wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2
2023-03-27 15:01 [Bug libstdc++/109299] New: wrong warning on std::wstring with -O2 -std=c++20 -D_FORTIFY_SOURCE=2 benni.buch at gmail dot com
` (10 preceding siblings ...)
2023-03-28 23:33 ` [Bug libstdc++/109299] [12 " cvs-commit at gcc dot gnu.org
@ 2023-03-28 23:34 ` redi at gcc dot gnu.org
11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2023-03-28 23:34 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109299
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |FIXED
Status|NEW |RESOLVED
--- Comment #12 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for 12.3
^ permalink raw reply [flat|nested] 13+ messages in thread