Bug(s) inside IdentityHashMap.java

Martin Kahlert martin.kahlert@infineon.com
Thu Sep 27 07:22:00 GMT 2001


Hi!
I have some problems with IdentityHashMap.java:
First: Where is table.length set?
Then:
The file contains a lot of copies of the term 
h = Math.abs (2 * System.identityHashCode (key) % table.length);
this is the same as 
h = Math.abs ((2 * System.identityHashCode (key)) % table.length);
thus, h may be table.length - 1;
but in get() we ask for return table[h + 1]; 
which is an memory violation.
Furthermore, as i understand the code, h should be even, which it is not
(table[h] should be a key).
So i replaced all (x*y%z) by (x*(y%z)) which is more like i understand that,
but i continue to get segfaults, this times inside get:
(h = 1078781084) which seems a bit large ;-) perhaps table.length overflowed
integer? Tomorrow i will look into that.
Strange.
Here is the patch anyway:
2001年09月27日 Martin Kahlert <martin.kahlert@infineon.com>
	* java/util/IdentityHashMap.java(containsKey): Fix operator precedence bug
	 (get): Likewise
	 (put): Likewise
	 (remove): Likewise
*** IdentityHashMap.java.old	Thu Sep 27 15:55:55 2001
--- IdentityHashMap.java	Thu Sep 27 15:55:28 2001
***************
*** 103,109 ****
 
 public boolean containsKey (Object key)
 {
! int h = Math.abs (2 * System.identityHashCode (key) % table.length);
 int save = h;
 while (true)
 {
--- 103,109 ----
 
 public boolean containsKey (Object key)
 {
! int h = Math.abs (2 * (System.identityHashCode (key) % table.length));
 int save = h;
 while (true)
 {
***************
*** 174,180 ****
 
 public Object get (Object key)
 {
! int h = Math.abs (2 * System.identityHashCode (key) % table.length);
 int save = h;
 while (true)
 {
--- 174,180 ----
 
 public Object get (Object key)
 {
! int h = Math.abs (2 * (System.identityHashCode (key) % table.length));
 int save = h;
 while (true)
 {
***************
*** 230,236 ****
 
 public Object put (Object key, Object value)
 {
! // Rehash is the load factor is too high.
 if (size * 3 / 2 > table.length)
 {
 	Object[] old = table;
--- 230,236 ----
 
 public Object put (Object key, Object value)
 {
! // Rehash if the load factor is too high.
 if (size * 3 / 2 > table.length)
 {
 	Object[] old = table;
***************
*** 248,254 ****
 	 }
 }
 
! int h = Math.abs (2 * System.identityHashCode (key) % table.length);
 int save = h;
 int del = -1;
 while (true)
--- 248,254 ----
 	 }
 }
 
! int h = Math.abs (2 * (System.identityHashCode (key) % table.length));
 int save = h;
 int del = -1;
 while (true)
***************
*** 288,294 ****
 
 public Object remove (Object key)
 {
! int h = Math.abs (2 * System.identityHashCode (key) % table.length);
 int save = h;
 while (true)
 {
--- 288,294 ----
 
 public Object remove (Object key)
 {
! int h = Math.abs (2 * (System.identityHashCode (key) % table.length));
 int save = h;
 while (true)
 {
-- 
The early bird catches the worm. If you want something else for 
breakfast, get up later.


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /