bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-gawk] [PATCH] Avoid left-shifting a negative value (by a positive v


From: Samy Mahmoudi
Subject: [bug-gawk] [PATCH] Avoid left-shifting a negative value (by a positive value)
Date: Wed, 15 Aug 2018 21:16:17 +0200

Hello,

Compiling with the option -Wshift-negative-value outputs the following warning:

missing_d/mktime.c:82:22: warning: left shift of negative value [-Wshift-negative-value]
       : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))

In relation to gcc PR c/65179, Martin Sebor wrote:

"Shifting a negative value by a positive number of bits does have a natural meaning (i.e., shifting the bit pattern the same way as unsigned). The reason why it's undefined in C and C++ is because some processors don't shift the sign bit out and may raise an overflow when a one bit is shifted into the sign position (typically those that provide an arithmetic left shift). But most processors implement a logical left shift and behave the same way for signed operands as for unsigned. The result of a left shift of a negative number computed by GCC matches that of hardware that doesn't differentiate between arithmetic and logical left shifts (which is all the common CPUs, including ARM, MIPS, PowerPC, x86), so the only value in diagnosing it is portability to rare CPUs or to compilers that behave differently than GCC (if there are any)."

On most platforms, the attached patch does not provide any functional change.

Besides, do you think using intmax_t and uintmax_t could results in a portability loss ?

Best regards,
Samy Mahmoudi

Attachment: git-diff.diff
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]