[ 
https://issues.apache.org/jira/browse/IO-805?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17737395#comment-17737395
 ] 

Hao Zhong edited comment on IO-805 at 6/27/23 12:37 AM:
--------------------------------------------------------

Many thanks for pointing out the regex. It is useful to avoid the exception, 
but it can to be too strict. Some legal ipv4 addresses can be determined as 
invalid. The following article comes from IBM document:

[https://www.ibm.com/docs/en/ts3500-tape-library?topic=functionality-ipv4-ipv6-address-formats]

It says that  01 . 102 . 103 . 104 is a legal ipv4 address, but  the regex will 
reject it. After I remove the blanks, the  isIPv4Address method still 
determines that it is illegal, due to the following code:
{color:#7f0055}if{color}{color:#000000} 
({color}{color:#6a3e3e}ipSegment{color}{color:#000000}.length() > 1 && 
{color}{color:#6a3e3e}ipSegment{color}{color:#000000}.startsWith({color}{color:#2a00ff}"0"{color}{color:#000000}))
 {{color}

In addition, the wiki of ipv4 provides some corner cases, but the isIPv4Address 
method rejects them. For example, it says that "the address _127.65530_ is 
equivalent to {_}127.0.255.250{_}" and "the loopback address _127.0.0.1_ is 
commonly written as {_}127.1{_}". Both cases will be rejected by the regex.

Please refer to Address representations of the following page:

https://en.wikipedia.org/wiki/Internet_Protocol_version_4

 

 


was (Author: haozhong):
Many thanks for pointing out the regex. It is useful to avoid the exception, 
but it can to be too strict. Some legal ipv4 addresses can be determined as 
invalid. The following article comes from IBM document:

[https://www.ibm.com/docs/en/ts3500-tape-library?topic=functionality-ipv4-ipv6-address-formats]

It says that  01 . 102 . 103 . 104 is a legal ipv4 address, but  the regex will 
reject it. After I remove the blanks, the  isIPv4Address method still 
determines that it is illegal, due to the following code:
{color:#7f0055}if{color}{color:#000000} 
({color}{color:#6a3e3e}ipSegment{color}{color:#000000}.length() > 1 && 
{color}{color:#6a3e3e}ipSegment{color}{color:#000000}.startsWith({color}{color:#2a00ff}"0"{color}{color:#000000}))
 {{color}

In addition, the wiki of ipv4 provides some corner cases, but the isIPv4Address 
method rejects them. For example, it says that "the address _127.65530_ is 
equivalent to {_}127.0.255.250{_}" and "the loopback address _127.0.0.1_ is 
commonly written as {_}127.1{_}". Both cases will be rejected by the regex.

 

> FilenameUtils.isIPv4Address does not handle illegal integers
> ------------------------------------------------------------
>
>                 Key: IO-805
>                 URL: https://issues.apache.org/jira/browse/IO-805
>             Project: Commons IO
>          Issue Type: Bug
>            Reporter: Hao Zhong
>            Priority: Major
>
> {code:java}
>  private static boolean isIPv4Address(final String name) {
>         final Matcher m = IPV4_PATTERN.matcher(name);
>         if (!m.matches() || m.groupCount() != 4) {
>             return false;
>         }        // verify that address subgroups are legal
>         for (int i = 1; i <= 4; i++) {
>             final String ipSegment = m.group(i);
>             final int iIpSegment = Integer.parseInt(ipSegment);
>             if (iIpSegment > IPV4_MAX_OCTET_VALUE) {
>                 return false;
>             }            if (ipSegment.length() > 1 && 
> ipSegment.startsWith("0")) {
>                 return false;
>             }        }        return true;
>     } {code}
> In the above code,  final int iIpSegment = Integer.parseInt(ipSegment) will 
> throw exceptions when ipSegment is illegal. 
>  
> The nearby isIPv6Address provides an example to handle the problem:
>  
> {code:java}
>   private static boolean isIPv6Address(final String inet6Address) {
>     ...
>      try {                    octetInt = Integer.parseInt(octet, BASE_16);    
>             } catch (final NumberFormatException e) {                    
> return false;                }
> }{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to