diff mbox

Avoid invoking undefined behavior when initializing CRC table

Message ID 1451275797-648-1-git-send-email-patrick@parcs.ath.cx
State New
Headers show

Commit Message

Patrick Palka Dec. 28, 2015, 4:09 a.m. UTC
When I built GDB with (an older snapshot of) GCC 6 I get the following
error:

.../binutils-gdb/gdb/gdbserver/server.c: In function ‘crc32’:
.../binutils-gdb/gdb/gdbserver/server.c:1895:15: error: iteration 128 invokes undefined behavior [-Werror=aggressive-loop-optimizations]
    for (c = i << 24, j = 8; j > 0; --j)
               ^
.../binutils-gdb/gdb/gdbserver/server.c:1893:7: note: within this loop
       for (i = 0; i < 256; i++)
       ^
This error seems to be correct.  When the variable "int i" is >= 128,
the computation "i << 24" overflows for 32-bit signed int.

To avoid shifting into the sign bit, this patch makes the variables i
(and j, because why not) have type unsigned int instead.

(Alternatively, I can just define this local crc32 function in terms of
libiberty's xcrc32.  Any reason not to?  xcrc32 seems to be
based off of GDB's crc32 implementation.  Its documentation even
refers to it!)

gdb/gdbserver/ChangeLog:

	* server.c (crc32): Change type of induction variables i and j
	from int to unsigned int.
---
 gdb/gdbserver/server.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff mbox

Patch

diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index b385afb..70acafc 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -1927,7 +1927,7 @@  crc32 (CORE_ADDR base, int len, unsigned int crc)
   if (!crc32_table[1])
     {
       /* Initialize the CRC table and the decoding table.  */
-      int i, j;
+      unsigned int i, j;
       unsigned int c;
 
       for (i = 0; i < 256; i++)