NetBSD Problem Report #56007

From woods@xentastic.weird.com  Mon Feb 22 23:03:39 2021
Return-Path: <woods@xentastic.weird.com>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 05BFC1A921F
	for <gnats-bugs@gnats.NetBSD.org>; Mon, 22 Feb 2021 23:03:38 +0000 (UTC)
Message-Id: <20210222230151.0F928511C6@xentastic.weird.com>
Date: Mon, 22 Feb 2021 15:01:50 -0800 (PST)
From: "Greg A. Woods" <woods@planix.ca>
Reply-To: "Greg A. Woods" <woods@planix.ca>
To: gnats-bugs@NetBSD.org
Subject: ksh unable to use ERR traps (probably since 2016/03/17 - i.e. 8.x and 9.x)
X-Send-Pr-Version: 3.95

>Number:         56007
>Category:       bin
>Synopsis:       ksh unable to execute ERR traps (probably since 2016/03/17 - i.e. 8.x and 9.x)
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    bin-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Feb 22 23:05:00 +0000 2021
>Closed-Date:    Mon Sep 20 03:25:26 +0000 2021
>Last-Modified:  Mon Sep 20 03:25:26 +0000 2021
>Originator:     Greg A. Woods
>Release:        NetBSD 9.99.64
>Organization:
Planix, Inc.; Kelowna, BC; Canada
>Environment:
System: NetBSD xentastic 9.99.64 NetBSD 9.99.64 (GENERIC) #11: Sat Jul 4 16:12:53 PDT 2020 woods@xentastic:/build/woods/xentastic/current-amd64-amd64-obj/build/src-current/sys/arch/amd64/compile/GENERIC amd64
Architecture: x86_64
Machine: amd64
>Description:

	N.B.:  This bug also affects pdksh-5.2.14nb7 from pkgsrc.

	Quite some time ago I noticed ksh wasn't executing ERR traps my
	more recent netbsd-current and releases.  I notice this almost
	immediately as my default login shell uses "trap ERR" to report
	any non-zero exit code from all interactive commands.

	Finally today I did some debugging to find out why (as I intend
	to put a more recent build into production).

	It seems that somehow an incomplete/broken change crept in
	between the changes to try to remove and re-add the code in the
	signal list generating script that's needed to properly expand
	and sort the signal list array entries:

----------------------------
revision 1.11
date: 2016-03-17 06:54:31 -0700;  author: christos;  state: Exp;  lines: +36 -4;
put back the complex sed/awk since the code can't handle unsorted or repeated
entries (Rin Okuyama)
----------------------------
revision 1.10
date: 2016-03-16 16:01:33 -0700;  author: christos;  state: Exp;  lines: +5 -35;
We don't need all this magic to build the signals lists. Do the work at
compile time.
----------------------------

	The errant changes seem to have come from the fix provided in
	the following email:

https://mail-index.netbsd.org/current-users/2016/03/17/msg029042.html

	As it turns out the "DUMMY" value that should be at index
	"SIGNALS" is not present at all in the intermediate data.  This
	means all the unused entries between the last valid one and the
	end of the table are missing, and thus given how the generated
	list is included in the static initialisation of the whole list,
	the final value used for "SIGERR_" is at the wrong index and
	thus can never be found.

>How-To-Repeat:

	First a working example:

$ uname -a
NetBSD more 5.2_STABLE NetBSD 5.2_STABLE (XEN3_DOMU) #0: Sat Feb 14 19:21:26 PST 2015  woods@more:/build/woods/more/netbsd-5-amd64-amd64-obj/once/rest/work/woods/m-NetBSD-5/sys/arch/amd64/compile/XEN3_DOMU amd64
$ trap
trap -- '. ${HOME}/.kshlogout ; exit $?' EXIT
trap -- 'trap 1; clearban; kill -1 6340' HUP
trap -- 'trap 2; clearban; kill -2 6340' INT
trap -- 'trap 3; clearban; kill -3 6340' QUIT
trap -- 'trap 15; clearban; kill -15 6340' TERM
trap -- '
        rc=$?;
        if ((ERRNO > 0)); then
                EMSG="; errno: $ERRNO"
        else
                EMSG=""
        fi;
        print "${0#-}: exit code: $rc$EMSG"
' ERR
$ false
ksh: exit code: 1
$ 

	Now the broken version (note the "ksh: exit code: 1" message is
	missing after "false" is executed):


$ uname -a
NetBSD xentastic 9.99.64 NetBSD 9.99.64 (GENERIC) #11: Sat Jul  4 16:12:53 PDT 2020  woods@xentastic:/build/woods/xentastic/current-amd64-amd64-obj/build/src-current/sys/arch/amd64/compile/GENERIC amd64
$ trap
trap -- '. ${HOME}/.kshlogout ; exit $?' EXIT
trap -- '
        rc=$?;
        if ((ERRNO > 0)); then
                EMSG="; errno: $ERRNO"
        else
                EMSG=""
        fi;
        print "${0#-}: exit code: $rc$EMSG"
' ERR
$ false
$ 

>Fix:

	The following changes fix the problem, clean up some ugliness in
	the generated output, and hopefully add a wee bit of
	future-proofing for changes in CPP output.

Index: bin/ksh/siglist.sh
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/bin/ksh/siglist.sh,v
retrieving revision 1.12
diff -u -r1.12 siglist.sh
--- bin/ksh/siglist.sh	17 Mar 2016 13:59:02 -0000	1.12
+++ bin/ksh/siglist.sh	22 Feb 2021 22:20:38 -0000
@@ -21,22 +21,23 @@
 # The trap here to make up for a bug in bash (1.14.3(1)) that calls the trap
 (trap $trapsigs;
  echo '#include "sh.h"';
- echo '	{ QwErTy SIGNALS , "DUMMY" , "hook for number of signals" },';
+ echo ' { QwErTy /* dummy for sed sillies */ },';
  ${SED} -e '/^[	 ]*#/d' -e 's/^[	 ]*\([^ 	][^ 	]*\)[	 ][	 ]*\(.*[^ 	]\)[ 	]*$/#ifdef SIG\1\
 	{ QwErTy .signal = SIG\1 , .name = "\1", .mess = "\2" },\
 #endif/') > $in
-# work around for gcc 5
+echo '	{ QwErTy .signal = SIGNALS , .name = "DUMMY", .mess = "hook to expand array to total signals" },' >> $in
+# work around for gcc > 5
 $CPP $in | grep -v '^#' | tr -d '\n' | ${SED} 's/},/},\
 /g' > $out
 ${SED} -n 's/{ QwErTy/{/p' < $out | ${AWK} '{print NR, $0}' | sort -k 5n -k 1n |
-    ${SED} 's/^[0-9]* //' |
-    ${AWK} 'BEGIN { last=0; nsigs=0; }
+    ${SED} -E -e 's/^[0-9]* //' -e 's/ +/ /' |
+    ${AWK} 'BEGIN { last=0; }
 	{
 	    if ($4 ~ /^[0-9][0-9]*$/ && $5 == ",") {
 		n = $4;
 		if (n > 0 && n != last) {
 		    while (++last < n) {
-			printf "\t{ .signal = %d , .name = NULL, .mess = `Signal %d` } ,\n", last, last;
+			printf " { .signal = %d , .name = NULL, .mess = `Signal %d` } ,\n", last, last;
 		    }
 		    print;
 		}

>Release-Note:

>Audit-Trail:
From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56007 CVS commit: src/bin/ksh
Date: Mon, 22 Feb 2021 20:31:30 -0500

 Module Name:	src
 Committed By:	christos
 Date:		Tue Feb 23 01:31:30 UTC 2021

 Modified Files:
 	src/bin/ksh: siglist.sh

 Log Message:
 PR/56007: Greg A. Woods: ksh unable to execute ERR traps
 (probably since 2016/03/17 - i.e. 8.x and 9.x)


 To generate a diff of this commit:
 cvs rdiff -u -r1.12 -r1.13 src/bin/ksh/siglist.sh

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Martin Husemann" <martin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56007 CVS commit: [netbsd-9] src/bin/ksh
Date: Tue, 23 Feb 2021 18:53:31 +0000

 Module Name:	src
 Committed By:	martin
 Date:		Tue Feb 23 18:53:31 UTC 2021

 Modified Files:
 	src/bin/ksh [netbsd-9]: siglist.sh

 Log Message:
 Pull up following revision(s) (requested by christos in ticket #1212):

 	bin/ksh/siglist.sh: revision 1.13

 PR/56007: Greg A. Woods: ksh unable to execute ERR traps
 (probably since 2016/03/17 - i.e. 8.x and 9.x)


 To generate a diff of this commit:
 cvs rdiff -u -r1.12 -r1.12.18.1 src/bin/ksh/siglist.sh

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Martin Husemann" <martin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56007 CVS commit: [netbsd-8] src/bin/ksh
Date: Tue, 23 Feb 2021 18:56:12 +0000

 Module Name:	src
 Committed By:	martin
 Date:		Tue Feb 23 18:56:12 UTC 2021

 Modified Files:
 	src/bin/ksh [netbsd-8]: siglist.sh

 Log Message:
 Pull up following revision(s) (requested by christos in ticket #1658):

 	bin/ksh/siglist.sh: revision 1.13

 PR/56007: Greg A. Woods: ksh unable to execute ERR traps
 (probably since 2016/03/17 - i.e. 8.x and 9.x)


 To generate a diff of this commit:
 cvs rdiff -u -r1.12 -r1.12.8.1 src/bin/ksh/siglist.sh

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->closed
State-Changed-By: kre@NetBSD.org
State-Changed-When: Mon, 20 Sep 2021 03:25:26 +0000
State-Changed-Why:
Problem fixed


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.