From 98e46340867028808e71e7f3373881cb7e5b6764 Mon Sep 17 00:00:00 2001
From: Peter Stephenson
Date: Wed, 30 Mar 2022 09:28:43 +0100
Subject: 49906 (Bart), 49911: Fixes to querying jobs in subshell.
Don't attempt to query invalid job off end of table, resulting in
crashes from $jobtstates.
If background task started in subshell, look at tatsks within subshell
instead of main shell. Document and add test.
---
Src/jobs.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
(limited to 'Src/jobs.c')
diff --git a/Src/jobs.c b/Src/jobs.c
index 18e43f03c..af0a1233d 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -1718,8 +1718,15 @@ clearjobtab(int monitor)
/* Don't report any job we're part of */
if (thisjob != -1 && thisjob < oldmaxjob)
memset(oldjobtab+thisjob, 0, sizeof(struct job));
+
+ /* oldmaxjob is now the size of the table, but outside
+ * this function, it's used as a job number, which must
+ * be the largest index available in the table.
+ */
+ --oldmaxjob;
}
+
memset(jobtab, 0, jobtabsize * sizeof(struct job)); /* zero out table */
maxjob = 0;
@@ -1733,6 +1740,18 @@ clearjobtab(int monitor)
thisjob = initjob();
}
+/* In a subshell, decide we want our own job table after all. */
+
+/**/
+mod_export void
+clearoldjobtab(void)
+{
+ if (oldjobtab)
+ free(oldjobtab);
+ oldjobtab = NULL;
+ oldmaxjob = 0;
+}
+
static int initnewjob(int i)
{
jobtab[i].stat = STAT_INUSE;
@@ -2449,6 +2468,7 @@ bin_fg(char *name, char **argv, Options ops, int func)
case BIN_BG:
case BIN_WAIT:
if (func == BIN_BG) {
+ clearoldjobtab();
jobtab[job].stat |= STAT_NOSTTY;
jobtab[job].stat &= ~STAT_CURSH;
}
--
cgit 1.4.1