bug-gnulib
[Top][All Lists]
Advanced

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

Re: [bug-gawk] [PATCH] Avoid left-shifting a negative value (by a positi


From: arnold
Subject: Re: [bug-gawk] [PATCH] Avoid left-shifting a negative value (by a positive value)
Date: Thu, 16 Aug 2018 00:41:45 -0600
User-agent: Heirloom mailx 12.4 7/29/08

Hi.

Thanks for the note.  The file under discussion came from GNULIB (I
believe) so I'm adding bug-gnulib and will let that team comment on
this.

Given that it's not an issue on commonly used CPUs, I don't see this
as a high priority issue either way.

Thanks,

Arnold

Samy Mahmoudi <address@hidden> wrote:

> 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

-------------------------------
diff --git a/missing_d/mktime.c b/missing_d/mktime.c
index d394ef17..16a944d3 100644
--- a/missing_d/mktime.c
+++ b/missing_d/mktime.c
@@ -79,7 +79,7 @@

 #ifndef TIME_T_MIN
 #define TIME_T_MIN (0 < (time_t) -1 ? (time_t) 0 \
-                   : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
+                   : (time_t) (intmax_t) ~ ((uintmax_t) ~ (time_t) 0 >> 1))
 #endif
 #ifndef TIME_T_MAX
 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)



reply via email to

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