[m-rev.] diff: fix version_array error checking

Simon Taylor staylr at gmail.com
Mon May 28 20:00:14 AEST 2007


Estimated hours taken: 0.5
Branches: main

library/version_array.m:
	Fix a bug which caused negative indices to be allowed
	when updating a version_array.

tests/hard_coded/version_array_test.{m,exp}:
	Add test cases.


Index: library/version_array.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/version_array.m,v
retrieving revision 1.14
diff -u -u -r1.14 version_array.m
--- library/version_array.m	13 Feb 2007 01:58:55 -0000	1.14
+++ library/version_array.m	28 May 2007 01:00:47 -0000
@@ -484,7 +484,7 @@
 
         VA1->rest.array->elements[I] = X;
     } else {
-        if (I >= ML_va_size(VA0)) {
+        if (I < 0 || I >= ML_va_size(VA0)) {
             return MR_FALSE;
         }
 
Index: tests/hard_coded/version_array_test.m
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/version_array_test.m,v
retrieving revision 1.2
diff -u -u -r1.2 version_array_test.m
--- tests/hard_coded/version_array_test.m	13 Feb 2007 01:59:03 -0000	1.2
+++ tests/hard_coded/version_array_test.m	28 May 2007 01:00:02 -0000
@@ -14,14 +14,14 @@
 
 
 
-:- pred main(io :: di, io :: uo) is det.
+:- pred main(io :: di, io :: uo) is cc_multi.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
 :- implementation.
 
-:- import_module int, list, string, version_array.
+:- import_module exception, int, list, string, univ, version_array.
 
 %-----------------------------------------------------------------------------%
 
@@ -66,7 +66,47 @@
     A7 = copy(A5),
     io.write_list(to_list(A7), ", ", io.write_int, !IO),
     io.format(" (size %d)\n", [i(size(A7))], !IO),
-    true.
+
+    test_exception(
+        ((pred) is semidet :-
+            _ = A7 ^ elem(-1)
+        ), !IO),
+    test_exception(
+        ((pred) is semidet :-
+            _ : version_array(int) = A7 ^ elem(-1) := 2
+        ), !IO),
+    _ = A7 ^ elem(1) := 1,
+    test_exception(
+        ((pred) is semidet :-
+            _ = A7 ^ elem(-1) := 2
+        ), !IO),
+    test_exception(
+        ((pred) is semidet :-
+            _ = A7 ^ elem(-1)
+        ), !IO),
+    test_exception(
+        ((pred) is semidet :-
+            _ = A7 ^ elem(4)
+        ), !IO),
+    true. 
+
+:- pred test_exception((pred)::in((pred) is semidet),
+    io::di, io::uo) is cc_multi.
+
+test_exception(Pred, !IO) :-
+    try((pred({}::out) is semidet :- Pred), Result),
+    (
+        Result = succeeded(_),
+        io.write_string("Error: test succeeded, expected exception\n", !IO)
+    ;
+        Result = failed,
+        io.write_string("Error: test failed, expected exception\n", !IO)
+    ;    
+        Result = exception(Exception),
+        io.write_string("Found exception as expected: ", !IO),
+        io.write(univ_value(Exception), !IO),
+        io.nl(!IO)
+    ).
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
Index: tests/hard_coded/version_array_test.exp
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/version_array_test.exp,v
retrieving revision 1.2
diff -u -u -r1.2 version_array_test.exp
--- tests/hard_coded/version_array_test.exp	13 Feb 2007 01:59:03 -0000	1.2
+++ tests/hard_coded/version_array_test.exp	28 May 2007 01:02:45 -0000
@@ -12,3 +12,8 @@
 7, 7, 7, 7, 9, 9, 9, 9, 9 (size 9)
  (sum 73)
 7, 7, 7, 7 (size 4)
+Found exception as expected: software_error("version_array.elem: index out of range")
+Found exception as expected: software_error("version_array.\'elem :=\': index out of range")
+Found exception as expected: software_error("version_array.\'elem :=\': index out of range")
+Found exception as expected: software_error("version_array.elem: index out of range")
+Found exception as expected: software_error("version_array.elem: index out of range")
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list