From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.devel Subject: Re: Undefined behavior in conv-integer.i.c? Date: Mon, 20 Jun 2016 16:07:00 -0400 Message-ID: <871t3rtyjf.fsf@netris.org> References: <20160217161639.GE6131@localhost> <874mcysf1v.fsf@netris.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1466453296 29348 80.91.229.3 (20 Jun 2016 20:08:16 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 20 Jun 2016 20:08:16 +0000 (UTC) Cc: guile-devel@gnu.org To: Miroslav Lichvar Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Mon Jun 20 22:08:09 2016 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1bF5UO-0005i9-R9 for guile-devel@m.gmane.org; Mon, 20 Jun 2016 22:08:08 +0200 Original-Received: from localhost ([::1]:46197 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bF5UO-0001ae-2J for guile-devel@m.gmane.org; Mon, 20 Jun 2016 16:08:08 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40563) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bF5TZ-0000jk-B5 for guile-devel@gnu.org; Mon, 20 Jun 2016 16:07:18 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bF5TU-0001QZ-Cp for guile-devel@gnu.org; Mon, 20 Jun 2016 16:07:17 -0400 Original-Received: from world.peace.net ([50.252.239.5]:44523) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bF5TU-0001QU-8X for guile-devel@gnu.org; Mon, 20 Jun 2016 16:07:12 -0400 Original-Received: from c-73-253-48-168.hsd1.ma.comcast.net ([73.253.48.168] helo=jojen) by world.peace.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1bF5TT-0000Tb-FI; Mon, 20 Jun 2016 16:07:11 -0400 In-Reply-To: <874mcysf1v.fsf@netris.org> (Mark H. Weaver's message of "Wed, 24 Feb 2016 03:11:56 -0500") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.95 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 50.252.239.5 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.org gmane.lisp.guile.devel:18375 Archived-At: Mark H Weaver writes: > Miroslav Lichvar writes: > >> I was looking at a problem with guile-1.8.8 when compiled with >> gcc-6.0. Two of the tests from the test suite were failing with >> strange "out of range" errors [1]. After some investigation I think >> the bug is that the code in libguile/conv-integer.i.c relies on >> overflow of signed integers in the following code (starting on line >> 77), specifically -TYPE_MIN being less than zero. Adding -fwrapv to >> CFLAGS worked as a workaround for me. >> >> if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0) >> { >> if (n < 0) >> goto out_of_range; >> } >> else >> { >> n = -n; >> if (n >= 0) >> goto out_of_range; >> } > > Thanks for bringing this to our attention. I've attached a preliminary > patch to address these issues on the 'stable-2.0' branch. > >> Looking at the current guile code, conv-integer.i.c is identical to >> what it was in 1.8.8, but interestingly the tests didn't fail for me. >> Maybe something else is preventing gcc from using the optimization? > > The build system of recent Guile 2.0.x automatically adds -fwrapv to > CFLAGS where supported. However, I hope to remove -fwrapv in the > future, when we gain confidence that no code in Guile depends on it. > >> I'm not sure what would be the best way to fix it. Maybe n should >> really be unsigned and compared to the maximum values, but what would >> be the absolute value of TYPE_MIN if it should work also with other >> integer representations than two's complement? > > My approach was to compare (abs_n - 1) to -(TYPE_MIN + 1) in the case > where n is negative. I pushed these fixes as commit 4b60562820d001674ec7124c4a10391ecf7e44c3 to the stable-2.0 branch, which will become guile-2.0.12. Thanks, Mark