From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.bugs Subject: bug#8545: issues with recent doprnt-related changes Date: Tue, 03 May 2011 13:24:16 -0700 Organization: UCLA Computer Science Department Message-ID: <4DC06470.4050902@cs.ucla.edu> References: <4DB50AB9.6060100@cs.ucla.edu> <83tydmaeo3.fsf@gnu.org> <4DB65FF1.5010003@cs.ucla.edu> <83aafb8p4a.fsf@gnu.org> <4DB8ABEA.3080503@cs.ucla.edu> <4DB8DAF8.7070408@cs.ucla.edu> <4DBB4E80.2020102@cs.ucla.edu> <4DBCF292.4030002@cs.ucla.edu> <4DBDF989.8070500@cs.ucla.edu> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1304455027 2513 80.91.229.12 (3 May 2011 20:37:07 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 3 May 2011 20:37:07 +0000 (UTC) Cc: 8545@debbugs.gnu.org To: rms@gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue May 03 22:37:03 2011 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1QHML6-0001Fw-EU for geb-bug-gnu-emacs@m.gmane.org; Tue, 03 May 2011 22:37:00 +0200 Original-Received: from localhost ([::1]:41510 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QHML5-0001fS-VQ for geb-bug-gnu-emacs@m.gmane.org; Tue, 03 May 2011 16:36:59 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:44015) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QHML1-0001c4-Lp for bug-gnu-emacs@gnu.org; Tue, 03 May 2011 16:36:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QHML0-0000Ht-G1 for bug-gnu-emacs@gnu.org; Tue, 03 May 2011 16:36:55 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:50263) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QHML0-0000Hp-DJ for bug-gnu-emacs@gnu.org; Tue, 03 May 2011 16:36:54 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.69) (envelope-from ) id 1QHM9V-0004gR-Ul; Tue, 03 May 2011 16:25:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Paul Eggert Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-To: owner@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 03 May 2011 20:25:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 8545 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 8545-submit@debbugs.gnu.org id=B8545.130445426717963 (code B ref 8545); Tue, 03 May 2011 20:25:01 +0000 Original-Received: (at 8545) by debbugs.gnu.org; 3 May 2011 20:24:27 +0000 Original-Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1QHM8w-0004fg-Oy for submit@debbugs.gnu.org; Tue, 03 May 2011 16:24:26 -0400 Original-Received: from smtp.cs.ucla.edu ([131.179.128.62]) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1QHM8t-0004fU-LR for 8545@debbugs.gnu.org; Tue, 03 May 2011 16:24:24 -0400 Original-Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp.cs.ucla.edu (Postfix) with ESMTP id 2B9AF39E80F2; Tue, 3 May 2011 13:24:18 -0700 (PDT) X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu Original-Received: from smtp.cs.ucla.edu ([127.0.0.1]) by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id p7dyAHXGbCFv; Tue, 3 May 2011 13:24:16 -0700 (PDT) Original-Received: from [131.179.64.200] (Penguin.CS.UCLA.EDU [131.179.64.200]) by smtp.cs.ucla.edu (Postfix) with ESMTPSA id C704B39E80B1; Tue, 3 May 2011 13:24:16 -0700 (PDT) User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.15) Gecko/20110421 Fedora/3.1.9-2.fc14 Thunderbird/3.1.9 In-Reply-To: X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.11 Precedence: list Resent-Date: Tue, 03 May 2011 16:25:01 -0400 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:46170 Archived-At: >> There are similar reliable tests for the other arithmetic operations. > > Is this documented somewhere? Is there a list of the standard ways? CERT has something, here: https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow Although the principles in that memo are OK, the actual code is hard to read and its multiplication overflow checking is buggy. Here's something better, which I just now wrote. Also, please see Emacs Bug#8611 ; its patch uses code like the following. #include int add_overflow (int a, int b) { return (b < 0 ? a < INT_MIN - b : INT_MAX - b < a); } int subtract_overflow (int a, int b) { return (b < 0 ? INT_MAX + b < a : a < INT_MIN + b); } int unary_minus_overflow (int a) { return a < -INT_MAX; } int multiply_overflow (int a, int b) { return (b < 0 ? (a < 0 ? a < INT_MAX / b : b != -1 && INT_MIN / b < a) : (b != 0 && (a < 0 ? a < INT_MIN / b : INT_MAX / b < a))); } int quotient_overflow (int a, int b) { /* This does not check for division by zero. Add that if you like. */ return a < -INT_MAX && b == -1; } int remainder_overflow (int a, int b) { /* Mathematically the remainder should never overflow, but on x86-like hosts INT_MIN % -1 traps, and the C standard permits this. */ return quotient_overflow (a, b); }