zsh-workers
 help / color / mirror / code / Atom feed
From: Chris Down <chris@chrisdown.name>
To: zsh-workers@zsh.org
Cc: Daniel Shahaf <d.s@daniel.shahaf.name>
Subject: [PATCH v2 2/2] builtins: kill: Do not signal current process group when pid is empty
Date: Sat, 15 Feb 2020 20:21:09 -0400	[thread overview]
Message-ID: <aa611722f5b347dade24dc9e12aba06932e470e6.1581812420.git.chris@chrisdown.name> (raw)
In-Reply-To: <e65ea745a197e445818a2c5192af51f829f75f8e.1581812420.git.chris@chrisdown.name>

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


  reply	other threads:[~2020-02-16  0:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-16  0:20 [PATCH v2 1/2] builtins: kill: Add basic test suite Chris Down
2020-02-16  0:21 ` Chris Down [this message]
2020-02-16 10:39   ` [PATCH v2 2/2] builtins: kill: Do not signal current process group when pid is empty Daniel Shahaf
2020-02-17 14:11     ` Chris Down
2020-02-17 14:33       ` Daniel Shahaf
2020-02-16 10:34 ` [PATCH v2 1/2] builtins: kill: Add basic test suite Daniel Shahaf
2020-02-17 13:49   ` 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=aa611722f5b347dade24dc9e12aba06932e470e6.1581812420.git.chris@chrisdown.name \
    --to=chris@chrisdown.name \
    --cc=d.s@daniel.shahaf.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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).