summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2008-08-31 13:35:28 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2008-08-31 13:35:28 +0000
commit50e4b4336299a09024f1e8361861ca939859279b (patch)
treea3b40beadc81040c8403164c6e85b6f9dbaf92b7
parentefed1e6c10daba9ea4260845a91f518f573ec1d3 (diff)
downloadzsh-50e4b4336299a09024f1e8361861ca939859279b.tar.gz
zsh-50e4b4336299a09024f1e8361861ca939859279b.tar.xz
zsh-50e4b4336299a09024f1e8361861ca939859279b.zip
25568: Frank Terbeck & pws: invalid continue and break arguments
also tests for control commands
-rw-r--r--ChangeLog5
-rw-r--r--Src/builtin.c5
-rw-r--r--Test/.distfiles1
-rw-r--r--Test/A07control.ztst112
4 files changed, 123 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index c65e2fed7..d62d2fe62 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2008-08-31  Peter Stephenson  <p.w.stephenson@ntlworld.com>
 
+	* 25568: Frank Terbeck & pws: Src/builtin.c, Test/.distfiles,
+	Test/A07control.ztst: break and continue arguments less than
+	1 are invalid (and hard errors); add some tests for control
+	commands.
+
 	* users/13162: Completion/Unix/Command/_module: strip
 	"~" suffix from modules.
 
diff --git a/Src/builtin.c b/Src/builtin.c
index ee44b3776..654665bfc 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -4443,6 +4443,11 @@ bin_break(char *name, char **argv, UNUSED(Options ops), int func)
 	nump = 1;
     }
 
+    if (nump > 0 && (func == BIN_CONTINUE || func == BIN_BREAK) && num <= 0) {
+	zerrnam(name, "argument is not positive: %d", num);
+	return 1;
+    }
+
     switch (func) {
     case BIN_CONTINUE:
 	if (!loops) {   /* continue is only permitted in loops */
diff --git a/Test/.distfiles b/Test/.distfiles
index 53d2b2dd6..740e36d96 100644
--- a/Test/.distfiles
+++ b/Test/.distfiles
@@ -7,6 +7,7 @@ A03quoting.ztst
 A04redirect.ztst
 A05execution.ztst
 A06assign.ztst
+A07control.ztst
 B01cd.ztst
 B02typeset.ztst
 B03print.ztst
diff --git a/Test/A07control.ztst b/Test/A07control.ztst
new file mode 100644
index 000000000..b9b89b588
--- /dev/null
+++ b/Test/A07control.ztst
@@ -0,0 +1,112 @@
+# Test control commands for loops and functions.
+
+%test
+
+  fn3() { return $1; print Error }
+  fn2() { fn3 $1 }
+  fn() {
+    print start $1
+    fn2 $1
+    return
+    print Error
+  }
+  for val in -1 0 1 255; do
+    fn $val; print $?
+  done
+0:Passing of return values back through functions
+>start -1
+>-1
+>start 0
+>0
+>start 1
+>1
+>start 255
+>255
+
+  fn() {
+    continue
+  }
+  fn
+1:continue outside loop
+?fn:continue:1 not in while, until, select, or repeat loop
+
+  for outer in 0 1 2 3; do
+    print outer $outer
+    for inner in 0 1 2 3; do
+      print inner $inner
+      continue $(( (outer & 1) ? 2 : 1 ))
+      print error
+    done
+    print outer end
+  done
+0:continue with valid argument
+>outer 0
+>inner 0
+>inner 1
+>inner 2
+>inner 3
+>outer end
+>outer 1
+>inner 0
+>outer 2
+>inner 0
+>inner 1
+>inner 2
+>inner 3
+>outer end
+>outer 3
+>inner 0
+
+  for outer in 0 1; do
+    continue 0
+    print -- $outer got here, status $?
+  done
+1:continue error case 0
+?(eval):continue:2: argument is not positive: 0
+
+  for outer in 0 1; do
+    continue -1
+    print -- $outer got here, status $?
+  done
+1:continue error case -1
+?(eval):continue:2: argument is not positive: -1
+
+  fn() {
+    break
+  }
+  for outer in 0 1; do
+    print $outer
+    fn
+  done
+0:break from within function (this is a feature, I disovered)
+>0
+
+  for outer in 0 1 2 3; do
+    print outer $outer
+    for inner in 0 1 2 3; do
+      print inner $inner
+      break $(( (outer & 1) ? 2 : 1 ))
+      print error
+    done
+    print outer end
+  done
+0:break with valid argument
+>outer 0
+>inner 0
+>outer end
+>outer 1
+>inner 0
+
+  for outer in 0 1; do
+    break 0
+    print -- $outer got here, status $?
+  done
+1:break error case 0
+?(eval):break:2: argument is not positive: 0
+
+  for outer in 0 1; do
+    break -1
+    print -- $outer got here, status $?
+  done
+1:break error case -1
+?(eval):break:2: argument is not positive: -1