[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
NSMapTable mishandles overwriting values
From: |
Alexander Malmberg |
Subject: |
NSMapTable mishandles overwriting values |
Date: |
Thu, 07 Mar 2002 23:09:12 +0100 |
Hi,
NSMapTable doesn't properly handle the case where you call NSMapInsert()
on a key that already has a value. Specifically:
NSMapTable *mt;
id o=[Object class];
mt=NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks,
NSIntMapValueCallBacks,1);
NSMapInsert(mt,o,(void *)5);
NSMapInsert(mt,o,(void *)7);
printf("now %i\n",NSMapGet(mt,o));
NSMapRemove(mt,o);
printf("now %i\n",NSMapGet(mt,o)); /* should print '0', prints '5' */
I've attached a patch that fixes this. (It could be optimized to modify
the value in place with additional complexity.)
- Alexander Malmberg
Index: Source/NSMapTable.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/base/Source/NSMapTable.m,v
retrieving revision 1.27
diff -u -r1.27 NSMapTable.m
--- Source/NSMapTable.m 13 Feb 2002 22:25:38 -0000 1.27
+++ Source/NSMapTable.m 7 Mar 2002 21:59:53 -0000
@@ -367,6 +367,7 @@
NSMapInsert(NSMapTable *table, const void *key, const void *value)
{
GSIMapTable t = (GSIMapTable)table;
+ GSIMapNode n;
if (table == 0)
{
@@ -378,6 +379,9 @@
[NSException raise: NSInvalidArgumentException
format: @"Attempt to place notAKeyMarker in map table"];
}
+ n = GSIMapNodeForKey(t, (GSIMapKey)key);
+ if (n != 0)
+ GSIMapRemoveKey(t, (GSIMapKey)key);
GSIMapAddPair(t, (GSIMapKey)key, (GSIMapVal)value);
}
- NSMapTable mishandles overwriting values,
Alexander Malmberg <=