Module Name:    src
Committed By:   ast
Date:           Sun Oct  6 21:05:50 UTC 2013

Modified Files:
        src/bin/sh: expand.c
        src/tests/bin/sh: t_expand.sh

Log Message:
Fix PR bin/48202 [non-critical/low]:
  sh +nounset and `for X; do` iteration fails if parameter set empty
by applying and testing FreeBSD's patch of Oct 24 2009 for this; see
  http://svnweb.freebsd.org/base/head/bin/sh/expand.c?r1=198453&r2=198454
Also created an ATF test in tests/bin/sh/t_expand.sh for this error and
corrected a space->tabs problem there as well.


To generate a diff of this commit:
cvs rdiff -u -r1.89 -r1.90 src/bin/sh/expand.c
cvs rdiff -u -r1.1 -r1.2 src/tests/bin/sh/t_expand.sh

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

Modified files:

Index: src/bin/sh/expand.c
diff -u src/bin/sh/expand.c:1.89 src/bin/sh/expand.c:1.90
--- src/bin/sh/expand.c:1.89	Wed Oct  2 19:52:58 2013
+++ src/bin/sh/expand.c	Sun Oct  6 21:05:50 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: expand.c,v 1.89 2013/10/02 19:52:58 christos Exp $	*/
+/*	$NetBSD: expand.c,v 1.90 2013/10/06 21:05:50 ast Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)expand.c	8.5 (Berkeley) 5/15/95";
 #else
-__RCSID("$NetBSD: expand.c,v 1.89 2013/10/02 19:52:58 christos Exp $");
+__RCSID("$NetBSD: expand.c,v 1.90 2013/10/06 21:05:50 ast Exp $");
 #endif
 #endif /* not lint */
 
@@ -667,7 +667,7 @@ again: /* jump here after setting a vari
 	varlen = 0;
 	startloc = expdest - stackblock();
 
-	if (!set && uflag) {
+	if (!set && uflag && *var != '@' && *var != '*') {
 		switch (subtype) {
 		case VSNORMAL:
 		case VSTRIMLEFT:

Index: src/tests/bin/sh/t_expand.sh
diff -u src/tests/bin/sh/t_expand.sh:1.1 src/tests/bin/sh/t_expand.sh:1.2
--- src/tests/bin/sh/t_expand.sh:1.1	Sat Mar 17 16:33:11 2012
+++ src/tests/bin/sh/t_expand.sh	Sun Oct  6 21:05:50 2013
@@ -1,4 +1,4 @@
-# $NetBSD: t_expand.sh,v 1.1 2012/03/17 16:33:11 jruoho Exp $
+# $NetBSD: t_expand.sh,v 1.2 2013/10/06 21:05:50 ast Exp $
 #
 # Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -37,7 +37,7 @@ delim_argv() {
 		else
 			str="${str} >$1<"
 		fi
-                shift
+		shift
 	done
 	echo ${str}
 }
@@ -119,10 +119,24 @@ arithmetic_body() {
 	atf_check_equal '9223372036854775807' '$(((1 << 63) - 1))'
 }
 
+atf_test_case iteration_on_null_parameter
+iteration_on_null_parameter_head() {
+	atf_set "descr" "Check iteration of \$@ in for loop when set to null;" \
+	                "the error \"sh: @: parameter not set\" is incorrect." \
+	                "PR bin/48202."
+}
+iteration_on_null_parameter_body() {
+	s1=`/bin/sh -uc 'N=; set -- ${N};   for X; do echo "[$X]"; done' 2>&1`
+	s2=`/bin/sh -uc 'N=; set -- ${N:-}; for X; do echo "[$X]"; done' 2>&1`
+	atf_check_equal ''   '$s1'
+	atf_check_equal '[]' '$s2'
+}
+
 atf_init_test_cases() {
 	atf_add_test_case dollar_at
 	atf_add_test_case dollar_at_with_text
 	atf_add_test_case strip
 	atf_add_test_case varpattern_backslashes
 	atf_add_test_case arithmetic
+	atf_add_test_case iteration_on_null_parameter
 }

Reply via email to