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#68272: [PATCH] Fix -1 leaking from C to lisp in 'read-event' etc. Date: Tue, 05 Mar 2024 11:45:41 -0500 Message-ID: References: <46480759b6d89b5a4864e8ee1b986817366a56e5.camel@timruffing.de> <83wmsmucuo.fsf@gnu.org> <1b3fa12138838a3fe5643a9e76a65d32a677e34d.camel@timruffing.de> <33de23a45d00e23ddfebb24d16db95d638ac96f1.camel@timruffing.de> <933c41e932327b6c149706ca251c18046d9ffb8b.camel@timruffing.de> 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="22486"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Eli Zaretskii , 68272@debbugs.gnu.org To: Tim Ruffing Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Mar 05 17:47:28 2024 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 1rhXws-0005ag-UL for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 05 Mar 2024 17:47:27 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rhXw7-0004NH-GQ; Tue, 05 Mar 2024 11:46:39 -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 1rhXw0-0004K4-9Y for bug-gnu-emacs@gnu.org; Tue, 05 Mar 2024 11:46:33 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rhXw0-0001do-0L for bug-gnu-emacs@gnu.org; Tue, 05 Mar 2024 11:46:32 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rhXwU-00011u-7H for bug-gnu-emacs@gnu.org; Tue, 05 Mar 2024 11:47: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: Tue, 05 Mar 2024 16:47:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68272 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 68272-submit@debbugs.gnu.org id=B68272.17096571933922 (code B ref 68272); Tue, 05 Mar 2024 16:47:02 +0000 Original-Received: (at 68272) by debbugs.gnu.org; 5 Mar 2024 16:46:33 +0000 Original-Received: from localhost ([127.0.0.1]:48795 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rhXw0-000119-So for submit@debbugs.gnu.org; Tue, 05 Mar 2024 11:46:33 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:19570) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rhXvx-00010j-19 for 68272@debbugs.gnu.org; Tue, 05 Mar 2024 11:46:31 -0500 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id E0FE380C35; Tue, 5 Mar 2024 11:45:52 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1709657151; bh=sSdJUFuw9fEMyzPYGQgaxC4kF4mL1BEujkuCH73zoAc=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=MLtU2ktVcs/cOoqSmqYwztzcr1wn0NkQlEL5air1P89VnIvNNaS1lLEXpROcVKtff 79WPnXV0kGPpZBKXGTxx4D4VycTG5tYYEIFPzrtRzaPUuNopVHmZ1I576EMMjH2Xc5 OoHH0unZ4zpVClNiVkC2y5CLc4/+EpefUtGK/CVKt8/RN45u6rw9I2TnmcCq9pZCof r0+V23WFCmOLvY0axjOrUQwAe9JEENuGckwKwkFPwoNjZ0gH4A4MymJCzNps1sIoCH HQTt20rU07tt3dwRguilWKRneChMquJwxKrvpx+4UmTSI1HvcZNbonnB/k02eKJR4t hh+eawrgjxnPg== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 73CEC805ED; Tue, 5 Mar 2024 11:45:51 -0500 (EST) Original-Received: from pastel (unknown [104.247.233.29]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 4D4F5120672; Tue, 5 Mar 2024 11:45:51 -0500 (EST) In-Reply-To: <933c41e932327b6c149706ca251c18046d9ffb8b.camel@timruffing.de> (Tim Ruffing's message of "Mon, 04 Mar 2024 19:42:09 +0100") 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:281063 Archived-At: > Hi, this is an updated patch set. Looks really nice, thank you. Comments/nitpicks below. Eli Zaretskii [2024-03-05 15:10:39] wrote: > IOW, what about callers that actually _want_ to know when the macro > ends prematurely? I couldn't find any, really. Of course, we could export `at_end_of_macro_p` to ELisp, but I don't see any need for it. [ Also: define "prematurely". My impression is that the callers of `read-char` are generally not in a position to know what is premature and what isn't because it tends to depend on the users' intentions. ] > +/* Whether the execution of a macro has reached its end. > + It makes only sense to call this when while executing a macro. */ ^^^^ ^^^^ \----------------------^ [ And same for the other copy of that coment. ] > +/* Return true if there are any pending requeued events (command events > + or events to be processed by input methods). */ I think I'd say "other levels of the input processing stages" instead of "input methods", so as to conceptually include any other "unread_*_events" we may end up with. > - /* If not, we should actually read a character. */ > + /* If we're at the end of a macro, exit it by returning 0, > + unless there are unread events pending. */ > + else if (!NILP (Vexecuting_kbd_macro) > + && at_end_of_macro_p () > + && !requeued_events_pending_p ()) > + { > + t = 0; > + /* The Microsoft C compiler can't handle the goto that > + would go here. */ > + dummyflag = true; > + break; > + } This "Microsoft C compiler" business dates back to 1994 (commit bc536d847736f466727453ca6aa7c07aef6fce46). I think it's safe to clean it up now :-) > index 62129be1629..98290e7e276 100644 > --- a/src/macros.c > +++ b/src/macros.c > @@ -314,6 +314,29 @@ DEFUN ("execute-kbd-macro", Fexecute_kbd_macro, Sexecute_kbd_macro, 1, 3, 0, > Vreal_this_command)); > record_unwind_protect (pop_kbd_macro, tem); > > + /* The following loop starts the execution of the macro. Individual > + characters from the macro are read by read_char, which takes care > + of incrementing executing_kbd_macro_index. The end of the > + macro is handled as follows: > + - read_key_sequence asks at_end_of_macro_p whether the end of > + (one iteration of the macro) has been reached. If so, it returns > + the magic value 0 to command_loop_1. > + - command_loop_1 returns Qnil to command_loop_2. > + - command_loop_2 returns Qnil to this function > + (but only the returning is relevant, not the actual value). Could you complete the sequence to the point where we clear Vexecuting_kbd_macro? > + If read_char happens to be called at the end of the macro, but > + before read_key_sequence could handle the end (e.g., because lisp > + code calls 'read-event', 'read-char', and 'read-char-exclusive'), > + read_char will simply continue reading other available input > + (Bug#68272). Could you clarify here what happens w.r.t the value of Vexecuting_kbd_macro (AFAICT, we "remain `at_end_of_macro_p`"). > + Note that this is similar (in observable behavior) to a simpler > + implementation of keyboard macros in which this function simply > + pushed all characters of the macro into the incoming event queue > + and returned immediately. Maybe this is the implementation that > + we ideally would like to have, but switching to it will require > + a larger code change. */ It might be worth mentioning that the main difference is the availability of `executing-kbd-macro` to let ELisp code behave differently when called via a kmacro than via "live input". Which also kind of justifies why `read-key-sequence` wants to detect the end: if a kmacro ends in the middle of a key sequence, then it's triggered both my kmacro and by live input. [ Of course, we could handle it in the command loop instead: check and compare the set of pending kmacro events before and after we call `read-key-sequence`. ] Stefan