Skip to content

Commit

Permalink
8282395: URL.openConnection can throw IOOBE
Browse files Browse the repository at this point in the history
Reviewed-by: dfuchs
  • Loading branch information
tkiriyama authored and dfuch committed Oct 11, 2022
1 parent fe70487 commit 4435d56
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/java.base/share/classes/sun/net/www/ParseUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -197,11 +197,14 @@ public static String decode(String s) {
bb.clear();
int ui = i;
for (;;) {
assert (n - i >= 2);
if (n - i < 2) {
throw new IllegalArgumentException("Malformed escape pair: " + s);
}

try {
bb.put(unescape(s, i));
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
} catch (NumberFormatException | IndexOutOfBoundsException e) {
throw new IllegalArgumentException("Malformed escape pair: " + s);
}
i += 3;
if (i >= n)
Expand Down
13 changes: 11 additions & 2 deletions src/java.base/share/classes/sun/net/www/protocol/ftp/Handler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,6 +30,7 @@
package sun.net.www.protocol.ftp;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.Proxy;
import java.util.Map;
Expand Down Expand Up @@ -58,6 +59,14 @@ protected java.net.URLConnection openConnection(URL u)

protected java.net.URLConnection openConnection(URL u, Proxy p)
throws IOException {
return new FtpURLConnection(u, p);
FtpURLConnection connection = null;
try {
connection = new FtpURLConnection(u, p);
} catch (IllegalArgumentException e) {
var mfue = new MalformedURLException(e.getMessage());
mfue.initCause(e);
throw mfue;
}
return connection;
}
}
57 changes: 57 additions & 0 deletions test/jdk/java/net/URL/B8282395.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @summary URL.openConnection can throw IOOBE
* @bug 8282395
*/

import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;

public class B8282395 {
public static void main(String[] args) throws Exception {
boolean res = false;
URL url = new URL("ftp://.:%@");
try {
// Will throw IndexOutOfBoundsException if not fixed
url.openConnection();
} catch (MalformedURLException e) {
res = true;
}
if (!res) {
throw new RuntimeException("MalformedURLException should be thrown");
}
res = false;
try {
// Will throw IndexOutOfBoundsException if not fixed
url.openConnection(Proxy.NO_PROXY);
} catch (MalformedURLException e) {
res = true;
}
if (!res) {
throw new RuntimeException("MalformedURLException should be thrown");
}
}

}

1 comment on commit 4435d56

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.