Bug 66827 - [6 Regression] left shifts of negative value warnings due to C++14 switch
Summary: [6 Regression] left shifts of negative value warnings due to C++14 switch
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: other (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: 6.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: ubsan
  Show dependency treegraph
 
Reported: 2015-07-10 08:56 UTC by Markus Trippelsdorf
Modified: 2015-12-21 09:55 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Convert to unsigned before shifting (250 bytes, patch)
2015-12-18 11:46 UTC, Nick Clifton
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Trippelsdorf 2015-07-10 08:56:58 UTC
For C++98 -fsanitize=undefined didn't warn in case of left shifts of negative values.
Now that trunk switched to C++14 a number of new warnings popped up:

gcc/config/rs6000/rs6000.c:5398:36: runtime error: left shift of negative value -12301
gcc/config/rs6000/rs6000.md:3126:17: runtime error: left shift of negative value -1
gcc/dce.c:287:16: runtime error: ../../gcc/gcc/haifa-sched.c:1442:26:left shift of negative value -1
gcc/expmed.c:3002:15: runtime error: left shift of negative value -1
gcc/haifa-sched.c:1164:24: runtime error: left shift of negative value -1
gcc/haifa-sched.c:1442:26: runtime error: left shift of negative value -1
gcc/hwint.h:250:19: runtime error: left shift of negative value -1000013824
gcc/real.c:2861:25: runtime error: left shift of negative value -63
gcc/sched-deps.c:112:20: runtime error: left shift of negative value -1
gcc/sreal.c:231:20: runtime error: left shift of negative value -1092263868
libcpp/files.c:683:30: runtime error: left shift of negative value -1
libiberty/regex.c:6970:11: runtime error: left shift of negative value -1
libiberty/regex.c:7165:4: runtime error: left shift of negative value -1
Comment 1 Mikhail Maltsev 2015-07-28 09:27:33 UTC
gcc/haifa-sched.c:1164:24
gcc/haifa-sched.c:1442:26
gcc/sched-deps.c:112:20

are caused by the following macro definition in gcc/sched-int.h:243:
#define UNKNOWN_DEP_COST (-1<<19)
Comment 2 Vittorio Zecca 2015-08-23 20:42:28 UTC
The warnings in regex.c are already described in issue 64920
Comment 3 Vittorio Zecca 2015-09-17 19:29:22 UTC
(In reply to Mikhail Maltsev from comment #1)
> gcc/haifa-sched.c:1164:24
> gcc/haifa-sched.c:1442:26
> gcc/sched-deps.c:112:20
> 
> are caused by the following macro definition in gcc/sched-int.h:243:
> #define UNKNOWN_DEP_COST (-1<<19)

I fixed this one with

#define UNKNOWN_DEP_COST (-1u<<19)
Comment 4 Paolo Bonzini 2015-11-18 16:48:00 UTC
Using

#define UNKNOWN_DEP_COST (-1u<<19)

is dangerous in case UNKNOWN_DEP_COST is later assigned to a long.  Do not do this please.
Comment 5 Vittorio Zecca 2015-11-19 00:55:03 UTC
I am traveling now so I cannot double check your hint.
What do you suggest?
Comment 6 Paolo Bonzini 2015-11-19 09:49:22 UTC
If you really want to fix it, (-(1 << 19)) is the best.

The real fix would be to lobby the C/C++ committees so that left shift of a negative value is unspecified behavior rather than undefined.
Comment 7 Jakub Jelinek 2015-12-15 13:17:37 UTC
I believe UNKNOWN_DEP_COST stuff has been fixed in the meantime.  The question is about the others, but it has been so long that the locations are all useless.
Comment 8 Nick Clifton 2015-12-18 11:46:39 UTC
Created attachment 37079 [details]
Convert to unsigned before shifting

(In reply to Jakub Jelinek from comment #7)
> The question is about the others, but it has been so long that the 
> locations are all useless.

I have just checked.  The only problems that still remain are in libiberty/regex.c.

The uploaded patch fixes this problem.  I plan to submit it for review after running a few more regression tests.
Comment 9 Nick Clifton 2015-12-21 08:24:07 UTC
Author: nickc
Date: Mon Dec 21 08:23:35 2015
New Revision: 231873

URL: https://gcc.gnu.org/viewcvs?rev=231873&root=gcc&view=rev
Log:
	PR 66827
	* regex.c (EXTRACT_NUMBER): Cast sign byte to unsigned before left
	shifting.

Modified:
    trunk/libiberty/ChangeLog
    trunk/libiberty/regex.c
Comment 10 Jakub Jelinek 2015-12-21 09:55:55 UTC
Fixed.