[Mono-devel-list] [PATCH] Fix key duplication in Hashtable.PutImpl

Luca Barbieri lb at lb.ods.org
Wed Aug 13 23:09:40 EDT 2003


Currently Hashtable.PutImpl has an incorrect test which causes the key
chain search to terminate as soon as a free slot is found, causing key
duplication.

Here are a patch (against 0.25, but CVS is also affected) and a test:

--- mcs-0.25/class/corlib/System.Collections/Hashtable.cs~	2003-08-14 04:37:40.000000000 +0200
+++ mcs-0.25/class/corlib/System.Collections/Hashtable.cs	2003-08-14 04:54:31.390370635 +0200
@@ -603,7 +603,7 @@
 
 				if (entry.key == null ||
 				    (entry.key == KeyMarker.Removed
-				     && (entry.hashMix & CHAIN_MARKER)!= 0)) {
+				     && (entry.hashMix & CHAIN_MARKER) == 0)) {
 
 					if (freeIndx == -1)
 						freeIndx = indx;




	class NullHashCodeObj
	{
		int _val;

		public NullHashCodeObj(int val)
		{
			_val = val;
		}
		
		public override int GetHashCode()
		{
			return 0;
		}

		public override bool Equals (object o)
		{
			return _val == ((NullHashCodeObj)o)._val;
		}
	}
	
	[Test]
        [ExpectedException (typeof (ArgumentException))]
	public static int TestChain1()
	{
		Hashtable ht = new Hashtable();

		NullHashCodeObj to1 = new NullHashCodeObj(1);
		NullHashCodeObj to2 = new NullHashCodeObj(2);
		NullHashCodeObj to3 = new NullHashCodeObj(3);
		
		ht.Add(to1, 1);
		ht.Add(to2, 2);
		ht.Add(to3, 3);

		ht.Remove(to2);
		
		ht.Add(to3, 4);		
		return 0;
	}

-- 
Luca Barbieri <lb at lb.ods.org>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20030814/6da63d2b/attachment.bin 


More information about the Mono-devel-list mailing list