about summary refs log tree commit diff
path: root/Src
diff options
context:
space:
mode:
authorChris Down <chris@chrisdown.name>2020-02-17 10:12:11 -0500
committerDaniel Shahaf <danielsh@apache.org>2020-02-18 12:59:53 +0000
commit5c55b3fb50bbfe602fcfa55fa6258e398ecc6b20 (patch)
tree87a6b9698d4fa95416a74ab7b745e8056e8e90dd /Src
parent59258252b501159c2988de3a2d87a18dc17e98b1 (diff)
downloadzsh-5c55b3fb50bbfe602fcfa55fa6258e398ecc6b20.tar.gz
zsh-5c55b3fb50bbfe602fcfa55fa6258e398ecc6b20.tar.xz
zsh-5c55b3fb50bbfe602fcfa55fa6258e398ecc6b20.zip
45453: builtins: kill: Do not signal current process group when pid is empty
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
(e.g. `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:

The regression test for `kill` without a sigspec is also included in
this commit, as previously it's not possible to test it trivially as it
would still kill the test runner in expected-to-fail mode; see
discussion in workers/45449.

0: workers/45426: https://www.zsh.org/mla/workers/2020/msg00251.html
1: The other call site 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.
Diffstat (limited to 'Src')
-rw-r--r--Src/jobs.c5
1 files changed, 3 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';