sh: Apply set -u to variables in arithmetic.
authorPeter Avalos <pavalos@dragonflybsd.org>
Sun, 3 Jul 2011 00:07:08 +0000 (14:07 -1000)
committerPeter Avalos <pavalos@dragonflybsd.org>
Sun, 21 Aug 2011 19:10:25 +0000 (12:10 -0700)
Note that this only applies to variables that are actually used.
Things like (0 && unsetvar) do not cause an error.

Obtained-from:   FreeBSD SVN rev 221463

bin/sh/arith_yacc.c
tools/regression/bin/sh/expansion/set-u3.0 [new file with mode: 0644]

index dd8a373..1dd364c 100644 (file)
@@ -31,7 +31,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: src/bin/sh/arith_yacc.c,v 1.3 2011/03/05 13:27:13 jilles Exp $
+ * $FreeBSD: src/bin/sh/arith_yacc.c,v 1.4 2011/05/04 22:12:22 jilles Exp $
  */
 
 #include <sys/limits.h>
@@ -98,6 +98,8 @@ arith_lookupvarint(char *varname)
        arith_t result;
 
        str = lookupvar(varname);
+       if (uflag && str == NULL)
+               yyerror("variable not set");
        if (str == NULL || *str == '\0')
                str = "0";
        errno = 0;
diff --git a/tools/regression/bin/sh/expansion/set-u3.0 b/tools/regression/bin/sh/expansion/set-u3.0
new file mode 100644 (file)
index 0000000..cc0bd1b
--- /dev/null
@@ -0,0 +1,6 @@
+# $FreeBSD: src/tools/regression/bin/sh/expansion/set-u3.0,v 1.1 2011/05/04 22:12:22 jilles Exp $
+
+set -u
+unset x
+v=$( (eval ': $((x))') 2>&1 >/dev/null)
+[ $? -ne 0 ] && [ -n "$v" ]