This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH 0/2] Fix invalid left shift of negative value.
- From: "Ulrich Weigand" <uweigand at de dot ibm dot com>
- To: kevinb at redhat dot com (Kevin Buettner)
- Cc: gdb-patches at sourceware dot org
- Date: Wed, 11 Nov 2015 18:23:27 +0100 (CET)
- Subject: Re: [PATCH 0/2] Fix invalid left shift of negative value.
- Authentication-results: sourceware.org; auth=none
Kevin Büttner wrote:
> Looking at one of your changes from part 1/2...
>
> - (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
> + -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
>
> What aspect of the original expression is not defined by the C standard?
The C standard (either C99 or C11) says:
The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits
are filled with zeros. If E1 has an unsigned type, the value of the result
is E1 * 2^E2, reduced modulo one more than the maximum value representable
in the result type. If E1 has a signed type and nonnegative value, and
E1 * 2^E2 is representable in the result type, then that is the resulting
value; otherwise, the behavior is undefined.
Note the "otherwise" case includes any E1 of signed type and negative value.
(For >>, the behavior in the latter case is at least implementation-
defined, and not undefined.)
Bye,
Ulrich
--
Dr. Ulrich Weigand
GNU/Linux compilers and toolchain
Ulrich.Weigand@de.ibm.com