1*b30d1939SAndy Fiddaman#
2*b30d1939SAndy Fiddaman# CDDL HEADER START
3*b30d1939SAndy Fiddaman#
4*b30d1939SAndy Fiddaman# The contents of this file are subject to the terms of the
5*b30d1939SAndy Fiddaman# Common Development and Distribution License (the "License").
6*b30d1939SAndy Fiddaman# You may not use this file except in compliance with the License.
7*b30d1939SAndy Fiddaman#
8*b30d1939SAndy Fiddaman# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*b30d1939SAndy Fiddaman# or http://www.opensolaris.org/os/licensing.
10*b30d1939SAndy Fiddaman# See the License for the specific language governing permissions
11*b30d1939SAndy Fiddaman# and limitations under the License.
12*b30d1939SAndy Fiddaman#
13*b30d1939SAndy Fiddaman# When distributing Covered Code, include this CDDL HEADER in each
14*b30d1939SAndy Fiddaman# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*b30d1939SAndy Fiddaman# If applicable, add the following below this CDDL HEADER, with the
16*b30d1939SAndy Fiddaman# fields enclosed by brackets "[]" replaced with your own identifying
17*b30d1939SAndy Fiddaman# information: Portions Copyright [yyyy] [name of copyright owner]
18*b30d1939SAndy Fiddaman#
19*b30d1939SAndy Fiddaman# CDDL HEADER END
20*b30d1939SAndy Fiddaman#
21*b30d1939SAndy Fiddaman
22*b30d1939SAndy Fiddaman#
23*b30d1939SAndy Fiddaman# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24*b30d1939SAndy Fiddaman#
25*b30d1939SAndy Fiddaman
26*b30d1939SAndy Fiddaman#
27*b30d1939SAndy Fiddaman# This test checks whether ksh93 does not execute builtin command
28*b30d1939SAndy Fiddaman# "foo" when referencing variable "foo" when the variable is not
29*b30d1939SAndy Fiddaman# set (this applies to all builtin commands not bound to a
30*b30d1939SAndy Fiddaman# specific PATH element, e.g. "test", "sleep", "print" etc.).
31*b30d1939SAndy Fiddaman#
32*b30d1939SAndy Fiddaman# This was reported as CR #6848486 ('"echo ${test}" with test
33*b30d1939SAndy Fiddaman# undefined crashes the shell')
34*b30d1939SAndy Fiddaman# ------------ snip ------------
35*b30d1939SAndy Fiddaman# This is an odd one:
36*b30d1939SAndy Fiddaman#
37*b30d1939SAndy Fiddaman# $ ksh93 --version
38*b30d1939SAndy Fiddaman#   version         sh (AT&T Research) 93t 2008-11-04
39*b30d1939SAndy Fiddaman# $ ksh93
40*b30d1939SAndy Fiddaman# jl138328@gir:~$ echo $test
41*b30d1939SAndy Fiddaman#
42*b30d1939SAndy Fiddaman# jl138328@gir:~$ echo ${test}
43*b30d1939SAndy Fiddaman# Segmentation Fault (core dumped)
44*b30d1939SAndy Fiddaman# ------------ snip ------------
45*b30d1939SAndy Fiddaman#
46*b30d1939SAndy Fiddaman# The bug originates from the ksh93 "type system" which allows
47*b30d1939SAndy Fiddaman# an application to define it's own types in ksh93. In such cases
48*b30d1939SAndy Fiddaman# the output of function "mytype.len" is used when type "mytype"
49*b30d1939SAndy Fiddaman# has no member variable "len" (note it requires the use of
50*b30d1939SAndy Fiddaman# ${foo} since the use of $foo does not allow "foo" to contain
51*b30d1939SAndy Fiddaman# a dot in the variable name).
52*b30d1939SAndy Fiddaman# The implementation in ast-ksh.2009-11-04 however does this
53*b30d1939SAndy Fiddaman# for _all_ types of variables and not only for those which
54*b30d1939SAndy Fiddaman# are a member of an application-defined type, therefore
55*b30d1939SAndy Fiddaman# causing this bug.
56*b30d1939SAndy Fiddaman#
57*b30d1939SAndy Fiddaman
58*b30d1939SAndy Fiddaman# test setup
59*b30d1939SAndy Fiddamanfunction err_exit
60*b30d1939SAndy Fiddaman{
61*b30d1939SAndy Fiddaman	print -u2 -n "\t"
62*b30d1939SAndy Fiddaman	print -u2 -r ${Command}[$1]: "${@:2}"
63*b30d1939SAndy Fiddaman	(( Errors < 127 && Errors++ ))
64*b30d1939SAndy Fiddaman}
65*b30d1939SAndy Fiddamanalias err_exit='err_exit $LINENO'
66*b30d1939SAndy Fiddaman
67*b30d1939SAndy Fiddamanset -o nounset
68*b30d1939SAndy FiddamanCommand=${0##*/}
69*b30d1939SAndy Fiddamaninteger Errors=0
70*b30d1939SAndy Fiddaman
71*b30d1939SAndy Fiddaman
72*b30d1939SAndy Fiddaman# Test 1: Test whether the shell crashes when looking for an empty
73*b30d1939SAndy Fiddaman# "shell" variable.
74*b30d1939SAndy Fiddaman# (note: return code 78 was just picked randomly)
75*b30d1939SAndy Fiddaman$SHELL -c 'unset test ; print ${test} ; exit 78' >/dev/null 2>&1
76*b30d1939SAndy Fiddaman(( $? == 78 )) || err_exit "expected return code is 78, got $?"
77*b30d1939SAndy Fiddaman
78*b30d1939SAndy Fiddaman
79*b30d1939SAndy Fiddaman# Test 2: Test whether the shell can reach a point (which prints
80*b30d1939SAndy Fiddaman# "#mark") after the use of ${test} in the script.
81*b30d1939SAndy Fiddamanout=$($SHELL -o errexit -c 'unset test ; print ${test} ; print "#mark"' 2>&1 ) || err_exit "Shell returned error code $?, expected 0."
82*b30d1939SAndy Fiddaman[[ "$out" == $'\n#mark' ]] || err_exit "Expected output \$'\n#mark', got '${out}'"
83*b30d1939SAndy Fiddaman
84*b30d1939SAndy Fiddaman
85*b30d1939SAndy Fiddaman# Test 3: Check whether the use of ${sleep} returns nothing
86*b30d1939SAndy Fiddaman# (ast-ksh.2008-11-04 will return the usage string of the sleep
87*b30d1939SAndy Fiddaman# builtin)
88*b30d1939SAndy Fiddamanout=$($SHELL -o errexit -c 'print ${sleep} ; print "#mark"' 2>&1 ) || err_exit "Shell returned error code $?, expected 0."
89*b30d1939SAndy Fiddaman[[ "$out" == $'\n#mark' ]] || err_exit "Expected output \$'\n#mark', got '${out}'"
90*b30d1939SAndy Fiddaman
91*b30d1939SAndy Fiddaman
92*b30d1939SAndy Fiddaman# tests done
93*b30d1939SAndy Fiddamanexit $((Errors))
94