Bug 21074 - Trivial bug in the method getHeaderFieldKey() in the file java/net/protocol/http/HTTPURLConnection.java
Summary: Trivial bug in the method getHeaderFieldKey() in the file java/net/protocol/...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libgcj (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.0.2
Assignee: Tom Tromey
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-04-17 18:48 UTC by Goffredo Baroncelli
Modified: 2005-08-22 18:05 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-06-15 20:10:03


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Goffredo Baroncelli 2005-04-17 18:48:37 UTC
Hi, 
 
I found a little bug in the implementention of the method  
getHeaderFieldKey( int index ) in the file  
java/net/protocol/http/HTTPURLConnection.java. 
 
This method doesn't check if the parameter 'index' if out of range, so if it  
is called with a index value too high a java.util.NoSuchElementException is  
raised. 
Instead the java doc  
( http://java.sun.com/j2se/1.4.2/docs/api/java/net/HttpURLConnection.html#getHeaderFieldKey(int) )  
report that this method should return 'the value of the nth header field, or  
null if the value does not exist.' 
 
I suggest this patch to correct this beaviour; the patch implement the same  
control performed by the method getHeaderField( ). 
 
--- old/libjava/gnu/java/net/protocol/http/HTTPURLConnection.java       Sun  
Apr 17 11:10:59 2005 
+++ new/libjava/gnu/java/net/protocol/http/HTTPURLConnection.java       Sun  
Apr 17 11:13:53 2005 
@@ -543,6 +543,10 @@ 
     int count = 1; 
     do 
       { 
+        if (!i.hasNext()) 
+          { 
+            return null; 
+          } 
         entry = (Map.Entry) i.next(); 
         count++; 
       } 
 
 
Below are the test case used to highligh the problem, comparing the beaviour  
of the sun's java and the gnu one. The last case is a case with the patch 
applied 
 
[ghigo@therra tmp]$ cat testbed.java 
import java.net.*; 
 
public class testbed { 
 
        static public void main( String args[] ) throws Exception { 
 
                URL u=new URL(args[0]); 
                HttpURLConnection con = (HttpURLConnection)u.openConnection(); 
 
                int     n=1; 
                while(true){ 
                        String headerKey = con.getHeaderFieldKey(n); 
                        if(headerKey==null ){ 
                                System.out.print("con.getHeaderFieldKey( ) has  
returned null\n"); 
                                break; 
                        } 
                        System.out.print("headerKey: "+headerKey+"\n"); 
                        String headerVal = con.getHeaderField(n); 
                        System.out.print("headerVal: "+headerVal+"\n"); 
 
                        n++; 
                } 
        } 
 
} 
 
 
[ghigo@therra tmp]$ java -version    # Fedora core 4 test 2 
java version "1.4.2" 
gcj (GCC) 4.0.0 20050405 (Red Hat 4.0.0-0.40) 
Copyright (C) 2005 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions.  There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 
[ghigo@therra tmp]$ java -cp . testbed http://www.google.com 
headerKey: Cache-Control 
headerVal: private 
headerKey: Content-Type 
headerVal: text/html 
headerKey: Set-Cookie 
headerVal:  
PREF=ID=158312593220e9b1:LD=it:TM=1113727883:LM=1113727883:S=Y7zUs9KFT_BVs6d7;  
expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.it 
headerKey: Server 
headerVal: GWS/2.1 
headerKey: Transfer-Encoding 
headerVal: chunked 
headerKey: Date 
headerVal: Sun, 17 Apr 2005 08:51:23 GMT 
Exception in thread "main" java.util.NoSuchElementException 
   at java.util.LinkedHashMap$1.next() (/usr/lib/libgcj.so.6.0.0) 
   at gnu.java.net.protocol.http.HTTPURLConnection.getHeaderFieldKey(int)  
(/usr/lib/libgcj.so.6.0.0) 
   at testbed.main(java.lang.String[]) (Unknown Source) 
   at gnu.java.lang.MainThread.call_main() (/usr/lib/libgcj.so.6.0.0) 
   at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0) 
 
[ghigo@therra tmp]$ /usr/local/java-1.5/jdk1.5.0_01/bin/java -version  
java version "1.5.0_01" 
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08) 
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing) 
[ghigo@therra tmp]$ /usr/local/java-1.5/jdk1.5.0_01/bin/java -cp . testbed  
http://www.google.com 
headerKey: Cache-Control 
headerVal: private 
headerKey: Content-Type 
headerVal: text/html 
headerKey: Set-Cookie 
headerVal:  
PREF=ID=81a840411c05e572:LD=it:TM=1113727895:LM=1113727895:S=B5ZxkSzn6RkPyhGV;  
expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.it 
headerKey: Server 
headerVal: GWS/2.1 
headerKey: Transfer-Encoding 
headerVal: chunked 
headerKey: Date 
headerVal: Sun, 17 Apr 2005 08:51:35 GMT 
con.getHeaderFieldKey( ) has returned null 
 
The last example was executed with the patch applied to the java library 
 
[ghigo@therra tmp]$ /opt/gcc-4.0.0-20050410/bin/gcj-400 --version 
gcj-400 (GCC) 4.0.0 20050410 (prerelease) 
Copyright (C) 2005 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions.  There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
[ghigo@therra tmp]$ CLASSPATH=.:/opt/gcc-4.0.0-20050410/lib \ 
/opt/gcc-4.0.0-20050410/bin/gij-400 testbed http://www.google.com  
http://www.google.com 
headerKey: Cache-Control 
headerVal: private 
headerKey: Content-Type 
headerVal: text/html 
headerKey: Set-Cookie 
headerVal: 
PREF=ID=bb14539184e1692b:LD=it:TM=1113763098:LM=1113763098:S=DpuOWd-IfDkN2-tO; 
expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.it 
headerKey: Server 
headerVal: GWS/2.1 
headerKey: Transfer-Encoding 
headerVal: chunked 
headerKey: Date 
headerVal: Sun, 17 Apr 2005 18:38:18 GMT 
con.getHeaderFieldKey( ) has returned null 
 
 
Ciao 
Goffredo
Comment 1 GCC Commits 2005-06-15 20:08:35 UTC
Subject: Bug 21074

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	tromey@gcc.gnu.org	2005-06-15 20:08:28

Modified files:
	libjava        : ChangeLog 
	libjava/gnu/java/net/protocol/http: HTTPURLConnection.java 

Log message:
	2005-06-15  Goffredo Baroncelli  <kreijack@inwind.it>
	
	PR libgcj/21074:
	* gnu/java/net/protocol/http/HTTPURLConnection.java
	(getHeaderFieldKey): Check index.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/ChangeLog.diff?cvsroot=gcc&r1=1.3667&r2=1.3668
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/gnu/java/net/protocol/http/HTTPURLConnection.java.diff?cvsroot=gcc&r1=1.3&r2=1.4

Comment 2 Tom Tromey 2005-06-15 20:10:03 UTC
I checked this in on the trunk.
Once the branch status changes, I will check this in to 4.0 and close the PR.
Comment 3 Tom Tromey 2005-08-22 18:05:26 UTC
Committed to 4.0 branch.
Comment 4 GCC Commits 2005-08-22 18:06:46 UTC
Subject: Bug 21074

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	tromey@gcc.gnu.org	2005-08-22 18:06:36

Modified files:
	libjava        : ChangeLog 
	libjava/gnu/java/net/protocol/http: HTTPURLConnection.java 

Log message:
	2005-06-15  Goffredo Baroncelli  <kreijack@inwind.it>
	
	PR libgcj/21074:
	* gnu/java/net/protocol/http/HTTPURLConnection.java
	(getHeaderFieldKey): Check index.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.3391.2.92&r2=1.3391.2.93
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/gnu/java/net/protocol/http/HTTPURLConnection.java.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.1&r2=1.1.20.1