From bec5cfee7660f6e283efbd30a693a6f8e9ea46b8 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 5 Apr 2020 01:17:32 -0700 Subject: Improve integer range checking * src/bignum.c (check_integer_range, check_uinteger_max) (check_int_nonnegative): New functions. * src/frame.c (check_frame_pixels): New function. (Fset_frame_height, Fset_frame_width, Fset_frame_size): Use it. * src/lisp.h (CHECK_RANGED_INTEGER, CHECK_TYPE_RANGED_INTEGER): Remove these macros. Unless otherwise specified, all callers replaced by calls to check_integer_range, check_uinteger_range, check_int_nonnegative. * src/frame.c (gui_set_right_divider_width) (gui_set_bottom_divider_width): * src/nsfns.m (ns_set_internal_border_width): * src/xfns.c (x_set_internal_border_width): Using check_int_nonnegative means these functions no longer incorrectly reject negative bignums; they treat them as 0, just like negative fixnums. --- src/coding.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'src/coding.c') diff --git a/src/coding.c b/src/coding.c index 0bea2a0c2bc..f0fc37dbdfa 100644 --- a/src/coding.c +++ b/src/coding.c @@ -11061,10 +11061,8 @@ usage: (define-coding-system-internal ...) */) else { CHECK_CONS (val); - CHECK_RANGED_INTEGER (XCAR (val), 0, 255); - from = XFIXNUM (XCAR (val)); - CHECK_RANGED_INTEGER (XCDR (val), from, 255); - to = XFIXNUM (XCDR (val)); + from = check_integer_range (XCAR (val), 0, 255); + to = check_integer_range (XCDR (val), from, 255); } for (int i = from; i <= to; i++) SSET (valids, i, 1); @@ -11149,7 +11147,7 @@ usage: (define-coding-system-internal ...) */) val = XCAR (tail); CHECK_CONS (val); CHECK_CHARSET_GET_ID (XCAR (val), id); - CHECK_RANGED_INTEGER (XCDR (val), 0, 3); + check_integer_range (XCDR (val), 0, 3); XSETCAR (val, make_fixnum (id)); } -- cgit v1.2.1 From 962562cde45071f74a37854a299e88470a4ecd49 Mon Sep 17 00:00:00 2001 From: Mattias EngdegÄrd Date: Sun, 5 Apr 2020 11:27:36 +0200 Subject: Fix inverted NOCOPY encode/decode parameter (bug#40407) In {encode,decode}-coding-string, the NOCOPY parameter had the opposite effect to what was intended and documented. This 18 year old bug (introduced in 4031e2bf0a) only affected calls with CODING-SYSTEM being nil. * src/coding.c (code_convert_string): Correct use of NOCOPY. * test/src/coding-tests.el (coding-nocopy-trivial): New test. --- src/coding.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/coding.c') diff --git a/src/coding.c b/src/coding.c index f0fc37dbdfa..1049f1b755a 100644 --- a/src/coding.c +++ b/src/coding.c @@ -9485,7 +9485,7 @@ code_convert_string (Lisp_Object string, Lisp_Object coding_system, if (! norecord) Vlast_coding_system_used = Qno_conversion; if (NILP (dst_object)) - return (nocopy ? Fcopy_sequence (string) : string); + return nocopy ? string : Fcopy_sequence (string); } if (NILP (coding_system)) -- cgit v1.2.1 From 4ed39549e3f9dbfeb2aea0e2674a7701dbc0e5ea Mon Sep 17 00:00:00 2001 From: Mattias EngdegÄrd Date: Fri, 3 Apr 2020 16:01:01 +0200 Subject: Avoid expensive recoding for ASCII identity cases (bug#40407) Optimise for the common case of encoding or decoding an ASCII-only string using an ASCII-compatible coding, for file names in particular. * src/coding.c (string_ascii_p): New function. (code_convert_string): Return the input string for ASCII-only inputs and ASCII-compatible codings. * test/src/coding-tests.el (coding-nocopy-ascii): New test. --- src/coding.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/coding.c') diff --git a/src/coding.c b/src/coding.c index 1049f1b755a..97a6eb949a8 100644 --- a/src/coding.c +++ b/src/coding.c @@ -9471,6 +9471,17 @@ not fully specified.) */) return code_convert_region (start, end, coding_system, destination, 1, 0); } +/* Whether a string only contains chars in the 0..127 range. */ +static bool +string_ascii_p (Lisp_Object str) +{ + ptrdiff_t nbytes = SBYTES (str); + for (ptrdiff_t i = 0; i < nbytes; i++) + if (SREF (str, i) > 127) + return false; + return true; +} + Lisp_Object code_convert_string (Lisp_Object string, Lisp_Object coding_system, Lisp_Object dst_object, bool encodep, bool nocopy, @@ -9502,7 +9513,21 @@ code_convert_string (Lisp_Object string, Lisp_Object coding_system, chars = SCHARS (string); bytes = SBYTES (string); - if (BUFFERP (dst_object)) + if (EQ (dst_object, Qt)) + { + /* Fast path for ASCII-only input and an ASCII-compatible coding: + act as identity. */ + Lisp_Object attrs = CODING_ID_ATTRS (coding.id); + if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)) + && (STRING_MULTIBYTE (string) + ? (chars == bytes) : string_ascii_p (string))) + return (nocopy + ? string + : (encodep + ? make_unibyte_string (SDATA (string), bytes) + : make_multibyte_string (SDATA (string), bytes, bytes))); + } + else if (BUFFERP (dst_object)) { struct buffer *buf = XBUFFER (dst_object); ptrdiff_t buf_pt = BUF_PT (buf); -- cgit v1.2.1 From 24c3fa96077a5fec6d8ba65d7c49ff1a731be32f Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 5 Apr 2020 10:45:09 -0700 Subject: * src/coding.c (code_convert_string): Fix type mismatches. --- src/coding.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/coding.c') diff --git a/src/coding.c b/src/coding.c index 97a6eb949a8..49c1e625d57 100644 --- a/src/coding.c +++ b/src/coding.c @@ -9524,8 +9524,8 @@ code_convert_string (Lisp_Object string, Lisp_Object coding_system, return (nocopy ? string : (encodep - ? make_unibyte_string (SDATA (string), bytes) - : make_multibyte_string (SDATA (string), bytes, bytes))); + ? make_unibyte_string (SSDATA (string), bytes) + : make_multibyte_string (SSDATA (string), bytes, bytes))); } else if (BUFFERP (dst_object)) { -- cgit v1.2.1