From 5948b4203de13f26825e3c5e7b92b84f494750a7 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Fri, 9 Feb 2024 21:11:50 +0000 Subject: [PATCH] getconf builtin: fix a bunch of missing and inaccurate values Regression test failure on NetBSD: test return begins at 2024-02-09+07:26:55 getconf: INT_MAX: unknown variable return.sh[256]: FAIL: return fails to warn for INT_MAX+1 (expected status 128 and *': out of range', got status 1 and '') The problem is that the getconf builtin (bound to the path /opt/ast/bin/getconf) did not return a value for INT_MAX, though it is listed in src/lib/libast/comp/comf.tab. Turns out that this problem exists everywhere, not just on NetBSD. But, on most systems, the problem is masked by the fact that the getconf builtin will defer to the external standard getconf command, which usually does know it. But the NetBSD one doesn't. I found other missing values, e.g. SHRT_MAX (but not INT_MIN or SHRT_MIN!). And some wrong ones as well, like on macOS or FreeBSD: $ /opt/ast/bin/getconf SSIZE_MAX 2147483647 $ /usr/bin/getconf SSIZE_MAX 9223372036854775807 After hours of trying to debug conf.sh, which generates the information used by the getconf builtin, I found that the way that the $tmp.v temporary file is uses makes no sense. It contains words that looks like identifiers, scanned from default values of entries in conf.tab. Each such identifier then gets a corresponding CONF_const_${identifier} variable set, which then (on line 1068 and on) stops any probe from happening. As a result, no value is detected, which is incorrect. (This also explains why INT_MIN works whereas INT_MAX doesn't.) On line 1065, we find two variables exempted: case ${conf_name} in LONG_MAX|SSIZE_MAX) x= ;; Emptying the 'x' variable causes the probe to happen for these two. Sure enough, removing this special-casing introduces the same problem for LONG_MAX as well: not found by the built-in. After experimenting I am seeing no reason why this probe should not simply always happen. This appears to be another case where removing code fixes a bug. src/lib/libast/comp/conf.sh: - Remove $tmp.v code that scans values for words looking like IDs and sets CONF_const_${ID} variables based on them. This should only happen for the actual IDs in conf.tab (first column), not their default values. - Remove special-casing. Always probe, even if a CONF_const_${ID} variable exists. src/lib/libast/comp/conf.tab: - Update FCHR_MAX (max file size) default from LONG_MAX to SSIZE_MAX to avoid the value changing after the above change. On macOS, the following are now corrected (verified with 'ksh -r' as restricted mode currently stops fallback to /usr/bin/getconf): * getconf CHARCLASS_NAME_MAX: was absent, now 14 * getconf INT_MAX: was absent, now 2147483647 * getconf LONG_BIT: was absent, now 64 * getconf NL_ARGMAX: was absent, now 9 * getconf NL_LANGMAX: was absent, now 14 * getconf NL_MSGMAX: was absent, now 32767 * getconf NL_NMAX: was absent, now 1 * getconf NL_SETMAX: was absent, now 255 * getconf NL_TEXTMAX: was absent, now 2048 * getconf NZERO: was absent, now 20 * getconf PTHREAD_DESTRUCTOR_ITERATIONS: was absent, now 4 * getconf PTHREAD_KEYS_MAX: was absent, now 512 * getconf SHRT_MAX: was absent, now 32767 * getconf SIZE_MAX: was 4294967295, now 18446744073709551615 * getconf SSIZE_MAX: was 2147483647, now 9223372036854775807 * getconf UID_MAX: was 60002, now 2147483647 * getconf UINT_MAX: was absent, now 4294967295 * getconf WORD_BIT: was absent, now 32 --- NEWS | 5 +++++ src/cmd/ksh93/include/version.h | 2 +- src/lib/libast/comp/conf.sh | 21 +++------------------ src/lib/libast/comp/conf.tab | 2 +- src/lib/libcmd/getconf.c | 2 +- 5 files changed, 11 insertions(+), 21 deletions(-) diff --git a/NEWS b/NEWS index 6aac022d8b26..f04b48599cb3 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,11 @@ This documents significant changes in the dev branch of ksh 93u+m. For full details, see the git log at: https://github.com/ksh93/ksh Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library. +2024-02-09: + +- Fixed multiple inaccurate or missing values in the /opt/ast/bin/getconf + path-bound built-in command. + 2024-02-08: - Fixed an init-time crash that may occur if standard error is on a terminal, diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index d7b3a55c5f4b..dd66e845384e 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -18,7 +18,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.1.0-alpha" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2024-02-08" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2024-02-09" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2024 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/lib/libast/comp/conf.sh b/src/lib/libast/comp/conf.sh index 060062a14cc6..dc97c9b9ad0e 100644 --- a/src/lib/libast/comp/conf.sh +++ b/src/lib/libast/comp/conf.sh @@ -2,7 +2,7 @@ # # # This software is part of the ast package # # Copyright (c) 1985-2011 AT&T Intellectual Property # -# Copyright (c) 2020-2023 Contributors to ksh 93u+m # +# Copyright (c) 2020-2024 Contributors to ksh 93u+m # # and is licensed under the # # Eclipse Public License, Version 2.0 # # # @@ -249,9 +249,6 @@ case $append$extra in esac ;; *) values=$values$sp$1 - case $1 in - $sym) echo "$1" >> $tmp.v ;; - esac ;; esac done @@ -799,8 +796,6 @@ do eval name=\"'$'CONF_name_$key\" done > $tmp.q sort -u < $tmp.q > $tmp.t mv $tmp.t $tmp.q -sort -u < $tmp.v > $tmp.t -mv $tmp.t $tmp.v case $debug in -d4) exit ;; esac @@ -862,7 +857,6 @@ case $verbose in 1) echo "$command: check macros/enums as static initializers" >&2 ;; esac defined $tmp.q -defined $tmp.v case $debug in -d5) exit ;; esac @@ -873,10 +867,6 @@ exec < $tmp.q while read line do eval CONF_const_${line}=1 done -exec < $tmp.v -while read line -do eval CONF_const_${line}=1 -done # mark the string literal values @@ -1061,13 +1051,8 @@ do eval name=\"'$'CONF_name_$key\" conf_limit=0 case $flags in *[Ll]*) d= - case ${conf_name} in - LONG_MAX|SSIZE_MAX) - x= - ;; - *) eval x='$'CONF_const_${conf_name} - ;; - esac + # always probe, even if a CONF_const_${conf_name} variable exists + x= case $x in '') for s in ${values} do case $s in diff --git a/src/lib/libast/comp/conf.tab b/src/lib/libast/comp/conf.tab index 4124752412ea..0d2430711b55 100644 --- a/src/lib/libast/comp/conf.tab +++ b/src/lib/libast/comp/conf.tab @@ -97,7 +97,7 @@ ENH_I18N XOPEN SC 1 FSUW EQUIV_CLASS_MAX C QQ 1 L EXEC_INTERPRETER_LENGTH C QQ 1 L EXPR_NEST_MAX POSIX SC 2 LMN 32 -FCHR_MAX SVID SC 1 LMU LONG_MAX 2147483647 +FCHR_MAX SVID SC 1 LMU SSIZE_MAX 2147483647 FILESIZEBITS POSIX PC 1 LMU (8*sizeof(off_t)) 32 FILE_LOCKING POSIX SC 1 FU FORT_DEV POSIX SC 2 CFUW diff --git a/src/lib/libcmd/getconf.c b/src/lib/libcmd/getconf.c index 6f16113fd6a9..44bc93dd623c 100644 --- a/src/lib/libcmd/getconf.c +++ b/src/lib/libcmd/getconf.c @@ -24,7 +24,7 @@ */ static const char usage[] = -"[-?\n@(#)$Id: getconf (ksh 93u+m) 2021-04-20 $\n]" +"[-?\n@(#)$Id: getconf (ksh 93u+m) 2024-02-09 $\n]" "[--catalog?" ERROR_CATALOG "]" "[+NAME?getconf - get configuration values]" "[+DESCRIPTION?\bgetconf\b displays the system configuration value for"