From e233513d28210a7aacc750a5e70fc8352b4bf12d Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Mon, 24 Nov 2025 10:26:21 -0500 Subject: [PATCH] editorconfig-core-handle.el: Fix silent misparse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/editorconfig/editorconfig-emacs/issues/380 * lisp/editorconfig-core-handle.el (editorconfig-core-handle--parse-file): Fix regexp to not inadvertently match LF. Remove an O(N²) complexity. Use `line-number-at-pos`. * lisp/editorconfig.el (editorconfig--get-coding-system): Don't let errors propagate. --- lisp/editorconfig-core-handle.el | 13 ++++--------- lisp/editorconfig.el | 26 +++++++++++++++----------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lisp/editorconfig-core-handle.el b/lisp/editorconfig-core-handle.el index 5dd8c73d4e2..892ad61b6d3 100644 --- a/lisp/editorconfig-core-handle.el +++ b/lisp/editorconfig-core-handle.el @@ -166,10 +166,7 @@ If CONF is not found return nil." ;; nil when pattern not appeared yet, "" when pattern is empty ("[]") (pattern nil) ;; Alist of properties for current PATTERN - (props ()) - - ;; Current line num - (current-line-number 1)) + (props ())) (while (not (eobp)) (skip-chars-forward " \t\f") (cond @@ -187,7 +184,7 @@ If CONF is not found return nil." (setq props nil) (setq pattern newpattern))) - ((looking-at "\\([^=: \t][^=:]*\\)[ \t]*[=:][ \t]*\\(.*?\\)[ \t]*$") + ((looking-at "\\([^=: \n\t][^=:\n]*\\)[ \t]*[=:][ \t]*\\(.*?\\)[ \t]*$") (let ((key (downcase (string-trim (match-string 1)))) (value (match-string 2))) (if pattern @@ -197,12 +194,10 @@ If CONF is not found return nil." top-props)))) (t (error "Error while reading config file: %s:%d:\n %s\n" - conf current-line-number + conf (line-number-at-pos) (buffer-substring-no-properties (line-beginning-position) (line-end-position))))) - (setq current-line-number (1+ current-line-number)) - (goto-char (point-min)) - (forward-line (1- current-line-number))) + (forward-line 1)) (when pattern (push (make-editorconfig-core-handle-section :name pattern diff --git a/lisp/editorconfig.el b/lisp/editorconfig.el index 04c7314369d..42ff3038b92 100644 --- a/lisp/editorconfig.el +++ b/lisp/editorconfig.el @@ -658,17 +658,21 @@ F is that function, and FILENAME and ARGS are arguments passed to F." "Return the coding system to use according to EditorConfig. Meant to be used on `auto-coding-functions'." (defvar auto-coding-file-name) ;; Emacs≥30 - (when (and (stringp auto-coding-file-name) - (file-name-absolute-p auto-coding-file-name) - ;; Don't recurse infinitely. - (not (member auto-coding-file-name - editorconfig--getting-coding-system))) - (let* ((editorconfig--getting-coding-system - (cons auto-coding-file-name editorconfig--getting-coding-system)) - (props (editorconfig-call-get-properties-function - auto-coding-file-name))) - (editorconfig-merge-coding-systems (gethash 'end_of_line props) - (gethash 'charset props))))) + ;; Not only we don't want that an error in the `.editorconfig' file + ;; prevents opening a file but we don't want an error to be dropped on + ;; the floor by some `ignore-errors' higher up. + (with-demoted-errors "EditorConfig: %S" + (when (and (stringp auto-coding-file-name) + (file-name-absolute-p auto-coding-file-name) + ;; Don't recurse infinitely. + (not (member auto-coding-file-name + editorconfig--getting-coding-system))) + (let* ((editorconfig--getting-coding-system + (cons auto-coding-file-name editorconfig--getting-coding-system)) + (props (editorconfig-call-get-properties-function + auto-coding-file-name))) + (editorconfig-merge-coding-systems (gethash 'end_of_line props) + (gethash 'charset props)))))) (defun editorconfig--get-dir-local-variables () "Return the directory local variables specified via EditorConfig.