zsh-workers
 help / color / Atom feed
From: Chris Down <chris@chrisdown.name>
To: zsh-workers@zsh.org
Subject: [PATCH] builtins: kill: Do not set signal on current shell when pid is empty
Date: Fri, 14 Feb 2020 11:15:56 -0400
Message-ID: <20200214151556.GA624243@chrisdown.name> (raw)

As an example:

    % 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 be desirable behaviour to me. It
seems unintentional judging by the code and documentation, since it
comes about as a result of `isanum` returns true for empty strings
(since an empty string technically only consists of digits and
minuses...) and `atoi`, when passed a pointer to an invalid number,
returns 0.

With this patch:

    % trap 'exit 5' TERM
    % kill ''
    kill: empty pid provided
---
 Src/jobs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Src/jobs.c b/Src/jobs.c
index e7438251e..a64368e10 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -2745,6 +2745,9 @@ bin_kill(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))
 	} else if (!isanum(*argv)) {
 	    zwarnnam("kill", "illegal pid: %s", *argv);
 	    returnval++;
+	} else if ((*argv)[0] == '\0')  {
+	    zwarnnam("kill", "empty pid provided");
+	    returnval++;
 	} else {
 	    int pid = atoi(*argv);
 	    if (kill(pid, sig) == -1) {
-- 
2.25.0


             reply index

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-14 15:15 Chris Down [this message]
2020-02-14 16:02 ` Chris Down
2020-02-15 13:52 ` [PATCH ALTERNATE] builtins: kill: Do not set signal on current pgroup " Chris Down
2020-02-15 14:10   ` Daniel Shahaf
2020-02-15 15:15     ` Chris Down

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200214151556.GA624243@chrisdown.name \
    --to=chris@chrisdown.name \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git