1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-05 22:20:24 -08:00

editorconfig-core-handle.el: Fix silent misparse

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.
This commit is contained in:
Stefan Monnier 2025-11-24 10:26:21 -05:00
parent 188ad4f909
commit e233513d28
2 changed files with 19 additions and 20 deletions

View file

@ -166,10 +166,7 @@ If CONF is not found return nil."
;; nil when pattern not appeared yet, "" when pattern is empty ("[]") ;; nil when pattern not appeared yet, "" when pattern is empty ("[]")
(pattern nil) (pattern nil)
;; Alist of properties for current PATTERN ;; Alist of properties for current PATTERN
(props ()) (props ()))
;; Current line num
(current-line-number 1))
(while (not (eobp)) (while (not (eobp))
(skip-chars-forward " \t\f") (skip-chars-forward " \t\f")
(cond (cond
@ -187,7 +184,7 @@ If CONF is not found return nil."
(setq props nil) (setq props nil)
(setq pattern newpattern))) (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)))) (let ((key (downcase (string-trim (match-string 1))))
(value (match-string 2))) (value (match-string 2)))
(if pattern (if pattern
@ -197,12 +194,10 @@ If CONF is not found return nil."
top-props)))) top-props))))
(t (error "Error while reading config file: %s:%d:\n %s\n" (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) (buffer-substring-no-properties (line-beginning-position)
(line-end-position))))) (line-end-position)))))
(setq current-line-number (1+ current-line-number)) (forward-line 1))
(goto-char (point-min))
(forward-line (1- current-line-number)))
(when pattern (when pattern
(push (make-editorconfig-core-handle-section (push (make-editorconfig-core-handle-section
:name pattern :name pattern

View file

@ -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. "Return the coding system to use according to EditorConfig.
Meant to be used on `auto-coding-functions'." Meant to be used on `auto-coding-functions'."
(defvar auto-coding-file-name) ;; Emacs≥30 (defvar auto-coding-file-name) ;; Emacs≥30
(when (and (stringp auto-coding-file-name) ;; Not only we don't want that an error in the `.editorconfig' file
(file-name-absolute-p auto-coding-file-name) ;; prevents opening a file but we don't want an error to be dropped on
;; Don't recurse infinitely. ;; the floor by some `ignore-errors' higher up.
(not (member auto-coding-file-name (with-demoted-errors "EditorConfig: %S"
editorconfig--getting-coding-system))) (when (and (stringp auto-coding-file-name)
(let* ((editorconfig--getting-coding-system (file-name-absolute-p auto-coding-file-name)
(cons auto-coding-file-name editorconfig--getting-coding-system)) ;; Don't recurse infinitely.
(props (editorconfig-call-get-properties-function (not (member auto-coding-file-name
auto-coding-file-name))) editorconfig--getting-coding-system)))
(editorconfig-merge-coding-systems (gethash 'end_of_line props) (let* ((editorconfig--getting-coding-system
(gethash 'charset props))))) (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 () (defun editorconfig--get-dir-local-variables ()
"Return the directory local variables specified via EditorConfig. "Return the directory local variables specified via EditorConfig.