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
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@baislsl
Copy link

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 commented Aug 7, 2020

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

@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 7, 2020
@toothrot toothrot added this to the Backlog milestone Aug 7, 2020
@toothrot
Copy link
Contributor

toothrot commented Aug 7, 2020

/cc @bradfitz @ianlancetaylor

@baislsl
Copy link
Author

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants