8332686: InetAddress.ofLiteral can throw StringIndexOutOfBoundsException

Reviewed-by: dfuchs, jpai
This commit is contained in:
Aleksei Efimov 2024-12-04 11:34:41 +00:00
parent 56d315da48
commit 0c7451ae5a
3 changed files with 21 additions and 3 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2024, 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
@ -554,6 +554,10 @@ class Inet6Address extends InetAddress {
*/
static InetAddress parseAddressString(String addressLiteral, boolean removeSqBrackets)
throws UnknownHostException {
// Empty strings are not parseable
if (addressLiteral.isEmpty()) {
return null;
}
// Remove trailing and leading square brackets if requested
if (removeSqBrackets && addressLiteral.charAt(0) == '[' &&
addressLiteral.length() > 2 &&

View file

@ -1618,6 +1618,9 @@ public sealed class InetAddress implements Serializable permits Inet4Address, In
*/
public static InetAddress ofLiteral(String ipAddressLiteral) {
Objects.requireNonNull(ipAddressLiteral);
if (ipAddressLiteral.isEmpty()) {
throw IPAddressUtil.invalidIpAddressLiteral(ipAddressLiteral);
}
InetAddress inetAddress;
try {
// First try to parse the input as an IPv4 address literal