From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.devel Subject: Re: Removing some workarounds for big integers Date: Mon, 22 Apr 2019 11:45:12 -0700 Organization: UCLA Computer Science Department Message-ID: References: <86d0uck5o0.fsf@gmail.com> <15786884-92c4-efb0-58d3-a6ec6726a0fe@cs.ucla.edu> <86lg02our6.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------A27DEEABCECEFBC093CE1CB9" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="246504"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 Cc: Emacs developers To: Philipp Stephani , Andy Moreton Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Apr 22 20:46:13 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hIdxE-001237-TU for ged-emacs-devel@m.gmane.org; Mon, 22 Apr 2019 20:46:13 +0200 Original-Received: from localhost ([127.0.0.1]:41243 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hIdxD-0005PG-Uq for ged-emacs-devel@m.gmane.org; Mon, 22 Apr 2019 14:46:11 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:38728) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hIdwU-0005OZ-4r for emacs-devel@gnu.org; Mon, 22 Apr 2019 14:45:27 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hIdwS-0006gH-TJ for emacs-devel@gnu.org; Mon, 22 Apr 2019 14:45:26 -0400 Original-Received: from zimbra.cs.ucla.edu ([131.179.128.68]:57782) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hIdwS-0006dO-Cj for emacs-devel@gnu.org; Mon, 22 Apr 2019 14:45:24 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id F31A6161830; Mon, 22 Apr 2019 11:45:16 -0700 (PDT) Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 3qXaVoFqLeb1; Mon, 22 Apr 2019 11:45:15 -0700 (PDT) Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id C8822161831; Mon, 22 Apr 2019 11:45:15 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id V0DebTrMt1M6; Mon, 22 Apr 2019 11:45:15 -0700 (PDT) Original-Received: from Penguin.CS.UCLA.EDU (Penguin.CS.UCLA.EDU [131.179.64.200]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id A5669161816; Mon, 22 Apr 2019 11:45:15 -0700 (PDT) Openpgp: preference=signencrypt Autocrypt: addr=eggert@cs.ucla.edu; prefer-encrypt=mutual; keydata= xsFNBEyAcmQBEADAAyH2xoTu7ppG5D3a8FMZEon74dCvc4+q1XA2J2tBy2pwaTqfhpxxdGA9 Jj50UJ3PD4bSUEgN8tLZ0san47l5XTAFLi2456ciSl5m8sKaHlGdt9XmAAtmXqeZVIYX/UFS 96fDzf4xhEmm/y7LbYEPQdUdxu47xA5KhTYp5bltF3WYDz1Ygd7gx07Auwp7iw7eNvnoDTAl KAl8KYDZzbDNCQGEbpY3efZIvPdeI+FWQN4W+kghy+P6au6PrIIhYraeua7XDdb2LS1en3Ss mE3QjqfRqI/A2ue8JMwsvXe/WK38Ezs6x74iTaqI3AFH6ilAhDqpMnd/msSESNFt76DiO1ZK QMr9amVPknjfPmJISqdhgB1DlEdw34sROf6V8mZw0xfqT6PKE46LcFefzs0kbg4GORf8vjG2 Sf1tk5eU8MBiyN/bZ03bKNjNYMpODDQQwuP84kYLkX2wBxxMAhBxwbDVZudzxDZJ1C2VXujC OJVxq2kljBM9ETYuUGqd75AW2LXrLw6+MuIsHFAYAgRr7+KcwDgBAfwhPBYX34nSSiHlmLC+ KaHLeCLF5ZI2vKm3HEeCTtlOg7xZEONgwzL+fdKo+D6SoC8RRxJKs8a3sVfI4t6CnrQzvJbB n6gxdgCu5i29J1QCYrCYvql2UyFPAK+do99/1jOXT4m2836j1wARAQABzSBQYXVsIEVnZ2Vy dCA8ZWdnZXJ0QGNzLnVjbGEuZWR1PsLBfgQTAQIAKAUCTIByZAIbAwUJEswDAAYLCQgHAwIG FQgCCQoLBBYCAwECH In-Reply-To: Content-Language: en-US X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 131.179.128.68 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:235782 Archived-At: This is a multi-part message in MIME format. --------------A27DEEABCECEFBC093CE1CB9 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit On 4/22/19 9:59 AM, Philipp Stephani wrote: > >> +#define INTEGER_TO_INT(num, type) \ >> + (TYPE_SIGNED (type) \ >> + ? ranged_integer_to_int ((num), TYPE_MINIMUM (type), TYPE_MAXIMUM (type)) \ >> + : ranged_integer_to_uint ((num), TYPE_MINIMUM (type))) >> ^^^^^^^^^^^^ >> This should be TYPE_MAXIMUM. > > Thanks, fixed > More important, INTEGER_TO_INT's type conversion messes up and can cause a signal on picky platforms. I installed the attached patch to undo that change. Although lisp.lh really does need a better API for integer conversion (and I'll volunteer to review/help if someone wants to work on that!), INTEGER_TO_INT is not a step in the right direction; we need something more general/accurate/whatever. --------------A27DEEABCECEFBC093CE1CB9 Content-Type: text/x-patch; name="0001-Go-back-to-old-way-of-checking-json-int-range.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Go-back-to-old-way-of-checking-json-int-range.patch" >From c2dcf5d671142b2fc9ddb1142aa498a2d5aa46ec Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 22 Apr 2019 11:40:13 -0700 Subject: [PATCH] Go back to old way of checking json int range Although the lisp.h macros really need improvement, INTEGER_TO_INT is not the right way to go about it, as it causes conversion from intmax_t to uintmax_t and back again, which can cause a signal if the value is negative. * src/lisp.h (INTEGER_TO_INT, ranged_integer_to_int) (ranged_integer_to_uint): Remove, reverting recent changes to this file. * src/json.c (lisp_to_json): Revert to previous code, as the change messes up with uintmax_t<->intmax_t conversion. --- src/json.c | 9 ++++++++- src/lisp.h | 27 --------------------------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/src/json.c b/src/json.c index 16500bce72..256d485eea 100644 --- a/src/json.c +++ b/src/json.c @@ -495,7 +495,14 @@ lisp_to_json (Lisp_Object lisp, struct json_configuration *conf) else if (EQ (lisp, Qt)) return json_check (json_true ()); else if (INTEGERP (lisp)) - return json_check (json_integer (INTEGER_TO_INT (lisp, json_int_t))); + { + intmax_t low = TYPE_MINIMUM (json_int_t); + intmax_t high = TYPE_MAXIMUM (json_int_t); + intmax_t value; + if (! (integer_to_intmax (lisp, &value) && low <= value && value <= high)) + args_out_of_range_3 (lisp, make_int (low), make_int (high)); + return json_check (json_integer (value)); + } else if (FLOATP (lisp)) return json_check (json_real (XFLOAT_DATA (lisp))); else if (STRINGP (lisp)) diff --git a/src/lisp.h b/src/lisp.h index ee5a8481ae..d803f16000 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -2640,13 +2640,6 @@ make_uint (uintmax_t n) #define INT_TO_INTEGER(expr) \ (EXPR_SIGNED (expr) ? make_int (expr) : make_uint (expr)) -/* Return the integral value of NUM. If NUM is too big for TYPE, - signal an error. */ -#define INTEGER_TO_INT(num, type) \ - (TYPE_SIGNED (type) \ - ? ranged_integer_to_int ((num), TYPE_MINIMUM (type), TYPE_MAXIMUM (type)) \ - : ranged_integer_to_uint ((num), TYPE_MAXIMUM (type))) - /* Forwarding pointer to an int variable. This is allowed only in the value cell of a symbol, @@ -5023,26 +5016,6 @@ maybe_gc (void) garbage_collect (); } -INLINE intmax_t -ranged_integer_to_int (Lisp_Object num, intmax_t min, intmax_t max) -{ - CHECK_INTEGER (num); - intmax_t result; - if (!(integer_to_intmax (num, &result) && min <= result && result <= max)) - args_out_of_range_3 (num, make_int (min), make_int (max)); - return result; -} - -INLINE uintmax_t -ranged_integer_to_uint (Lisp_Object num, uintmax_t max) -{ - CHECK_INTEGER (num); - uintmax_t result; - if (!(integer_to_uintmax (num, &result) && result <= max)) - args_out_of_range_3 (num, make_fixed_natnum (0), make_uint (max)); - return result; -} - INLINE_HEADER_END #endif /* EMACS_LISP_H */ -- 2.20.1 --------------A27DEEABCECEFBC093CE1CB9--