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

net: ParseIP return 16 bytes length IP for IPv4 address #40606

Closed
baislsl opened this issue Aug 6, 2020 · 3 comments
Closed

net: ParseIP return 16 bytes length IP for IPv4 address #40606

baislsl opened this issue Aug 6, 2020 · 3 comments
Milestone

Comments

@baislsl
Copy link

@baislsl baislsl commented Aug 6, 2020

net.ParseIP parse the IPv4 address into 4 bytes length IP result, but converts it into a 16 bytes length redundantly.

Below is the relative code in net/ip.go

func ParseIP(s string) IP {
	for i := 0; i < len(s); i++ {
		switch s[i] {
		case '.':
			return parseIPv4(s)
		case ':':
			return parseIPv6(s)
		}
	}
	return nil
}

// Parse IPv4 address (d.d.d.d).
func parseIPv4(s string) IP {
	var p [IPv4len]byte
	for i := 0; i < IPv4len; i++ {
		if len(s) == 0 {
			// Missing octets.
			return nil
		}
		if i > 0 {
			if s[0] != '.' {
				return nil
			}
			s = s[1:]
		}
		n, c, ok := dtoi(s)
		if !ok || n > 0xFF {
			return nil
		}
		s = s[c:]
		p[i] = byte(n)
	}
	if len(s) != 0 {
		return nil
	}
	return IPv4(p[0], p[1], p[2], p[3]) // seems to be unnecessary?
}

func IPv4(a, b, c, d byte) IP {
	p := make(IP, IPv6len)
	copy(p, v4InV6Prefix)
	p[12] = a
	p[13] = b
	p[14] = c
	p[15] = d
	return p
}

Suppose we can return p directly without transfer in parseIPv4 ?

@toothrot
Copy link
Contributor

@toothrot toothrot commented Aug 7, 2020

I think this is a duplicate of #18804, let me know if you agree.

@toothrot toothrot added this to the Backlog milestone Aug 7, 2020
@toothrot
Copy link
Contributor

@toothrot toothrot commented Aug 7, 2020

@baislsl
Copy link
Author

@baislsl baislsl commented Aug 9, 2020

I think this is a duplicate of #18804, let me know if you agree.

It does. Discussion in #18804 more comprehensive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.