Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should "01.01.01.01" be parsed as legal IPv4 ? #61186

Closed
huangjj27 opened this issue May 25, 2019 · 2 comments
Closed

Should "01.01.01.01" be parsed as legal IPv4 ? #61186

huangjj27 opened this issue May 25, 2019 · 2 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@huangjj27
Copy link

original problem: https://leetcode-cn.com/problems/validate-ip-address/
while std::net::IpAddr::parse successfully parse the address, but the leetcode problem doesn't regard the address as "legal",

test DEMO:

use std::net::IpAddr;

fn main() {
    if let Ok(IpAddr::V4(_)) = "01.01.01.01".parse() {
        println!("parsed!");  // it gets in!
    }
}

Usually, we consider IPv4 address containing only non-leading-zero number 0-255 legal, but not those with leading-zero numbers in. Should we change the behavior?

@jonas-schievink jonas-schievink added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 25, 2019
@kennytm
Copy link
Member

kennytm commented May 25, 2019

It is definitely legal. Either Leetcode is wrong or the question doesn't suppose you to "cheat" with the standard library.

$ ping 01.01.01.01
PING 01.01.01.01 (1.1.1.1): 56 data bytes
>>> import ipaddress
>>> ipaddress.IPv4Address('01.01.01.01')
IPv4Address('1.1.1.1')
package main

import (
	"net"
	"fmt"
)

func main() {
	fmt.Println(net.ParseIP("01.01.01.01")) // prints 1.1.1.1
}

@WiSaGaN
Copy link
Contributor

WiSaGaN commented May 29, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants