From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 6b979e5a for ; Sun, 16 Feb 2020 00:21:57 +0000 (UTC) Received: (qmail 11793 invoked by alias); 16 Feb 2020 00:21:45 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 45436 Received: (qmail 13468 invoked by uid 1010); 16 Feb 2020 00:21:45 -0000 X-Qmail-Scanner-Diagnostics: from mail-vk1-f194.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.1/25718. spamassassin: 3.4.2. Clear:RC:0(209.85.221.194):SA:0(-2.0/5.0):. Processed in 1.203927 secs); 16 Feb 2020 00:21:45 -0000 X-Envelope-From: chris@chrisdown.name X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 209.85.221.194 as permitted sender) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=Mslyj5UTkgWlF8BEA/N2g7Vq+/rI18Vd4PZ5XaEUyl0=; b=G+AlAcZJCNylMUgkF4m7PAaPCRaJo3dfJrQZNdj8j0m5PI0B5GjZwp2KqAyaNqV1G8 HXHqSwyEJDwV7hExn7UFWqS1OKEBc3EKIoITPGcUheNfzEwk5XO2L1cdDMwH34JK4xpm sadtAqSLKgF9GLliyQx7ikPmUBsLoMncvwZBT/TUJ9Lh43X/13XJGSaO1kPSeD13J+0D 2W+/D707CIPhO3DKoN/rCBxZSVmzNFvpcHHl5zYnzWzjqX4GCm1yAuTXbHctqMH6J5mq GyiAXUYnEdvz4IBGenyT4ySjM6hog4lZCML7s/JPYsiVoRQZuNfrf1/S9AKl4lfGiso2 V4QA== X-Gm-Message-State: APjAAAViEmdIabA9ESRa6X92n6v6k0NI6Z1D9YZbsPzdvE4uzUq9AYQP warb2C1R7q3xmfxqv7a6A/nz4U9wC/JmDA== X-Google-Smtp-Source: APXvYqyJqEtso5o81vNri4pUWtknAnv4EIZUYs1KQR5/VYTPMZH5S7arnyVERq/7DcH/vollf9tv1g== X-Received: by 2002:a1f:d583:: with SMTP id m125mr2748984vkg.17.1581812470861; Sat, 15 Feb 2020 16:21:10 -0800 (PST) Date: Sat, 15 Feb 2020 20:21:09 -0400 From: Chris Down To: zsh-workers@zsh.org Cc: Daniel Shahaf Subject: [PATCH v2 2/2] builtins: kill: Do not signal current process group when pid is empty Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: The following case was encountered in the wild: % zsh; echo "$?" % trap 'exit 5' TERM % kill '' 5 This behaviour seems more likely to be the result of bugs in programs (eg. `kill -9 "$unsetvar") rather than being desirable behaviour to me. It also seems unintentional judging by the code and documentation, since it comes about as a result of the fact that: - `isanum` returns true for empty strings (since an empty string technically only consists of digits and minuses...); - `atoi`, when passed a pointer to an invalid number, returns 0; - `kill(0, signal)` sends the signal in question to all processes in the current process group. There are (at least) two ways to solve this issue: 1. Add special handling to `kill` to avoid this case. See this patch[0] for a version that does that. 2. Change how isanum behaves. Since the only two call sites that use it both seem like they should handle the case where the input char array is empty, that seems like a reasonable overall change to me.[1] After this patch: % trap 'exit 5' TERM % kill '' kill: illegal pid: 0: https://www.zsh.org/mla/workers/2020/msg00251.html 1: The other callsite using isanum() is the fg builtin, but in that case we just fail later since we can't find any job named '', so no big deal either way. It's the kill case which is more concerning. --- Src/jobs.c | 5 +++-- Test/B11kill.ztst | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Src/jobs.c b/Src/jobs.c index e7438251e..0485f2c7c 100644 --- a/Src/jobs.c +++ b/Src/jobs.c @@ -1854,13 +1854,14 @@ scanjobs(void) /* This simple function indicates whether or not s may represent * * a number. It returns true iff s consists purely of digits and * - * minuses. Note that minus may appear more than once, and the empty * - * string will produce a `true' response. */ + * minuses. Note that minus may appear more than once. */ /**/ static int isanum(char *s) { + if (*s == '\0') + return 0; while (*s == '-' || idigit(*s)) s++; return *s == '\0'; diff --git a/Test/B11kill.ztst b/Test/B11kill.ztst index 6a548213b..69d4388c7 100644 --- a/Test/B11kill.ztst +++ b/Test/B11kill.ztst @@ -50,3 +50,15 @@ kill -INT 1:kill with sigspec only ?(eval):kill:1: not enough arguments + +# Regression tests: `kill ''` should not result in `kill 0`. + + trap 'exit 19' TERM + kill '' +1:Plain kill with empty pid should not send signal to current process group +?(eval):kill:2: illegal pid: + + trap 'exit 11' INT + kill -INT '' +1:kill with empty pid and sigspec should not send signal to current process group +?(eval):kill:2: illegal pid: -- 2.25.0