>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