From mboxrd@z Thu Jan 1 00:00:00 1970 From: Bryce McKinlay To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org Subject: Re: libgcj/2357: HashMap does not handle initialCapacity=0 correctly Date: Sat, 24 Mar 2001 00:16:00 -0000 Message-id: <20010324081601.4355.qmail@sourceware.cygnus.com> X-SW-Source: 2001-03/msg00248.html List-Id: The following reply was made to PR libgcj/2357; it has been noted by GNATS. From: Bryce McKinlay To: mark@klomp.org Cc: gcc-gnats@gcc.gnu.org, java-patches@gcc.gnu.org Subject: Re: libgcj/2357: HashMap does not handle initialCapacity=0 correctly Date: Sat, 24 Mar 2001 20:10:23 +1200 This is a multi-part message in MIME format. --------------6D752FBA07076294F4854D68 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit mark@klomp.org wrote: > >Description: > When the initialCapacity of a HashMap is zero then bucket.length will be zero and then you will get a ArithmetichException in the hash() method. I've checked in this fix. It seems better to simply bump the initialCapacity to 1 if 0 is given, than to add a special case in hash(), making it slower. regards [ bryce ] --------------6D752FBA07076294F4854D68 Content-Type: text/plain; charset=us-ascii; name="hashfix.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="hashfix.patch" 2001-03-24 Bryce McKinlay * java/util/HashMap.java (HashMap): If 0 is given for initialCapacity paramater, bump it to 1. * java/util/Hashtable.java (Hashtable): Likewise. Index: Hashtable.java =================================================================== RCS file: /cvs/gcc/egcs/libjava/java/util/Hashtable.java,v retrieving revision 1.10.4.1 diff -u -r1.10.4.1 Hashtable.java --- Hashtable.java 2001/02/22 04:26:06 1.10.4.1 +++ Hashtable.java 2001/03/24 08:00:18 @@ -179,7 +179,9 @@ + initialCapacity); if (loadFactor <= 0) throw new IllegalArgumentException("Illegal Load Factor: " + loadFactor); - + + if (initialCapacity == 0) + initialCapacity = 1; buckets = new Entry[initialCapacity]; this.loadFactor = loadFactor; this.threshold = (int) (initialCapacity * loadFactor); Index: HashMap.java =================================================================== RCS file: /cvs/gcc/egcs/libjava/java/util/HashMap.java,v retrieving revision 1.4.4.2 diff -u -r1.4.4.2 HashMap.java --- HashMap.java 2001/02/22 04:26:06 1.4.4.2 +++ HashMap.java 2001/03/24 08:00:18 @@ -157,7 +157,7 @@ * @param loadFactor the load factor * * @throws IllegalArgumentException if (initialCapacity < 0) || - * (initialLoadFactor > 1.0) || + * (loadFactor <= 0) */ public HashMap(int initialCapacity, float loadFactor) throws IllegalArgumentException @@ -167,7 +167,9 @@ + initialCapacity); if (loadFactor <= 0) throw new IllegalArgumentException("Illegal Load Factor: " + loadFactor); - + + if (initialCapacity == 0) + initialCapacity = 1; buckets = new Entry[initialCapacity]; this.loadFactor = loadFactor; this.threshold = (int) (initialCapacity * loadFactor); --------------6D752FBA07076294F4854D68--