How to fix “stack overflow in regexp matcher” in emacs

与世无争的帅哥 提交于 2019-12-05 02:05:07

The underlying problem is that a regular expression (regexp) contains too many alternatives, and when applied to a (typically long) text it fails to match whatever it tried to match.

In your case it's the regexp:

"\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\"\\(?:[^\\\"]\\|\\\\.\\)*\"\\)"

Which is used by the function gdb-jsonify-buffer.

It looks like this regexp tries to match assignments. Basically, it matches a variable to the left of an = and (part of) an expression to the right. One of the things the regexp seems to match is a string, with containing escaped quotes -- this is always a warning sign as Emacs provides far better methods for parsing strings.

The problem can originate from the fact that this regexp is wrong (so that it matches a lot more than your string), that you have a malformed string, or that your program simply contains a really large string.

I would suggest that you file a bug report to the maintainer of that package. Make sure you include the text that caused the error to be triggered.

Alternatively, you could make an attempt at fixing this yourself. I would suggest that you replace the complex regexp with a simpler regexp that find the beginning of a string. You could then use, for example, (forward-sexp) to find the end of the string.

I was also having this problem, so I used Lindydancer's suggestion of converting the regexp on the string literal to use (forward-sexp), and it's been working fine for me.

I've posted the patch:

http://lists.gnu.org/archive/html/bug-gnu-emacs/2017-12/msg00968.html

so hopefully it will get merged at some point. In the meantime, you can use this for gdb-jsonify-buffer:


(defun gdb-jsonify-buffer (&optional fix-key fix-list)
  "Prepare GDB/MI output in current buffer for parsing with `json-read'.

Field names are wrapped in double quotes and equal signs are
replaced with semicolons.

If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
partial output.  This is used to get rid of useless keys in lists
in MI messages, e.g.: [key=.., key=..].  -stack-list-frames and
-break-info are examples of MI commands which issue such
responses.

If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
\"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
-break-info output when it contains breakpoint script field
incompatible with GDB/MI output syntax.

If `default-directory' is remote, full file names are adapted accordingly."
  (save-excursion
    (let ((remote (file-remote-p default-directory)))
      (when remote
        (goto-char (point-min))
        (while (re-search-forward "[\\[,]fullname=\"\\(.+\\)\"" nil t)
          (replace-match (concat remote "\\1") nil nil nil 1))))
    (goto-char (point-min))
    (when fix-key
      (save-excursion
        (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
          (replace-match "" nil nil nil 1))))
    (when fix-list
      (save-excursion
        ;; Find positions of braces which enclose broken list
        (while (re-search-forward (concat fix-list "={\"") nil t)
          (let ((p1 (goto-char (- (point) 2)))
                (p2 (progn (forward-sexp)
                           (1- (point)))))
            ;; Replace braces with brackets
            (save-excursion
              (goto-char p1)
              (delete-char 1)
              (insert "[")
              (goto-char p2)
              (delete-char 1)
              (insert "]"))))))
    (goto-char (point-min))
    (insert "{")
    (let ((re (concat "\\([[:alnum:]-_]+\\)=")))
      (while (re-search-forward re nil t)
        (replace-match "\"\\1\":" nil nil)
        (if (eq (char-after) ?\") (forward-sexp) (forward-char))))
    (goto-char (point-max))
    (insert "}")))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!