From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: lloda Newsgroups: gmane.lisp.guile.bugs Subject: bug#50609: number overflow Date: Thu, 4 Nov 2021 20:12:10 +0100 Message-ID: References: Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_E61CEFE1-69F8-4DCA-AC02-3AED80524B90" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3566"; mail-complaints-to="usenet@ciao.gmane.io" To: 50609@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Thu Nov 04 20:13:08 2021 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1miiAe-0000iK-AQ for guile-bugs@m.gmane-mx.org; Thu, 04 Nov 2021 20:13:08 +0100 Original-Received: from localhost ([::1]:45318 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1miiAc-0003dH-Sj for guile-bugs@m.gmane-mx.org; Thu, 04 Nov 2021 15:13:06 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43768) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1miiAY-0003co-Ba for bug-guile@gnu.org; Thu, 04 Nov 2021 15:13:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:60289) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1miiAY-0008Hf-2g for bug-guile@gnu.org; Thu, 04 Nov 2021 15:13:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1miiAX-0006XU-RJ for bug-guile@gnu.org; Thu, 04 Nov 2021 15:13:01 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: Resent-From: lloda Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Thu, 04 Nov 2021 19:13:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 50609 X-GNU-PR-Package: guile Original-Received: via spool by 50609-submit@debbugs.gnu.org id=B50609.163605314225089 (code B ref 50609); Thu, 04 Nov 2021 19:13:01 +0000 Original-Received: (at 50609) by debbugs.gnu.org; 4 Nov 2021 19:12:22 +0000 Original-Received: from localhost ([127.0.0.1]:43602 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mii9u-0006Wa-40 for submit@debbugs.gnu.org; Thu, 04 Nov 2021 15:12:22 -0400 Original-Received: from mta-10-3.privateemail.com ([198.54.127.62]:39850) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mii9s-0006WK-2V for 50609@debbugs.gnu.org; Thu, 04 Nov 2021 15:12:20 -0400 Original-Received: from mta-10.privateemail.com (localhost [127.0.0.1]) by mta-10.privateemail.com (Postfix) with ESMTP id 0FBD3180009E for <50609@debbugs.gnu.org>; Thu, 4 Nov 2021 15:12:14 -0400 (EDT) Original-Received: from [192.168.1.105] (unknown [10.20.151.235]) by mta-10.privateemail.com (Postfix) with ESMTPA id 4CA7518000A4 for <50609@debbugs.gnu.org>; Thu, 4 Nov 2021 15:12:13 -0400 (EDT) X-Mailer: Apple Mail (2.3608.120.23.2.7) X-Virus-Scanned: ClamAV using ClamSMTP X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.io gmane.lisp.guile.bugs:10207 Archived-At: --Apple-Mail=_E61CEFE1-69F8-4DCA-AC02-3AED80524B90 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Not quite comfortable poking around in the compiler, but find a patch = attached. I also found this related bug: = https://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D32644. --Apple-Mail=_E61CEFE1-69F8-4DCA-AC02-3AED80524B90 Content-Disposition: attachment; filename=0001-Limit-the-range-of-ash-round-ash-count-argument-to-I.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="0001-Limit-the-range-of-ash-round-ash-count-argument-to-I.patch" Content-Transfer-Encoding: quoted-printable =46rom=20699919f181ea94ba00d392b49d07ed85e7535fcc=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Daniel=20Llorens=20=0ADate:=20= Thu,=204=20Nov=202021=2014:52:21=20+0100=0ASubject:=20[PATCH=201/2]=20= Limit=20the=20range=20of=20ash,=20round-ash=20count=20argument=20to=20= INT32=0A=0AThis=20avoids=20gmp=20aborting=20e.g.=20with=20(ash=201=20= (expt=202=2037)).=20The=20new=20limit=20is=0Asuch=20that=20(ash=201=20= (expt=2030))=20is=20accepted=20but=20(ash=201=20(expt=2031))=20throws.=0A= =0A*=20libguile/numbers.c=20(ash,=20round-ash):=20As=20stated.=0A*=20= test-suite/tests/numbers.test:=20Test=20a=20case=20known=20to=20make=20= gmp=20abort=20before.=0A---=0A=20libguile/numbers.c=20=20=20=20=20=20=20=20= =20=20=20=20|=2098=20+++++++++++++++--------------------=0A=20= test-suite/tests/numbers.test=20|=20=205=20+-=0A=202=20files=20changed,=20= 47=20insertions(+),=2056=20deletions(-)=0A=0Adiff=20--git=20= a/libguile/numbers.c=20b/libguile/numbers.c=0Aindex=20= 18bd22dbb..1f7785aa6=20100644=0A---=20a/libguile/numbers.c=0A+++=20= b/libguile/numbers.c=0A@@=20-5090,6=20+5090,8=20@@=20= round_right_shift_exact_integer=20(SCM=20n,=20long=20count)=0A=20=20=20=20= and=20moreover=20that=20they=20can=20be=20negated=20without=20overflow.=20= */=0A=20verify=20(SCM_MOST_NEGATIVE_FIXNUM=20>=3D=20LONG_MIN=20+=201=0A=20= =20=20=20=20=20=20=20=20&&=20SCM_MOST_POSITIVE_FIXNUM=20<=3D=20= LONG_MAX);=0A+/*=20the=20practicable=20limits=20are=20smaller=20*/=0A= +verify=20(INT32_MIN=20>=3D=20LONG_MIN=20&&=20INT32_MAX=20<=3D=20= LONG_MAX);=0A=20=0A=20SCM_DEFINE=20(scm_ash,=20"ash",=202,=200,=200,=0A=20= =20=20=20=20=20=20=20=20=20=20=20=20(SCM=20n,=20SCM=20count),=0A@@=20= -5110,35=20+5112,28=20@@=20SCM_DEFINE=20(scm_ash,=20"ash",=202,=200,=20= 0,=0A=20=09=20=20=20=20"@end=20lisp")=0A=20#define=20FUNC_NAME=20= s_scm_ash=0A=20{=0A-=20=20if=20(SCM_I_INUMP=20(n)=20||=20SCM_BIGP=20(n))=0A= -=20=20=20=20{=0A-=20=20=20=20=20=20long=20bits_to_shift;=0A-=0A-=20=20=20= =20=20=20if=20(SCM_I_INUMP=20(count))=20=20/*=20fast=20path,=20not=20= strictly=20needed=20*/=0A-=20=20=20=20=20=20=20=20bits_to_shift=20=3D=20= SCM_I_INUM=20(count);=0A-=20=20=20=20=20=20else=20if=20= (scm_is_signed_integer=20(count,=20LONG_MIN=20+=201,=20LONG_MAX))=0A-=20=20= =20=20=20=20=20=20/*=20We=20exclude=20LONG_MIN=20to=20ensure=20that=20= 'bits_to_shift'=20can=20be=0A-=20=20=20=20=20=20=20=20=20=20=20negated=20= without=20overflowing.=20*/=0A-=20=20=20=20=20=20=20=20bits_to_shift=20=3D= =20scm_to_long=20(count);=0A-=20=20=20=20=20=20else=20if=20(scm_is_false=20= (scm_positive_p=20(scm_sum=20(scm_integer_length=20(n),=0A-=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= count))))=0A-=20=20=20=20=20=20=20=20/*=20Huge=20right=20shift=20that=20= eliminates=20all=20but=20the=20sign=20bit=20*/=0A-=20=20=20=20=20=20=20=20= return=20scm_is_false=20(scm_negative_p=20(n))=0A-=20=20=20=20=20=20=20=20= =20=20?=20SCM_INUM0=20:=20SCM_I_MAKINUM=20(-1);=0A-=20=20=20=20=20=20= else=20if=20(scm_is_true=20(scm_zero_p=20(n)))=0A-=20=20=20=20=20=20=20=20= return=20SCM_INUM0;=0A-=20=20=20=20=20=20else=0A-=20=20=20=20=20=20=20=20= scm_num_overflow=20("ash");=0A-=0A-=20=20=20=20=20=20if=20(bits_to_shift=20= >=200)=0A-=20=20=20=20=20=20=20=20return=20left_shift_exact_integer=20= (n,=20bits_to_shift);=0A-=20=20=20=20=20=20else=20if=20(SCM_LIKELY=20= (bits_to_shift=20<=200))=0A-=20=20=20=20=20=20=20=20return=20= floor_right_shift_exact_integer=20(n,=20-bits_to_shift);=0A-=20=20=20=20=20= =20else=0A-=20=20=20=20=20=20=20=20return=20n;=0A-=20=20=20=20}=0A-=20=20= else=0A+=20=20if=20(!SCM_I_INUMP=20(n)=20&&=20!SCM_BIGP=20(n))=0A=20=20=20= =20=20SCM_WRONG_TYPE_ARG=20(SCM_ARG1,=20n);=0A+=20=20=0A+=20=20if=20= (scm_is_false=20(scm_positive_p=20(scm_sum=20(scm_integer_length=20(n),=0A= +=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20count))))=0A= +=20=20=20=20/*=20Huge=20right=20shift=20that=20eliminates=20all=20but=20= the=20sign=20bit=20*/=0A+=20=20=20=20return=20scm_is_false=20= (scm_negative_p=20(n))=0A+=20=20=20=20=20=20?=20SCM_INUM0=20:=20= SCM_I_MAKINUM=20(-1);=0A+=20=20else=20if=20(scm_is_true=20(scm_zero_p=20= (n)))=0A+=20=20=20=20return=20SCM_INUM0;=0A+=20=20else=20if=20= (scm_is_signed_integer=20(count,=20INT32_MIN=20+=201,=20INT32_MAX))=20{=0A= +=20=20=20=20/*=20We=20exclude=20MIN=20to=20ensure=20that=20= 'bits_to_shift'=20can=20be=0A+=20=20=20=20=20=20=20negated=20without=20= overflowing,=20if=20INT32_MIN=20happens=20to=20be=20LONG_MIN=20*/=0A+=20=20= =20=20long=20bits_to_shift=20=3D=20scm_to_long=20(count);=0A+=20=20=20=20= if=20(bits_to_shift=20>=200)=0A+=20=20=20=20=20=20return=20= left_shift_exact_integer=20(n,=20bits_to_shift);=0A+=20=20=20=20else=20= if=20(SCM_LIKELY=20(bits_to_shift=20<=200))=0A+=20=20=20=20=20=20return=20= floor_right_shift_exact_integer=20(n,=20-bits_to_shift);=0A+=20=20=20=20= else=0A+=20=20=20=20=20=20return=20n;=0A+=20=20}=20else=0A+=20=20=20=20= scm_num_overflow=20("ash");=0A=20}=0A=20#undef=20FUNC_NAME=0A=20=0A@@=20= -5164,34=20+5159,27=20@@=20SCM_DEFINE=20(scm_round_ash,=20"round-ash",=20= 2,=200,=200,=0A=20=09=20=20=20=20"@end=20lisp")=0A=20#define=20FUNC_NAME=20= s_scm_round_ash=0A=20{=0A-=20=20if=20(SCM_I_INUMP=20(n)=20||=20SCM_BIGP=20= (n))=0A-=20=20=20=20{=0A-=20=20=20=20=20=20long=20bits_to_shift;=0A-=0A-=20= =20=20=20=20=20if=20(SCM_I_INUMP=20(count))=20=20/*=20fast=20path,=20not=20= strictly=20needed=20*/=0A-=20=20=20=20=20=20=20=20bits_to_shift=20=3D=20= SCM_I_INUM=20(count);=0A-=20=20=20=20=20=20else=20if=20= (scm_is_signed_integer=20(count,=20LONG_MIN=20+=201,=20LONG_MAX))=0A-=20=20= =20=20=20=20=20=20/*=20We=20exclude=20LONG_MIN=20to=20ensure=20that=20= 'bits_to_shift'=20can=20be=0A-=20=20=20=20=20=20=20=20=20=20=20negated=20= without=20overflowing.=20*/=0A-=20=20=20=20=20=20=20=20bits_to_shift=20=3D= =20scm_to_long=20(count);=0A-=20=20=20=20=20=20else=20if=20(scm_is_true=20= (scm_negative_p=20(scm_sum=20(scm_integer_length=20(n),=0A-=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= count)))=0A-=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20||=20= scm_is_true=20(scm_zero_p=20(n)))=0A-=20=20=20=20=20=20=20=20/*=20If=20N=20= is=20zero,=20or=20the=20right=20shift=20count=20exceeds=20the=20integer=0A= -=20=20=20=20=20=20=20=20=20=20=20length,=20the=20result=20is=20zero.=20= */=0A-=20=20=20=20=20=20=20=20return=20SCM_INUM0;=0A-=20=20=20=20=20=20= else=0A-=20=20=20=20=20=20=20=20scm_num_overflow=20("round-ash");=0A-=0A= -=20=20=20=20=20=20if=20(bits_to_shift=20>=200)=0A-=20=20=20=20=20=20=20=20= return=20left_shift_exact_integer=20(n,=20bits_to_shift);=0A-=20=20=20=20= =20=20else=20if=20(SCM_LIKELY=20(bits_to_shift=20<=200))=0A-=20=20=20=20=20= =20=20=20return=20round_right_shift_exact_integer=20(n,=20= -bits_to_shift);=0A-=20=20=20=20=20=20else=0A-=20=20=20=20=20=20=20=20= return=20n;=0A-=20=20=20=20}=0A-=20=20else=0A+=20=20if=20(!SCM_I_INUMP=20= (n)=20&&=20!SCM_BIGP=20(n))=0A=20=20=20=20=20SCM_WRONG_TYPE_ARG=20= (SCM_ARG1,=20n);=0A+=20=20=0A+=20=20if=20(scm_is_true=20(scm_negative_p=20= (scm_sum=20(scm_integer_length=20(n),=0A+=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20count)))=0A+=20=20=20=20=20=20||=20scm_is_true=20= (scm_zero_p=20(n)))=0A+=20=20=20=20/*=20If=20N=20is=20zero,=20or=20the=20= right=20shift=20count=20exceeds=20the=20integer=0A+=20=20=20=20=20=20=20= length,=20the=20result=20is=20zero.=20*/=0A+=20=20=20=20return=20= SCM_INUM0;=0A+=20=20else=20if=20(scm_is_signed_integer=20(count,=20= INT32_MIN=20+=201,=20INT32_MAX))=20{=0A+=20=20=20=20/*=20We=20exclude=20= MIN=20to=20ensure=20that=20'bits_to_shift'=20can=20be=0A+=20=20=20=20=20=20= =20negated=20without=20overflowing,=20if=20INT32_MIN=20happens=20to=20be=20= LONG_MIN=20*/=0A+=20=20=20=20long=20bits_to_shift=20=3D=20scm_to_long=20= (count);=0A+=20=20=20=20if=20(bits_to_shift=20>=200)=0A+=20=20=20=20=20=20= return=20left_shift_exact_integer=20(n,=20bits_to_shift);=0A+=20=20=20=20= else=20if=20(SCM_LIKELY=20(bits_to_shift=20<=200))=0A+=20=20=20=20=20=20= return=20round_right_shift_exact_integer=20(n,=20-bits_to_shift);=0A+=20=20= =20=20else=0A+=20=20=20=20=20=20return=20n;=0A+=20=20}=20else=20=0A+=20=20= =20=20scm_num_overflow=20("round-ash");=0A=20}=0A=20#undef=20FUNC_NAME=0A= =20=0Adiff=20--git=20a/test-suite/tests/numbers.test=20= b/test-suite/tests/numbers.test=0Aindex=208f644874d..51263f0ac=20100644=0A= ---=20a/test-suite/tests/numbers.test=0A+++=20= b/test-suite/tests/numbers.test=0A@@=20-5466,7=20+5466,10=20@@=0A=20=20=20= =20=20=20=20=20=20(ash-variant=20-1=20(-=20(expt=202=201000))))=0A=20=20=20= =20=20=20=20(pass-if-exception=20"Huge=20left=20shift=20of=20non-zero=20= =3D>=20numerical=20overflow"=0A=20=20=20=20=20=20=20=20=20=20=20= exception:numerical-overflow=0A-=20=20=20=20=20=20=20=20(ash-variant=20= 123=20(expt=202=201000)))))=0A+=20=20=20=20=20=20=20=20(ash-variant=20= 123=20(expt=202=201000)))=0A+=20=20=20=20=20=20(pass-if-exception=20= "Shift=20large=20enough=20to=20cause=20gmp=20abort=20in=203.0.7"=0A+=20=20= =20=20=20=20=20=20=20=20exception:numerical-overflow=0A+=20=20=20=20=20=20= =20=20(ash-variant=201=20(expt=202=2037)))))=0A=20=0A=20=20=20= (test-ash-variant=20=20=20=20=20=20=20'ash=20=20=20=20=20=20=20ash=20= floor=20#f)=0A=20=20=20(test-ash-variant=20'round-ash=20round-ash=20= round=20#t)=0A--=20=0A2.33.1=0A=0A= --Apple-Mail=_E61CEFE1-69F8-4DCA-AC02-3AED80524B90 Content-Disposition: attachment; filename=0002-Avoid-ash-with-arguments-that-might-overflow-in-lang.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="0002-Avoid-ash-with-arguments-that-might-overflow-in-lang.patch" Content-Transfer-Encoding: quoted-printable =46rom=2047f99500e68e40f34a1dab8b0350f32c2ed62d9d=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Daniel=20Llorens=20=0ADate:=20= Thu,=204=20Nov=202021=2016:02:42=20+0100=0ASubject:=20[PATCH=202/2]=20= Avoid=20ash=20with=20arguments=20that=20might=20overflow=20in=20= (language=0A=20cps=20types)=0A=0AFixes=20https://debbugs.gnu.org/50609=0A= =0A*=20module/languages/cps/types.scm=20(ulsh):=20As=20stated.=0A---=0A=20= module/language/cps/types.scm=20|=204=20+++-=0A=201=20file=20changed,=20= 3=20insertions(+),=201=20deletion(-)=0A=0Adiff=20--git=20= a/module/language/cps/types.scm=20b/module/language/cps/types.scm=0A= index=20d3be176bf..87c58d5bc=20100644=0A---=20= a/module/language/cps/types.scm=0A+++=20b/module/language/cps/types.scm=0A= @@=20-1444,7=20+1444,9=20@@=20minimum,=20and=20maximum."=0A=20=20=20=20=20= =20=20=20=20(define!=20result=20&s64=20&s64-min=20&s64-max))))=0A=20=0A=20= (define-type-inferrer=20(ulsh=20a=20b=20result)=0A-=20=20(if=20(<=3D=20= (ash=20(&max/u64=20a)=20(&max/u64=20b))=20&u64-max)=0A+=20=20(if=20(and=0A= +=20=20=20=20=20=20=20(or=20(zero?=20(&max/u64=20a))=20(<=20(&max/u64=20= b)=2064))=20;=20don't=20even=20try=0A+=20=20=20=20=20=20=20(<=3D=20(ash=20= (&max/u64=20a)=20(&max/u64=20b))=20&u64-max))=0A=20=20=20=20=20=20=20;;=20= No=20overflow;=20we=20can=20be=20precise.=0A=20=20=20=20=20=20=20= (define!=20result=20&u64=0A=20=20=20=20=20=20=20=20=20(ash=20(&min/0=20= a)=20(&min/0=20b))=0A--=20=0A2.33.1=0A=0A= --Apple-Mail=_E61CEFE1-69F8-4DCA-AC02-3AED80524B90 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii --Apple-Mail=_E61CEFE1-69F8-4DCA-AC02-3AED80524B90--