public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* libgcj/2357: HashMap does not handle initialCapacity=0 correctly
@ 2001-03-23  4:26 mark
  0 siblings, 0 replies; 2+ messages in thread
From: mark @ 2001-03-23  4:26 UTC (permalink / raw)
  To: gcc-gnats

>Number:         2357
>Category:       libgcj
>Synopsis:       HashMap does not handle initialCapacity=0 correctly
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Mar 23 04:26:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     mark@klomp.org
>Release:        gcc version 3.0 20010323 (prerelease)
>Organization:
>Environment:

>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.

import java.util.HashMap;

public class HashMapTest {
    public static void main(String args[]) {
        HashMap map = new HashMap(0);
        map.get(new Object());
    }
}

Exception in thread "main" java.lang.ArithmeticException: / by zero
   at 0x4016836d: _Jv_ThrowSignal (/usr/local/gcc/lib/libgcj.so.1)
   at 0x40168402: _Jv_ThrowSignal (/usr/local/gcc/lib/libgcj.so.1)
   at 0x401dc840: java.util.HashMap.hash(java.lang.Object) (/usr/local/gcc/lib/libgcj.so.1)
   at 0x401dc0a4: java.util.HashMap.get(java.lang.Object) (/usr/local/gcc/lib/libgcj.so.1)
   at 0x08048a82: HashMapTest.main(java.lang.String[]) (/tmp/HashMapTest.java:6)   at 0x4017f0a5: gnu.gcj.runtime.FirstThread.run() (/usr/local/gcc/lib/libgcj.so.1)
   at 0x40189d92: java.lang.Thread.run_(java.lang.Object) (/usr/local/gcc/lib/libgcj.so.1)
   at 0x402a2e68: _Jv_ThreadSetPriority(_Jv_Thread_t, int) (/usr/local/gcc/lib/libgcj.so.1)
   at 0x4045653d: GC_start_routine (/usr/local/gcc/lib/libgcjgc.so.1)
   at 0x4046fc75: pthread_detach (/lib/libpthread.so.0)
   at 0x40559eba: __clone (/lib/libc.so.6)
>How-To-Repeat:

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: libgcj/2357: HashMap does not handle initialCapacity=0 correctly
@ 2001-03-24  0:16 Bryce McKinlay
  0 siblings, 0 replies; 2+ messages in thread
From: Bryce McKinlay @ 2001-03-24  0:16 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR libgcj/2357; it has been noted by GNATS.

From: Bryce McKinlay <bryce@albatross.co.nz>
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  <bryce@albatross.co.nz>
 
 	* 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--
 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-03-24  0:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-23  4:26 libgcj/2357: HashMap does not handle initialCapacity=0 correctly mark
2001-03-24  0:16 Bryce McKinlay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).