From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "Bryan C. Mills" via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#48940: 27.2; regression: "emacs --script /dev/stdin" parses the script incorrectly when /dev/stdin is a pipe Date: Mon, 14 Jun 2021 17:37:17 -0400 Message-ID: References: <83pmwq6oxc.fsf@gnu.org> Reply-To: "Bryan C. Mills" 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="25241"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 48940@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jun 14 23:39:10 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 1lsuIX-0006MI-KZ for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 14 Jun 2021 23:39:09 +0200 Original-Received: from localhost ([::1]:40540 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lsuIW-0006PC-MV for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 14 Jun 2021 17:39:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34718) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lsuIQ-0006Om-L0 for bug-gnu-emacs@gnu.org; Mon, 14 Jun 2021 17:39:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:36339) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lsuIQ-0000x3-Ds for bug-gnu-emacs@gnu.org; Mon, 14 Jun 2021 17:39:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lsuIQ-0002md-AM for bug-gnu-emacs@gnu.org; Mon, 14 Jun 2021 17:39:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: "Bryan C. Mills" Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 14 Jun 2021 21:39:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 48940 X-GNU-PR-Package: emacs Original-Received: via spool by 48940-submit@debbugs.gnu.org id=B48940.162370668510625 (code B ref 48940); Mon, 14 Jun 2021 21:39:02 +0000 Original-Received: (at 48940) by debbugs.gnu.org; 14 Jun 2021 21:38:05 +0000 Original-Received: from localhost ([127.0.0.1]:47881 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lsuHV-0002lJ-4g for submit@debbugs.gnu.org; Mon, 14 Jun 2021 17:38:05 -0400 Original-Received: from mail-vk1-f173.google.com ([209.85.221.173]:35804) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lsuHT-0002kp-6X for 48940@debbugs.gnu.org; Mon, 14 Jun 2021 17:38:03 -0400 Original-Received: by mail-vk1-f173.google.com with SMTP id l7so4486443vkk.2 for <48940@debbugs.gnu.org>; Mon, 14 Jun 2021 14:38:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=DPNT7zn+aYKGkqLQlopdVPD3NV7R04XBxUjFmzXGfgw=; b=ncQsYR6HwuvH/EwAtZgZlpvQC1cXs1iTHTXd0HCR8Fq1sIliyKyq8CGILmbHu1wnd7 AWf8K0+86ZnV0P0KeSgE+o317NJksnER2pR6JWiP1nGJF/eZ2f4FuRzwwnjG+X4jMWeI 54xTKoqKpA0+PBtxqDzRKSpsMKsYNWqJxeHLZlTx5Q4H5P4+C+NgdL/LUZ1X+17pPsE7 V5+IqyO548EhezPC55NGMitT2cWN+SswdhnPAufroj76ClxSrri5s87vtOVcZuDCpLz4 YZk9ffsEMt/jusI6/PvoBfuW3K+WVQVwOx70SSk6XzTECk/nRxV3yFyozo6jcDuBsCOE QqnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=DPNT7zn+aYKGkqLQlopdVPD3NV7R04XBxUjFmzXGfgw=; b=pgAQ346WFuLeU0OPghSXqfQhTm7KVSYUvNrxtIEFTPu1sgFPYAMvQMMgHxAEP6+k9/ 4pgqTdiv4p6O9ygwSdnVWr7GFLBwg8fe8rsl1oQAT5Hjcrvs8CoTYMewCdJ/WjQ0ygoz YscTJ7WvzXSMXyXA0MMS56KEOwjPHS1Hxz6dlCkN/4RoZZQoOgAjGIjrKiBg87BtNQJV SH2pRXWfP5aoVEcG5Z89ZdU6kPBv9G7ynZeEHRktluazlrGKfRvQwrbf3JKQA+0krcHE HkCqREj1cg7eOd5BKdvG058vEIsfeMiEeCgkH2lzN6RwTaUi0B8875R8J47SVQ6J1AYd h9wg== X-Gm-Message-State: AOAM5332c4eR8MJHAx63dR4YoufeymaNGcYbiNF0YRziempAe9rPJXkj kGI+SXtbPRUermCV8klRsQlqovbkLamTtLBT9b5tgkDdxO2iYA== X-Google-Smtp-Source: ABdhPJxzXGZ/GVa0aF4g0Ge9H69nX90gAZ5zmqn7Ykc0EnjxGA2swwQoDmTZDhvc2I+6nFzM7izB7EaaQre2hCGSH3Q= X-Received: by 2002:a1f:2a42:: with SMTP id q63mr1259316vkq.13.1623706677258; Mon, 14 Jun 2021 14:37:57 -0700 (PDT) In-Reply-To: <83pmwq6oxc.fsf@gnu.org> 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:208520 Archived-At: I did a little digging and found that I can reproduce the issue under a debugger by constructing a named pipe with `mkfifo pipe.el` and feeding the script into it using `cat script.el >>pipe.el`. (Then I can execute `run --script pipe.el` under gdb to capture a backtrace at the problematic lseek call.) I identified one bug in the safe_to_load_version function. I think I've got a fix for it, but I'm not very familiar with the emacs codebase, especially when it comes to regression testing. (My patch against the emacs-27 branch is below.) I tested the fix locally with a small dummy script as input and it seems to work, but I can't be confident that there won't be deeper bugs. You're welcome to use it as a starting point for a more robust fix. >From c97e4b97af86023177417e49c46b19812b2f4caa Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Mon, 14 Jun 2021 17:30:11 -0400 Subject: [PATCH] Fix invocation with '--script /dev/stdin' * src/lread.c (safe_to_load_version): Check lseek errors. (Bug#48940) --- src/lread.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/lread.c b/src/lread.c index 47116ad5ae..96b6695b5c 100644 --- a/src/lread.c +++ b/src/lread.c @@ -990,12 +990,21 @@ #define UPDATE_BEG_END_STATE(ch) \ because of an incompatible change in the byte compiler. */ static int -safe_to_load_version (int fd) +safe_to_load_version (Lisp_Object file, int fd) { + struct stat st; char buf[512]; int nbytes, i; int version =3D 1; + /* If the file is not regular, then we cannot safely seek it. + Assume that it is not safe to load as a compiled file. */ + if (fstat(fd, &st) =3D=3D 0) + { + if (!S_ISREG (st.st_mode)) + return 0; + } + /* Read the first few bytes from the file, and look for a line specifying the byte compiler version used. */ nbytes =3D emacs_read_quit (fd, buf, sizeof buf); @@ -1013,7 +1022,9 @@ safe_to_load_version (int fd) version =3D 0; } - lseek (fd, 0, SEEK_SET); + if (lseek (fd, 0, SEEK_SET) < 0) + report_file_error ("Seeking to start of file", file); + return version; } @@ -1316,7 +1327,7 @@ DEFUN ("load", Fload, Sload, 1, 5, 0, if (is_elc /* version =3D 1 means the file is empty, in which case we can treat it as not byte-compiled. */ - || (fd >=3D 0 && (version =3D safe_to_load_version (fd)) > 1)) + || (fd >=3D 0 && (version =3D safe_to_load_version (file, fd)) > 1)) /* Load .elc files directly, but not when they are remote and have no handler! */ { @@ -1326,7 +1337,7 @@ DEFUN ("load", Fload, Sload, 1, 5, 0, int result; if (version < 0 - && ! (version =3D safe_to_load_version (fd))) + && ! (version =3D safe_to_load_version (file, fd))) { safe_p =3D 0; if (!load_dangerous_libraries) --=20 2.32.0.272.g935e593368-goog On Sun, Jun 13, 2021 at 6:26 AM Eli Zaretskii wrote: > > > Date: Wed, 9 Jun 2021 16:31:36 -0400 > > From: "Bryan C. Mills" via "Bug reports for GNU Emacs, > > the Swiss army knife of text editors" > > > > `emacs --script /dev/stdin` worked reliably for me on Emacs 24 through = 26. > > > > As of Emacs 27, it no longer works reliably =E2=80=94 depending on the = script contents, > > it either fails with spurious errors or silently exits without > > finishing the script. > > > > > > In the following bash interaction, all three emacs invocations > > *should* run the same script. > > The third invocation demonstrates the bug: when /dev/stdin is a pipe fr= om `cat` > > (instead of the script.el file directly), the script is no longer inter= preted. > > With other scripts, I get various errors, seemingly due to `;` or `"` > > characters being > > dropped or ignored from the input. > > > > ``` > > $ cat script.el | cat /dev/stdin > > (print "Hello emacs!") > > > > $ emacs --no-init-file --no-site-file --script script.el > > > > "Hello emacs!" > > > > $ emacs --no-init-file --no-site-file --script /dev/stdin > > > "Hello emacs!" > > > > $ cat script.el | emacs --no-init-file --no-site-file --script /dev/std= in > > > > $ echo $? > > 0 > > > > $ > > ``` > > > > This reproduces with the GNU Emacs 27.1 currently packaged on Debian Te= sting, > > as well as the Google build of GNU Emacs 27.2 from which I produced > > the report below. > > I see the problem, but it happens for me in Emacs 26 and Emacs 25 as > well, so I'm not sure this is new, or why it works for you in older > versions. Maybe Debian included some local patches in those older > versions? (My old Emacs versions were built from the unmodified > upstream sources.) > > AFAICS, the problem seems to be that load-with-code-conversion calls > insert-file-contents, and the latter comes up with an empty buffer in > the problematic case. > > It is notoriously hard to debug a program whose standard input was > redirected from a pipe of another program, so I couldn't see why the > above happens. If someone could step in a debugger through > insert-file-contents in this case and describe what's going on there, > or tell how to do that when Emacs is invoked like this, maybe we > could make some progress. > > Thanks.