Fixed bug 417833 which allowed for the creation of illegal
authorScott Thomas <lunchtimemama@gmail.com>
Tue, 26 Aug 2008 01:51:50 +0000 (01:51 -0000)
committerScott Thomas <lunchtimemama@gmail.com>
Tue, 26 Aug 2008 01:51:50 +0000 (01:51 -0000)
BitVector32.Sections. Also added a unit test for the bug.

svn path=/trunk/mcs/; revision=111591

mcs/class/System/System.Collections.Specialized/BitVector32.cs
mcs/class/System/System.Collections.Specialized/ChangeLog
mcs/class/System/Test/System.Collections.Specialized/BitVector32Test.cs
mcs/class/System/Test/System.Collections.Specialized/ChangeLog

index 11d059e796a4844fc4f636bf55463237755317a2..495664629d95a3b37f1988087c6b7a3302fb57be 100644 (file)
@@ -184,11 +184,11 @@ namespace System.Collections.Specialized {
                        if (maxValue < 1)
                                throw new ArgumentException ("maxValue");
                        
-                       int bit = HighestSetBit(maxValue) + 1;
+                       int bit = HighestSetBit(maxValue);
                        int mask = (1 << bit) - 1;
-                       int offset = previous.Offset + NumberOfSetBits (previous.Mask);
+                       int offset = previous.Offset + HighestSetBit (previous.Mask);
 
-                       if (offset > 32) {
+                       if (offset + bit > 32) {
                                throw new ArgumentException ("Sections cannot exceed 32 bits in total");
                        }
 
@@ -227,27 +227,12 @@ namespace System.Collections.Specialized {
                }
 
                // Private utilities
-               private static int NumberOfSetBits (int i) 
+               private static int HighestSetBit (int i) 
                {
                        int count = 0;
-                       for (int bit = 0; bit < 32; bit++) {
-                               int mask = 1 << bit;
-                               if ((i & mask) != 0) 
-                                       count++;
-                       }
+                       while(i >> count != 0)
+                               count++;
                        return count;
                }
-
-               private static int HighestSetBit (int i) 
-               {
-                       for (int bit = 31; bit >= 0; bit--) {
-                               int mask = 1 << bit;
-                               if ((mask & i) != 0) {
-                                       return bit;
-                               }
-                       }
-
-                       return -1;
-               }
        }
 }
index 2099b72b5452f8619c1045ef76a4c28a779323e3..313df48856f381b7cfb644d74c2f82d40026d9d2 100644 (file)
@@ -1,3 +1,10 @@
+2008-08-06  Scott Peterson  <lunchtimemama@gmail.com>
+
+       * BitVector32.cs: Fixed a bug which allowed for invalid sections
+       to be created with CreateSection. Also simplified HighestSetBit
+       algorithm and got rid ofNumberOfSetBits (using HighestSetBit
+       works just fine).
+
 2008-07-31  Jb Evain  <jbevain@novell.com>
 
        * StringDictionary.cs: remove ComponentModel bits for NET_2_1.
index cb699cbd1bf42cd561c32ac3815ad4a8ff0c598a..3adb631a36939d4b762ae76df98f7c16b343fc67 100644 (file)
@@ -173,6 +173,28 @@ namespace MonoTests.System.Collections.Specialized
                        Assert.AreEqual ("Section{0x1f, 0x0}", s2.ToString (), "ToString()");
                }
 
+               [Test]
+               public void SectionCorrectSize ()
+               {
+                       BitVector32.Section s1 = BitVector32.CreateSection (32767);
+                       BitVector32.Section s2 = BitVector32.CreateSection (32767, s1);
+                       BitVector32.Section s3 = BitVector32.CreateSection (3, s2);
+                       BitVector32 v1 = new BitVector32 (0);
+                       v1[s3] = 3;
+                       Assert.AreEqual (v1[s3], 3);
+               }
+
+               [Test]
+               public void SectionIncorrectSize ()
+               {
+                       BitVector32.Section s1 = BitVector32.CreateSection (32767);
+                       BitVector32.Section s2 = BitVector32.CreateSection (32767, s1);
+                       try {
+                               BitVector32.Section s3 = BitVector32.CreateSection (4, s2);
+                               Assert.Fail("Illegal section created");
+                       } catch (ArgumentException) {}
+               }
+
                 [Test]
                 public void NegativeIndexer ()
                 {
index 1e927248fae63c47151f2d218fb8661680191bd7..7a442b2ba83492d89cb18495e637779484339d67 100644 (file)
@@ -1,3 +1,8 @@
+2008-08-21  Scott Peterson  <lunchtimemama@gmail.com>
+
+       * BitVector32Test.cs: Added test to make sure illegal sections cannot
+         be created.
+
 2008-01-07  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * NameObjectCollectionBaseTest.cs: Add tests for argument checking in