Bug 487551 - nsTArray.IndexOf may scan beyond array bounds for non-zero start argument. unit test that fails without patch, succeeds with patch. r=bsmedberg
authorAndrew Sutherland <asutherland@asutherland.org>
Fri, 10 Apr 2009 18:08:08 -0700
changeset 27204 730004d7878c2cbbf75b94ebb59ece5a1448b8ce
parent 27203 ed0e7eabef8f1773629d7e02ac829ee041df390b
child 27205 3b57ad0d0ed83db8b3562425566f89153d352e7b
push idunknown
push userunknown
push dateunknown
reviewersbsmedberg
bugs487551
milestone1.9.2a1pre
Bug 487551 - nsTArray.IndexOf may scan beyond array bounds for non-zero start argument. unit test that fails without patch, succeeds with patch. r=bsmedberg
xpcom/tests/TestTArray.cpp
--- a/xpcom/tests/TestTArray.cpp
+++ b/xpcom/tests/TestTArray.cpp
@@ -493,16 +493,31 @@ static PRBool test_autoarray() {
   }
 
   return PR_TRUE;
 }
 #endif
 
 //----
 
+// IndexOf used to potentially scan beyond the end of the array.  Test for
+// this incorrect behavior by adding a value (5), removing it, then seeing
+// if IndexOf finds it.
+static PRBool test_indexof() {
+  nsTArray<int> array;
+  array.AppendElement(0);
+  // add and remove the 5
+  array.AppendElement(5);
+  array.RemoveElementAt(1);
+  // we should not find the 5!
+  return array.IndexOf(5, 1) == -1;
+}
+
+//----
+
 typedef PRBool (*TestFunc)();
 #define DECL_TEST(name) { #name, name }
 
 static const struct Test {
   const char* name;
   TestFunc    func;
 } tests[] = {
   DECL_TEST(test_int_array),
@@ -512,16 +527,17 @@ static const struct Test {
   DECL_TEST(test_object_array),
   DECL_TEST(test_string_array),
   DECL_TEST(test_comptr_array),
   DECL_TEST(test_refptr_array),
   DECL_TEST(test_ptrarray),
 #ifdef DEBUG
   DECL_TEST(test_autoarray),
 #endif
+  DECL_TEST(test_indexof),
   { nsnull, nsnull }
 };
 
 }
 
 using namespace TestTArray;
 
 int main(int argc, char **argv) {