From cabd22f5c6c2f47b1f6efd627e9e760a31e8f015 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sat, 21 Aug 2021 15:24:15 +0200 Subject: [PATCH] Make parse-partial-sexp signal an error if TO is smaller than FROM * src/syntax.c (Fparse_partial_sexp): Signal an error if TO is smaller than FROM (bug#49944). --- etc/NEWS | 6 ++++++ src/syntax.c | 9 +++++++-- test/src/syntax-tests.el | 6 ++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 02185a6fb48..c3993b74f60 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -3108,6 +3108,12 @@ This is to keep the same behavior as Eshell. * Incompatible Lisp Changes in Emacs 28.1 +--- +** 'parse-partial-sexp' now signals an error it TO is smaller than FROM. +Previously this would lead to the function interpreting FROM as TO and +vice versa, which would be confusing when passing in OLDSTATE, which +refers to the old state at FROM. + +++ ** 'overlays-in' now handles zero-length overlays slightly differently. Previosly, zero-length overlays at the end of the buffer were included diff --git a/src/syntax.c b/src/syntax.c index 7bba336744a..6cbe0f6855f 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -3547,8 +3547,10 @@ DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0, doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO. Parsing stops at TO or when certain criteria are met; point is set to where parsing stops. -If fifth arg OLDSTATE is omitted or nil, - parsing assumes that FROM is the beginning of a function. + +If OLDSTATE is omitted or nil, parsing assumes that FROM is the + beginning of a function. If not, OLDSTATE should be the state at + FROM. Value is a list of elements describing final state of parsing: 0. depth in parens. @@ -3594,6 +3596,9 @@ Sixth arg COMMENTSTOP non-nil means stop after the start of a comment. else target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth. */ + if (XFIXNUM (to) < XFIXNUM (from)) + error ("End position should be larger than start position."); + validate_region (&from, &to); internalize_parse_state (oldstate, &state); scan_sexps_forward (&state, XFIXNUM (from), CHAR_TO_BYTE (XFIXNUM (from)), diff --git a/test/src/syntax-tests.el b/test/src/syntax-tests.el index e4e3054d37a..bd89283dd14 100644 --- a/test/src/syntax-tests.el +++ b/test/src/syntax-tests.el @@ -500,4 +500,10 @@ the `parse-partial-sexp's are expected to stop. See (syntax-pps-comments /* 56 76 77 58) (syntax-pps-comments /* 60 78 79) +(ert-deftest test-from-to-parse-partial-sexp () + (with-temp-buffer + (insert "foo") + (should (parse-partial-sexp 1 1)) + (should-error (parse-partial-sexp 2 1)))) + ;;; syntax-tests.el ends here -- 2.39.5