You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary D. Gregory (Jira)" <ji...@apache.org> on 2023/06/23 07:10:00 UTC

[jira] [Commented] (IO-805) FilenameUtils.isIPv4Address does not handle illegal integers

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

Gary D. Gregory commented on IO-805:
------------------------------------

Hello [~haozhong] 

Thank you for your report.

Would you please provide a PR on GitHub?

> 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)