zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] builtins: kill: Do not set signal on current shell when pid is empty
@ 2020-02-14 15:15 Chris Down
  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
  0 siblings, 2 replies; 5+ messages in thread
From: Chris Down @ 2020-02-14 15:15 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] builtins: kill: Do not set signal on current shell when pid is empty
  2020-02-14 15:15 [PATCH] builtins: kill: Do not set signal on current shell when pid is empty Chris Down
@ 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
  1 sibling, 0 replies; 5+ messages in thread
From: Chris Down @ 2020-02-14 16:02 UTC (permalink / raw)
  To: zsh-workers

Alternatively, I can make isanum false if the string is empty. It looks like 
maybe that's desirable for all current call sites.

Let me know if you'd prefer that, and if you do I can send a patch to that 
effect.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH ALTERNATE] builtins: kill: Do not set signal on current pgroup when pid is empty
  2020-02-14 15:15 [PATCH] builtins: kill: Do not set signal on current shell when pid is empty Chris Down
  2020-02-14 16:02 ` Chris Down
@ 2020-02-15 13:52 ` Chris Down
  2020-02-15 14:10   ` Daniel Shahaf
  1 sibling, 1 reply; 5+ messages in thread
From: Chris Down @ 2020-02-15 13:52 UTC (permalink / raw)
  To: zsh-workers

This was first noticed in the `kill` builtin:

    % 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 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 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, but
   either works.

After this patch:

    % trap 'exit 5' TERM
    % kill ''
    kill: illegal pid:

0: https://www.zsh.org/mla/workers/2020/msg00251.html
---
 Src/jobs.c | 5 +++--
 1 file 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';
-- 
2.25.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH ALTERNATE] builtins: kill: Do not set signal on current pgroup when pid is empty
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Shahaf @ 2020-02-15 14:10 UTC (permalink / raw)
  To: Chris Down; +Cc: zsh-workers

Chris Down wrote on Sat, 15 Feb 2020 09:52 -0400:
> There are 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, but
>    either works.

Thanks for the patch and the revisions.  I prefer #2.  If no one
objects (or says POSIX requires «kill ''» to be equivalent to
«kill 0»…) I'll apply it.

It would be nice to have a regression test for this.
Test/C03traps.ztst has the examples, but the test should be added to
a B*.ztst file (probably a new one).  Would you happen to have time to
look into this?  No worries if not.

Cheers,

Daniel

> After this patch:
> 
>     % trap 'exit 5' TERM
>     % kill ''
>     kill: illegal pid:
> 
> 0: https://www.zsh.org/mla/workers/2020/msg00251.html
> ---
>  Src/jobs.c | 5 +++--
>  1 file 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';


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH ALTERNATE] builtins: kill: Do not set signal on current pgroup when pid is empty
  2020-02-15 14:10   ` Daniel Shahaf
@ 2020-02-15 15:15     ` Chris Down
  0 siblings, 0 replies; 5+ messages in thread
From: Chris Down @ 2020-02-15 15:15 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: zsh-workers

Daniel Shahaf writes:
>Chris Down wrote on Sat, 15 Feb 2020 09:52 -0400:
>> There are 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, but
>>    either works.
>
>Thanks for the patch and the revisions.  I prefer #2.  If no one
>objects (or says POSIX requires «kill ''» to be equivalent to
>«kill 0»…) I'll apply it.

Sure thing, thanks.

>It would be nice to have a regression test for this.
>Test/C03traps.ztst has the examples, but the test should be added to
>a B*.ztst file (probably a new one).  Would you happen to have time to
>look into this?  No worries if not.

No worries, I've got time. I'll send v2 in a little while with the updates.

Thanks for the review!

Chris

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-02-15 15:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-14 15:15 [PATCH] builtins: kill: Do not set signal on current shell when pid is empty Chris Down
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

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).