From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Newsgroups: gmane.emacs.bugs Subject: bug#50946: insert-file-contents can corrupt buffers. [Was: bug#50946: Emacs-28: Inadequate coding in hack-elisp-shorthands] Date: Sun, 03 Oct 2021 19:59:24 +0100 Message-ID: <87ilyeaq9f.fsf@gmail.com> References: <831r54einq.fsf@gnu.org> <871r54xnds.fsf@gmail.com> <87ee933bcj.fsf@gmail.com> <83pmsnbnci.fsf@gnu.org> <83k0ivbjbu.fsf@gnu.org> <83czonbhex.fsf@gnu.org> <83lf3a8eo7.fsf@gnu.org> <87y27at950.fsf@gmail.com> <83bl4685m2.fsf@gnu.org> <837deu84i0.fsf@gnu.org> <87pmsmavj6.fsf@gmail.com> <83y27a6lh8.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22576"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.60 (gnu/linux) Cc: 50946@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Oct 03 21:00:22 2021 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 1mX6ij-0005dK-5A for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 03 Oct 2021 21:00:21 +0200 Original-Received: from localhost ([::1]:47746 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mX6ig-0007jd-Bj for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 03 Oct 2021 15:00:18 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35374) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mX6iQ-0007i0-Lv for bug-gnu-emacs@gnu.org; Sun, 03 Oct 2021 15:00:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:51647) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mX6iQ-0003OW-AE for bug-gnu-emacs@gnu.org; Sun, 03 Oct 2021 15:00:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mX6iQ-00089e-5y for bug-gnu-emacs@gnu.org; Sun, 03 Oct 2021 15:00:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 03 Oct 2021 19:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 50946 X-GNU-PR-Package: emacs Original-Received: via spool by 50946-submit@debbugs.gnu.org id=B50946.163328757431281 (code B ref 50946); Sun, 03 Oct 2021 19:00:02 +0000 Original-Received: (at 50946) by debbugs.gnu.org; 3 Oct 2021 18:59:34 +0000 Original-Received: from localhost ([127.0.0.1]:34960 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mX6hy-00088T-3r for submit@debbugs.gnu.org; Sun, 03 Oct 2021 14:59:34 -0400 Original-Received: from mail-wr1-f45.google.com ([209.85.221.45]:39745) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mX6hu-00088D-Tn for 50946@debbugs.gnu.org; Sun, 03 Oct 2021 14:59:32 -0400 Original-Received: by mail-wr1-f45.google.com with SMTP id d26so26227437wrb.6 for <50946@debbugs.gnu.org>; Sun, 03 Oct 2021 11:59:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-transfer-encoding; bh=r6TaJ1BVDG0NEe5vP9WC7J5DrLh24gRtWCLxqhpDIac=; b=NNLE/8gpfEWsDUhVvuJXyeOuuJj5sRqM5B1U9M0W5r8qtNWAaMLnsR2ZDK3yLq/96H YRuBuiISXFovIrzE2mAUGhu7ngl/c7EEOCHBJOaHSZmHUmXXcE9a+HxpFABKnt9NFl33 vo/3of1+u+qrZ+rojhUPpWFO55Y2rJdRzMxQoh9KmvcNUJbHR2iPv9ZJPOJL27ZVK3iC JtfgYbHW1MtXD1uYSZ59SF+L0HlCrahMnT33z7on8WVJxRgF2OHS74LxQW5tT+L9pfKz dkYOHCz7mko3r8BBBGBBgFGlI0cNcprRy8l+awBACF4UeiGSRvLy9qci/tSWP+kopVnj /9SA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-transfer-encoding; bh=r6TaJ1BVDG0NEe5vP9WC7J5DrLh24gRtWCLxqhpDIac=; b=EQa9R72w5YmCu1mZuSizSMF2BBets/Y9g1USA9eGArdTZxSzLePsQd38EQ4HHiT8Dj Y7nzNv1f4JTikgyyM/Z0SUs5M704nE9Sn8eF3/VHc5ODUE01UgdhncV2lJzr3zWxWkQz HDkinckUO1hnOlgbA2MmAHVT1FvEd50IwAF9k3XsG4gSciqsq/VRHESdrQDxzYLMezUc 0jW7TS+gbnUs2K3Tombnh/YsWO163of0L7EZVyEZv8tiW6VJaDFAox8eRkzvCsHXEyLy MVPRd06maVgftrs9TkOzXmMTSi0l7xSRT6r+BGCSBxqS2Plm1HjL7iIWtvkzEVKMcams IqWg== X-Gm-Message-State: AOAM5331cXFF4TQJd30wlcZJvA2CHOhtuQrESgrpbIk+FcSmn2Xchc2Y ujdkKJmIksTiKxylwsH5CDHw0JKscHY= X-Google-Smtp-Source: ABdhPJwsfBFElPhHaKCC6eZH+T9U/K+SvNSR7QeCvsoAUl8etGVrmr4i1/rnBXsjSie/xmonoLnUMQ== X-Received: by 2002:adf:979a:: with SMTP id s26mr5258992wrb.2.1633287563611; Sun, 03 Oct 2021 11:59:23 -0700 (PDT) Original-Received: from krug (87-196-156-235.net.novis.pt. [87.196.156.235]) by smtp.gmail.com with ESMTPSA id l13sm12317180wme.38.2021.10.03.11.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 11:59:23 -0700 (PDT) In-Reply-To: <83y27a6lh8.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 03 Oct 2021 20:56:19 +0300") 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" Xref: news.gmane.io gmane.emacs.bugs:216299 Archived-At: Eli Zaretskii writes: >> From: Jo=C3=A3o T=C3=A1vora >> Cc: 50946@debbugs.gnu.org >> Date: Sun, 03 Oct 2021 18:05:33 +0100 > Note that previously, the shorthand searching and application was > effectively turned off until very late into the loadup procedure. But > now, we enable it as soon as files.el is loaded, which is way > earlier. Somewhere there is the reason for the problem. Yes, I agree, this makes sense. > So I think, instead of the fboundp test, introduce a variable, > say inhibit-shorthands, set it to t at the beginning of loadup, then > reset to nil after shorthands.el has been loaded. At this point, I think that's slightly worse than introducing hack-read-symbol-shorthands-function... or introducing a hook as I had before. Given you dislike hooks, the patch with hack-read-symbol-shorthands-function is below. Looks good? Jo=C3=A3o commit d4416d7f2083bd55984193b94241edb76bb2879c Author: Jo=C3=A3o T=C3=A1vora Date: Sun Oct 3 16:05:40 2021 +0100 Simplify hack-read-symbol-shorthands again (bug#50946) =20=20=20=20 * lisp/loadup.el (load-source-file-function): Don't set twice. =20=20=20=20 * lisp/shorthands.el (hack-read-symbol-shorthands): Simplify. (load-with-shorthands-and-code-conversion): Remove. =20=20=20=20 * lisp/international/mule.el (load-with-code-conversion): Call hack-read-symbol-shorthands-function. Set up shorthands. (hack-read-symbol-shorthands-function): New variable =20=20=20=20 diff --git a/lisp/international/mule.el b/lisp/international/mule.el index 2a855b5673..d00e39d228 100644 --- a/lisp/international/mule.el +++ b/lisp/international/mule.el @@ -294,6 +294,9 @@ define-charset =20 (apply 'define-charset-internal name (mapcar 'cdr attrs)))) =20 +(defvar hack-read-symbol-shorthands-function nil + "Holds function to compute `read-symbol-shorthands'.") + (defun load-with-code-conversion (fullname file &optional noerror nomessag= e) "Execute a file of Lisp code named FILE whose absolute name is FULLNAME. The file contents are decoded before evaluation if necessary. @@ -319,7 +322,8 @@ load-with-code-conversion (let ((load-true-file-name fullname) (load-file-name fullname) (set-auto-coding-for-load t) - (inhibit-file-name-operation nil)) + (inhibit-file-name-operation nil) + shorthands) (with-current-buffer buffer ;; So that we don't get completely screwed if the ;; file is encoded in some complicated character set, @@ -328,6 +332,12 @@ load-with-code-conversion ;; Don't let deactivate-mark remain set. (let (deactivate-mark) (insert-file-contents fullname)) + (setq shorthands + ;; We need this indirection because hacking local + ;; variables in too early seems to have cause recursive + ;; load loops (bug#50946). + (and hack-read-symbol-shorthands-function + (funcall hack-read-symbol-shorthands-function))) ;; If the loaded file was inserted with no-conversion or ;; raw-text coding system, make the buffer unibyte. ;; Otherwise, eval-buffer might try to interpret random @@ -338,11 +348,13 @@ load-with-code-conversion (set-buffer-multibyte nil)) ;; Make `kill-buffer' quiet. (set-buffer-modified-p nil)) - ;; Have the original buffer current while we eval. - (eval-buffer buffer nil - ;; This is compatible with what `load' does. - (if dump-mode file fullname) - nil t)) + ;; Have the original buffer current while we eval, + ;; but consider shorthands of the eval'ed one. + (let ((read-symbol-shorthands shorthands)) + (eval-buffer buffer nil + ;; This is compatible with what `load' does. + (if dump-mode file fullname) + nil t))) (let (kill-buffer-hook kill-buffer-query-functions) (kill-buffer buffer))) (do-after-load-evaluation fullname) diff --git a/lisp/loadup.el b/lisp/loadup.el index 3fb6b81328..3a55d2c805 100644 --- a/lisp/loadup.el +++ b/lisp/loadup.el @@ -355,7 +355,6 @@ (load "paren") =20 (load "shorthands") -(setq load-source-file-function #'load-with-shorthands-and-code-conversion) =20 (load "emacs-lisp/eldoc") (load "cus-start") ;Late to reduce customize-rogue (needs loaddefs.el anyw= ay) diff --git a/lisp/shorthands.el b/lisp/shorthands.el index c31ef3d216..40a960ff7d 100644 --- a/lisp/shorthands.el +++ b/lisp/shorthands.el @@ -28,35 +28,15 @@ (require 'files) (eval-when-compile (require 'cl-lib)) =20 -(defun hack-read-symbol-shorthands (fullname) - "Return value of `read-symbol-shorthands' file-local variable in FULLNAM= E. -FULLNAME is the absolute file name of an Elisp .el file which -potentially specifies a file-local value for -`read-symbol-shorthands'. The Elisp code in FULLNAME isn't read -or evaluated in any way, except for extraction of the -buffer-local value of `read-symbol-shorthands'." - (let* ((size (nth 7 (file-attributes fullname))) - (from (max 0 (- size 3000))) - (to size)) - (with-temp-buffer - (while (and (< (buffer-size) 3000) (>=3D from 0)) - (insert-file-contents fullname nil from to) - (setq to from - from (cond - ((=3D from 0) -1) - (t (max 0 (- from 100)))))) - ;; FIXME: relies on the `hack-local-variables--find-variables' - ;; detail of files.el. That function should be exported, - ;; possibly be refactored into two parts, since we're only - ;; interested in basic "Local Variables" parsing. - (alist-get 'read-symbol-shorthands (hack-local-variables--find-varia= bles))))) - -(defun load-with-shorthands-and-code-conversion (fullname file noerror nom= essage) - "Like `load-with-code-conversion', but also consider Elisp shorthands. -This function uses shorthands defined in the file FULLNAME's local -value of `read-symbol-shorthands', when it processes that file's Elisp cod= e." - (let ((read-symbol-shorthands (hack-read-symbol-shorthands fullname))) - (load-with-code-conversion fullname file noerror nomessage))) +(defun hack-read-symbol-shorthands () + "Compute `read-symbol-shorthands' from Local Variables section." + ;; FIXME: relies on the `hack-local-variables--find-variables' + ;; detail of files.el. That function should be exported, + ;; possibly be refactored into two parts, since we're only + ;; interested in basic "Local Variables" parsing. + (alist-get 'read-symbol-shorthands (hack-local-variables--find-variables= ))) + +(setq hack-read-symbol-shorthands-function #'hack-read-symbol-shorthands) =20 ;; FIXME: move this all to progmodes/elisp-mode.el? OTOH it'd make