From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#61514: 30.0.50; sadistically long xml line hangs emacs Date: Mon, 20 Feb 2023 13:49:49 -0500 Message-ID: References: <87lel0c65v.fsf@everybody.org> <838rgvymcd.fsf@gnu.org> <831qmkwmux.fsf@gnu.org> <83cz64v3v7.fsf@gnu.org> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8602"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Eli Zaretskii , 61514@debbugs.gnu.org, mah@everybody.org To: Gregory Heytings Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Feb 20 19:51:16 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pUBFr-0001z9-Js for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 20 Feb 2023 19:51:15 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUBFg-0007JC-HF; Mon, 20 Feb 2023 13:51:04 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUBFe-00076m-M0 for bug-gnu-emacs@gnu.org; Mon, 20 Feb 2023 13:51:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pUBFe-0006Ii-BJ for bug-gnu-emacs@gnu.org; Mon, 20 Feb 2023 13:51:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pUBFe-0002Jx-7E for bug-gnu-emacs@gnu.org; Mon, 20 Feb 2023 13:51:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 20 Feb 2023 18:51:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61514 X-GNU-PR-Package: emacs Original-Received: via spool by 61514-submit@debbugs.gnu.org id=B61514.16769190028843 (code B ref 61514); Mon, 20 Feb 2023 18:51:02 +0000 Original-Received: (at 61514) by debbugs.gnu.org; 20 Feb 2023 18:50:02 +0000 Original-Received: from localhost ([127.0.0.1]:53518 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pUBEg-0002IZ-60 for submit@debbugs.gnu.org; Mon, 20 Feb 2023 13:50:02 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:16939) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pUBEd-0002Hw-4f for 61514@debbugs.gnu.org; Mon, 20 Feb 2023 13:50:01 -0500 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 74053806AA; Mon, 20 Feb 2023 13:49:53 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id CC32180658; Mon, 20 Feb 2023 13:49:51 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1676918991; bh=1t5Wv5Ywn3q7/4ZrEeU7DlipL7BHVwZGA2dEV+X4N4k=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=ffV/56GU8txDSzYoK3sZI4CoDaRkMmlzC6jeF3BAQrwiCRSkMwI3AqIKrB7GSfLY9 b/fUPivHEA3UcgKvKrJa6cjCVQqfODlInuZIRCX8SoUJxMRe3Y0MJmalmYr5ik0jRh M7Ecn/dNKMv0Z1DZLmQUCWrmeGtyPl+lHEBpNIVny5Gt1D56scpwlxMZIzCuUN9+n6 lq8djUs0piGKMN/SceQrNQrIOwX8qs+7ZOnC4HnmjOW/uhWugLn2H23F3uqMJJTl47 dZFT0OSU6Z+f9Rx56VN4QEUTFRGfSf3/rhSMfXnNXlm5stAzkThb5niebey1cpGOU5 9HnNmv+/JMhnQ== Original-Received: from pastel (unknown [216.154.34.24]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 959B012320F; Mon, 20 Feb 2023 13:49:51 -0500 (EST) In-Reply-To: (Stefan Monnier's message of "Mon, 20 Feb 2023 11:47:38 -0500") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:256212 Archived-At: > where I can even reduce the regexp down to "[-._[:alnum:]]*\t*=". > Looks like we're missing a case in our backtracking-elimination code. The patch below fixes the stack overflow. [ And thanks Gregory for the yet simpler test cases. ] I don't think we want that for `emacs-29`, but unless there's some objection I'll push this to `master`, Stefan diff --git a/src/regex-emacs.c b/src/regex-emacs.c index 2dca0d16ad9..2571812cb39 100644 --- a/src/regex-emacs.c +++ b/src/regex-emacs.c @@ -3653,6 +3653,7 @@ mutually_exclusive_p (struct re_pattern_buffer *bufp, re_char *p1, re_opcode_t op2; bool multibyte = RE_MULTIBYTE_P (bufp); unsigned char *pend = bufp->buffer + bufp->used; + re_char *p2_orig = p2; eassert (p1 >= bufp->buffer && p1 < pend && p2 >= bufp->buffer && p2 <= pend); @@ -3822,6 +3823,23 @@ mutually_exclusive_p (struct re_pattern_buffer *bufp, re_char *p1, case notcategoryspec: return ((re_opcode_t) *p1 == categoryspec && p1[1] == p2[1]); + case on_failure_jump_nastyloop: + case on_failure_jump_smart: + case on_failure_jump_loop: + case on_failure_keep_string_jump: + case on_failure_jump: + { + int mcnt; + p2++; + EXTRACT_NUMBER_AND_INCR (mcnt, p2); + /* Don't just test `mcnt > 0` because non-greedy loops have + their test at the end with an unconditional jump at the start. */ + if (p2 + mcnt > p2_orig) /* Ensure forward progress. */ + return (mutually_exclusive_p (bufp, p1, p2) + && mutually_exclusive_p (bufp, p1, p2 + mcnt)); + break; + } + default: ; } diff --git a/test/src/regex-emacs-tests.el b/test/src/regex-emacs-tests.el index 34fa35e32ff..52d43775b8e 100644 --- a/test/src/regex-emacs-tests.el +++ b/test/src/regex-emacs-tests.el @@ -872,4 +872,15 @@ regexp-atomic-failure (should (equal (string-match "\\`\\(?:ab\\)*\\'" "a") nil)) (should (equal (string-match "\\`a\\{2\\}*\\'" "a") nil))) +(ert-deftest regexp-tests-backtrack-optimization () ;bug#61514 + ;; Make sure we don't use up the regexp stack needlessly. + (with-current-buffer (get-buffer-create "*bug*") + (erase-buffer) + (insert (make-string 1000000 ?x) "=") + (goto-char (point-min)) + (should (looking-at "x*=*")) + (should (looking-at "x*\\(=\\|:\\)")) + (should (looking-at "x*\\(=\\|:\\)*")) + (should (looking-at "x*=*?")))) + ;;; regex-emacs-tests.el ends here