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 16:34:19 +0100 Message-ID: <87y27at950.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> 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="35276"; 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 17:37:39 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 1mX3YZ-00090y-04 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 03 Oct 2021 17:37:39 +0200 Original-Received: from localhost ([::1]:48498 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mX3YW-0005ne-2U for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 03 Oct 2021 11:37:36 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36176) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mX3W2-0003JO-8m for bug-gnu-emacs@gnu.org; Sun, 03 Oct 2021 11:35:05 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:51452) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mX3W2-0004l7-12 for bug-gnu-emacs@gnu.org; Sun, 03 Oct 2021 11:35:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mX3W1-0002bj-Jt for bug-gnu-emacs@gnu.org; Sun, 03 Oct 2021 11:35:01 -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 15:35:01 +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.16332752689978 (code B ref 50946); Sun, 03 Oct 2021 15:35:01 +0000 Original-Received: (at 50946) by debbugs.gnu.org; 3 Oct 2021 15:34:28 +0000 Original-Received: from localhost ([127.0.0.1]:34765 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mX3VT-0002as-GS for submit@debbugs.gnu.org; Sun, 03 Oct 2021 11:34:27 -0400 Original-Received: from mail-wm1-f42.google.com ([209.85.128.42]:53124) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mX3VR-0002ad-8C for 50946@debbugs.gnu.org; Sun, 03 Oct 2021 11:34:25 -0400 Original-Received: by mail-wm1-f42.google.com with SMTP id m42so811354wms.2 for <50946@debbugs.gnu.org>; Sun, 03 Oct 2021 08:34:25 -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=MrceOVvaPFG7gXNXhVKmnV/RPoRK/QXBUZB1ZeBsn2Y=; b=QbiT8K73vp2O1WfCJ5aPhlSpSWQYDKjSwaFKy2sljFFFSpe/Ld1+xzA01drkICWrGG QlCyN1BtVHm0lBRItYT5hbRUBkEAf3OxMDA4G+PFlHSJ+bbHOJIdPi7nXTQOmbGd9+fq U/9OJf+oeAl5+4ZlqWEGJnZqEcijCQ6wz8wFqciPGDz5/bK82YP6e9yFrch3NmkJ+x4h JiRq0j3kerq7FRMGtIdzWUwqzdPBKaaJFMlRp2cjk6DeKq8onU2jcVO3rD9rRJs2/vsa DNCVoTNAxCAEjSrOPLh7xat9LlrTJb3OkQA1M5+swm9PaxLty4toJ+FrtihDVGC3yqB1 q7dQ== 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=MrceOVvaPFG7gXNXhVKmnV/RPoRK/QXBUZB1ZeBsn2Y=; b=627STsHCoqM6sRWrJ1jp6Uxl1uBIzma+PulJ2NEyl+jnYtNiHuVQ10LTlMr96MBL51 +KdsdAAvceqC2LbVDBScOK48Y8tV7cfDc4xReYBAcaJC+IYsukcqEHXe+nS0UrU5NaZk StmvQTWYiJ0YL2hmh38O0ThchUTVmmP6Bsy31ipYr6D3akq1V8wKi8eFQRaVr8k6GuWk jYTE6fAlp7ROs7G0d7Vy2HeR2TT+a60L555qhAQfpT8Ca6KsnIfE8hBSAPnwLJZff9SW W/ick/E/j8crjs3sRBRbPaPK2Ly2p4qZWpqprZPJfp00O73WLU0NkBxxTE4C0e4IqdKt 8qAg== X-Gm-Message-State: AOAM532kJyAHv3fBpWbsGRQ+KR8dEJSFa4B5q7f5JTXkLYdmL3yRdYVu UD6kHU9rZRPY/vI0X7YNNtinhfXqZ/o= X-Google-Smtp-Source: ABdhPJxJ4/YKB8y8DvmLIHhL9QayGVTzbknotqzHApEYMKA4qITxD9Re/XHFG4befUybFjNQtigDQw== X-Received: by 2002:a7b:cf2f:: with SMTP id m15mr14315547wmg.37.1633275258875; Sun, 03 Oct 2021 08:34:18 -0700 (PDT) Original-Received: from krug ([87.196.156.235]) by smtp.gmail.com with ESMTPSA id a63sm12544347wme.18.2021.10.03.08.34.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 08:34:18 -0700 (PDT) In-Reply-To: <83lf3a8eo7.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 03 Oct 2021 15:40:24 +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:216269 Archived-At: Eli Zaretskii writes: > Jo=C3=A3o, why didn't you simply insert > > (alist-get 'elisp-shorthands (hack-local-variables--find-variables)) > > in load-with-code-conversion, immediately after it calls > insert-file-contents? Are there any problems with that, and if so, > what are they? There shouldn't be any big problems. As I said, I think that is cleaner. However, doing it "from the outside" is safer (except for these insert-file-contents bugs/edge cases, which frankly escape me). Your suggestion has a very minor problem, that you're doing this stuff in lisp/international/mule.el, which slightly icky. A bigger problem is that hack-local-variables--find-variables isn't defined at that point and the function will then be used to load lisp/files.el itself (which happens to be where h-l-v--f-v is defined). So either we change file loading order -- a bit scary -- or we setup some kind of indirection (a hook, a variable). I tried with a hook but it doesn't work: it breaks a shorthands test, a most basic one. Maybe you can understand where the problem is? It takes me a while to debug cause I take this stuff in loadup.el to always need a 'make bootstrap' after any changes. I attach the patch I used. (BTW this is after the agreed renaming to read-symbol-shorthands, which I just pushed) Jo=C3=A3o diff --git a/lisp/international/mule.el b/lisp/international/mule.el index 2a855b5673..be6d397f79 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 load-with-code-conversion-hook nil + "Hook run in `load-with-code-conversion'.") + (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. @@ -328,6 +331,7 @@ load-with-code-conversion ;; Don't let deactivate-mark remain set. (let (deactivate-mark) (insert-file-contents fullname)) + (run-hooks 'load-with-code-conversion-hook) ;; 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 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..ecf04ac587 100644 --- a/lisp/shorthands.el +++ b/lisp/shorthands.el @@ -28,35 +28,17 @@ (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))) +(add-hook 'load-with-code-conversion-hook #'hack-read-symbol-shorthands) + +(defun hack-read-symbol-shorthands () + "Set `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. + (setq-local read-symbol-shorthands + (alist-get 'read-symbol-shorthands + (hack-local-variables--find-variables)))) =20 ;; FIXME: move this all to progmodes/elisp-mode.el? OTOH it'd make