Skip to content

Commit

Permalink
Check null values in Host Address (#3526)
Browse files Browse the repository at this point in the history
Add a null check for host addresses to avoid
NPE in getVersionFormattedHostAddress().
  • Loading branch information
chetangudisagar committed Feb 22, 2023
1 parent b1c4f1c commit 08f4586
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions common/src/main/java/org/corfudb/common/util/URLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ public static String getVersionFormattedEndpointURL(String address) {
*
* @param host host address that needs formatting
* @return version-formatted address
* @throws IllegalArgumentException if the parameter host is null or empty
*/
public static String getVersionFormattedHostAddress(String host) {

// In LR-SaaS, host could be null.
if (host == null || host.isEmpty()) {
throw new IllegalArgumentException("getVersionFormattedHostAddress: " +
"host is either null or empty.");
}

// getByName(host) fails when host has scope/interface in it like ([....%eth0])
// remove the trailing names %eth0 or %en0 if present
String formattedHost = host.trim().split("%")[0];
Expand Down
10 changes: 10 additions & 0 deletions common/src/test/java/org/corfudb/common/util/URLUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.net.InetSocketAddress;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -37,6 +38,7 @@ public class URLUtilsTest {
private static final String IPV6_ENDPOINT_URL_3_SHORT_MALFORMED = "::1:9000";
private static final String IPV6_ENDPOINT_URL_MALFORMED_1 = "0:0:0:0:0:0:0:1:9000";
private static final String IPV6_ENDPOINT_URL_MALFORMED_2 = "0:0:0:0:0:0:0:2:9001";
private static final String EMPTY_STRING = "";

/**
* Utility method to get a mock ChannelHandlerContext object.
Expand Down Expand Up @@ -107,6 +109,14 @@ void testGetVersionFormattedHostAddress() {
// Should return version-formatted IPv6 Address with '[' and ']'
assertThat(URLUtils.getVersionFormattedHostAddress(IPV6_ADDRESS_2_MALFORMED))
.isEqualTo(IPV6_ADDRESS_2_SHORT);

// Empty Host Address should throw IllegalArgumentException
assertThatThrownBy(() -> URLUtils.getVersionFormattedHostAddress(EMPTY_STRING)).
isInstanceOf(IllegalArgumentException.class);

// null Host Address should throw IllegalArgumentException
assertThatThrownBy(() -> URLUtils.getVersionFormattedHostAddress(null)).
isInstanceOf(IllegalArgumentException.class);
}

/**
Expand Down

0 comments on commit 08f4586

Please sign in to comment.