>From 19c525b34cab7b409463c0112c33693df35b3658 Mon Sep 17 00:00:00 2001
From: Eric Desrochers <eric.desroch...@canonical.com>
Date: Thu, 6 Jul 2017 09:24:36 -0400
Subject: [PATCH] ksh segfaults in job_chksave after receiving SIGCHLD

Prior to this update, the compiler optimization dropped parts from the ksh job
locking mechanism from the binary code. As a consequence, ksh could terminate
unexpectedly with a segmentation fault after it received the SIGCHLD signal.
This update implements a fix to ensure the compiler does not drop parts of the
ksh mechanism and the crash no longer occurs.
---
 src/cmd/ksh93/include/jobs.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/cmd/ksh93/include/jobs.h b/src/cmd/ksh93/include/jobs.h
index b67b097..7717d35 100644
--- a/src/cmd/ksh93/include/jobs.h
+++ b/src/cmd/ksh93/include/jobs.h
@@ -149,15 +149,18 @@ extern struct jobs job;
 #define vmbusy()	0
 #endif
 
-#define job_lock()	(job.in_critical++)
+#define asoincint(p)  __sync_fetch_and_add(p,1)
+#define asodecint(p)  __sync_fetch_and_sub(p,1)
+
+#define job_lock()      asoincint(&job.in_critical)
 #define job_unlock()	\
 	do { \
 		int	sig; \
-		if (!--job.in_critical && (sig = job.savesig)) \
+		if (asodecint(&job.in_critical)==1 && (sig = job.savesig)) \
 		{ \
-			if (!job.in_critical++ && !vmbusy()) \
+			if (!asoincint(&job.in_critical) && !vmbusy()) \
 				job_reap(sig); \
-			job.in_critical--; \
+			asodecint(&job.in_critical); \
 		} \
 	} while(0)
 
-- 
2.7.4

Reply via email to