From patchwork Mon Dec 28 04:09:57 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 10150 Received: (qmail 24766 invoked by alias); 28 Dec 2015 04:10:09 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 24755 invoked by uid 89); 28 Dec 2015 04:10:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy=induction, HX-HELO:sk:mail-qk, refers, H*r:sk:mail-qk X-HELO: mail-qk0-f169.google.com Received: from mail-qk0-f169.google.com (HELO mail-qk0-f169.google.com) (209.85.220.169) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Mon, 28 Dec 2015 04:10:07 +0000 Received: by mail-qk0-f169.google.com with SMTP id p187so191670590qkd.1 for ; Sun, 27 Dec 2015 20:10:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-type:content-transfer-encoding; bh=098vyLFLoKDurVhvg2WB+iF2rkLyrsVcKfpeprFiCGk=; b=B+BO2aWGb0lnUMwTIQnW3L69ySheEWHsQfHY2tx5U8XhO4GqZamtT2srepJ3vztG2y htLIs52kg+1M1gL9x3dq455vUiB1GBnyHDr+DPrrqniTEO7jkkCcyO1RLZ283lSFRWP/ 28WtXm8Ufw12ugWG6eRtGSTlOdKAZNwcCxfKAD+jENPM6sXfbLvv5hbTCyZ74y7JGMf9 RE61tgVuaMlDEoMF3lUY8OW6dOVqmRT4L6O1jcr/9hb80CAv7l7oAiL7FTZkJ1fT85ku bPPt5gCFPMOHHfLRdhwgIQuDmU4Q6nzjn5bCjKGP8tWtFWiuSsV/jCi6XA60FG/ei8YU W23w== X-Gm-Message-State: ALoCoQlNPE8sr7mDnybl3RayuXPgUXmkd6N+qyKLchzYV2h8Y6Kv/M5D8JE8xmhTdrg+qo7CvqEg4RCAAjc1KUzNnJmLiyyJZQ== X-Received: by 10.55.75.131 with SMTP id y125mr4762377qka.61.1451275805187; Sun, 27 Dec 2015 20:10:05 -0800 (PST) Received: from localhost.localdomain (ool-4353a8be.dyn.optonline.net. [67.83.168.190]) by smtp.gmail.com with ESMTPSA id c3sm26619831qge.12.2015.12.27.20.10.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 27 Dec 2015 20:10:04 -0800 (PST) From: Patrick Palka To: gdb-patches@sourceware.org Cc: Patrick Palka Subject: [PATCH] Avoid invoking undefined behavior when initializing CRC table Date: Sun, 27 Dec 2015 23:09:57 -0500 Message-Id: <1451275797-648-1-git-send-email-patrick@parcs.ath.cx> MIME-Version: 1.0 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 --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++)