public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/104875] New: libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type
@ 2022-03-10 23:51 redi at gcc dot gnu.org
  2022-03-11  0:22 ` [Bug libstdc++/104875] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-03-10 23:51 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104875
           Summary: libstdc++-v3/src/c++11/codecvt.cc:312:24: warning:
                    left shift count >= width of type
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---
            Target: avr, msp430-elf

/home/jwakely/src/gcc/gcc/libstdc++-v3/src/c++11/codecvt.cc:312:24: warning:
left shift count >= width of type [-Wshift-count-overflow]
  312 |       char32_t c = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4 -
0x3C82080;
      |                    ~~~~^~~~~~

c1 is an unsigned char, so c1 << 18 promotes to int, but for 16-bit int that
doesn't work as intended.

It needs to be converted to a 32-bit type first, e.g. (char32_t)c1 << 18

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/104875] libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type
  2022-03-10 23:51 [Bug libstdc++/104875] New: libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type redi at gcc dot gnu.org
@ 2022-03-11  0:22 ` redi at gcc dot gnu.org
  2022-03-14 13:09 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-03-11  0:22 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-03-11
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(c1 << 12) is wrong too, because it loses the high bits.

We should just declare all those variables as char32_t in the first place.

--- a/libstdc++-v3/src/c++11/codecvt.cc
+++ b/libstdc++-v3/src/c++11/codecvt.cc
@@ -254,7 +254,7 @@ namespace
     const size_t avail = from.size();
     if (avail == 0)
       return incomplete_mb_character;
-    unsigned char c1 = from[0];
+    char32_t c1 = (unsigned char) from[0];
     // https://en.wikipedia.org/wiki/UTF-8#Sample_code
     if (c1 < 0x80)
     {
@@ -267,7 +267,7 @@ namespace
     {
       if (avail < 2)
        return incomplete_mb_character;
-      unsigned char c2 = from[1];
+      char32_t c2 = (unsigned char) from[1];
       if ((c2 & 0xC0) != 0x80)
        return invalid_mb_sequence;
       char32_t c = (c1 << 6) + c2 - 0x3080;
@@ -279,12 +279,12 @@ namespace
     {
       if (avail < 3)
        return incomplete_mb_character;
-      unsigned char c2 = from[1];
+      char32_t c2 = (unsigned char) from[1];
       if ((c2 & 0xC0) != 0x80)
        return invalid_mb_sequence;
       if (c1 == 0xE0 && c2 < 0xA0) // overlong
        return invalid_mb_sequence;
-      unsigned char c3 = from[2];
+      char32_t c3 = (unsigned char) from[2];
       if ((c3 & 0xC0) != 0x80)
        return invalid_mb_sequence;
       char32_t c = (c1 << 12) + (c2 << 6) + c3 - 0xE2080;
@@ -296,17 +296,17 @@ namespace
     {
       if (avail < 4)
        return incomplete_mb_character;
-      unsigned char c2 = from[1];
+      char32_t c2 = (unsigned char) from[1];
       if ((c2 & 0xC0) != 0x80)
        return invalid_mb_sequence;
       if (c1 == 0xF0 && c2 < 0x90) // overlong
        return invalid_mb_sequence;
       if (c1 == 0xF4 && c2 >= 0x90) // > U+10FFFF
       return invalid_mb_sequence;
-      unsigned char c3 = from[2];
+      char32_t c3 = (unsigned char) from[2];
       if ((c3 & 0xC0) != 0x80)
        return invalid_mb_sequence;
-      unsigned char c4 = from[3];
+      char32_t c4 = (unsigned char) from[3];
       if ((c4 & 0xC0) != 0x80)
        return invalid_mb_sequence;
       char32_t c = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4 - 0x3C82080;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/104875] libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type
  2022-03-10 23:51 [Bug libstdc++/104875] New: libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type redi at gcc dot gnu.org
  2022-03-11  0:22 ` [Bug libstdc++/104875] " redi at gcc dot gnu.org
@ 2022-03-14 13:09 ` cvs-commit at gcc dot gnu.org
  2022-11-23 17:58 ` gjl at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-03-14 13:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 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:8f7b7c1495f92c72da154d32317943a2cc276ca8

commit r12-7643-g8f7b7c1495f92c72da154d32317943a2cc276ca8
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Mar 11 14:52:38 2022 +0000

    libstdc++: Fix reading UTF-8 characters for 16-bit targets [PR104875]

    The current code in read_utf8_code_point assumes that integer promotion
    will create a 32-bit int, but that's not true for 16-bit targets like
    msp430 and avr. This changes the intermediate variables used for each
    octet from unsigned char to char32_t, so that (c << N) works correctly
    when N > 8.

    libstdc++-v3/ChangeLog:

            PR libstdc++/104875
            * src/c++11/codecvt.cc (read_utf8_code_point): Use char32_t to
            hold octets that will be left-shifted.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/104875] libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type
  2022-03-10 23:51 [Bug libstdc++/104875] New: libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type redi at gcc dot gnu.org
  2022-03-11  0:22 ` [Bug libstdc++/104875] " redi at gcc dot gnu.org
  2022-03-14 13:09 ` cvs-commit at gcc dot gnu.org
@ 2022-11-23 17:58 ` gjl at gcc dot gnu.org
  2022-11-23 18:06 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: gjl at gcc dot gnu.org @ 2022-11-23 17:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Georg-Johann Lay <gjl at gcc dot gnu.org> ---
Is this fixed now?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/104875] libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type
  2022-03-10 23:51 [Bug libstdc++/104875] New: libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-11-23 17:58 ` gjl at gcc dot gnu.org
@ 2022-11-23 18:06 ` redi at gcc dot gnu.org
  2023-05-16 12:50 ` cvs-commit at gcc dot gnu.org
  2023-05-16 15:03 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-23 18:06 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Only for gcc-12 and trunk.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/104875] libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type
  2022-03-10 23:51 [Bug libstdc++/104875] New: libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type redi at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-11-23 18:06 ` redi at gcc dot gnu.org
@ 2023-05-16 12:50 ` cvs-commit at gcc dot gnu.org
  2023-05-16 15:03 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-05-16 12:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:e4b0d0b84b719ea9cd3d0a7b0668cdd8055a07d2

commit r11-10770-ge4b0d0b84b719ea9cd3d0a7b0668cdd8055a07d2
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Mar 11 14:52:38 2022 +0000

    libstdc++: Fix reading UTF-8 characters for 16-bit targets [PR104875]

    The current code in read_utf8_code_point assumes that integer promotion
    will create a 32-bit int, but that's not true for 16-bit targets like
    msp430 and avr. This changes the intermediate variables used for each
    octet from unsigned char to char32_t, so that (c << N) works correctly
    when N > 8.

    libstdc++-v3/ChangeLog:

            PR libstdc++/104875
            * src/c++11/codecvt.cc (read_utf8_code_point): Use char32_t to
            hold octets that will be left-shifted.

    (cherry picked from commit 8f7b7c1495f92c72da154d32317943a2cc276ca8)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/104875] libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type
  2022-03-10 23:51 [Bug libstdc++/104875] New: libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type redi at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-05-16 12:50 ` cvs-commit at gcc dot gnu.org
@ 2023-05-16 15:03 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2023-05-16 15:03 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
   Target Milestone|---                         |11.4
         Resolution|---                         |FIXED

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for 11.4 now as well. I'm not going to bother with gcc-10.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-05-16 15:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-10 23:51 [Bug libstdc++/104875] New: libstdc++-v3/src/c++11/codecvt.cc:312:24: warning: left shift count >= width of type redi at gcc dot gnu.org
2022-03-11  0:22 ` [Bug libstdc++/104875] " redi at gcc dot gnu.org
2022-03-14 13:09 ` cvs-commit at gcc dot gnu.org
2022-11-23 17:58 ` gjl at gcc dot gnu.org
2022-11-23 18:06 ` redi at gcc dot gnu.org
2023-05-16 12:50 ` cvs-commit at gcc dot gnu.org
2023-05-16 15:03 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).