Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] builtins: kill: Do not set signal on current shell when pid is empty



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



Messages sorted by: Reverse Date, Date, Thread, Author