mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-23 07:33:32 -06:00
c23a7fce4e
Go 1.17 includes a breaking change to both net.ParseIP and net.ParseCIDR functions to reject IPv4 address octets written with leading zeros. Our use of these functions as part of the various CIDR functions in the Terraform language doesn't have the same security concerns that the Go team had in evaluating this change to the standard library, and so we can't justify an exception to our v1.0 compatibility promises on the same sort of security grounds that the Go team used to justify their compatibility exception. For that reason, we'll now use our own fork of the Go library functions which has the new check disabled in order to preserve the prior behavior. We're taking this path, rather than pre-normalizing the IP address before calling into the standard library, because an additional normalization layer would be entirely new code and additional complexity, whereas this fork is relatively minor in terms of code size and avoids any significant changes to our own calls to these functions. Thanks to the Kubernetes team for their prior work on carving out a subset of the "net" package for their similar backward-compatibility concern. Our "ipaddr" package here is a lightly-modified fork of their fork, with only the comments changed to talk about Terraform instead of Kubernetes. This fork is not intended for use in any other future feature implementations, because they wouldn't be subject to the same compatibility constraints as our existing functions. We will use these forked implementations for new callers only if consistency with the behavior of the existing functions is a key requirement.
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Simple file i/o and string manipulation, to avoid
|
|
// depending on strconv and bufio and strings.
|
|
|
|
package ipaddr
|
|
|
|
// Bigger than we need, not too big to worry about overflow
|
|
const big = 0xFFFFFF
|
|
|
|
// Decimal to integer.
|
|
// Returns number, characters consumed, success.
|
|
func dtoi(s string) (n int, i int, ok bool) {
|
|
n = 0
|
|
for i = 0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
|
|
n = n*10 + int(s[i]-'0')
|
|
if n >= big {
|
|
return big, i, false
|
|
}
|
|
}
|
|
if i == 0 {
|
|
return 0, 0, false
|
|
}
|
|
return n, i, true
|
|
}
|
|
|
|
// Hexadecimal to integer.
|
|
// Returns number, characters consumed, success.
|
|
func xtoi(s string) (n int, i int, ok bool) {
|
|
n = 0
|
|
for i = 0; i < len(s); i++ {
|
|
if '0' <= s[i] && s[i] <= '9' {
|
|
n *= 16
|
|
n += int(s[i] - '0')
|
|
} else if 'a' <= s[i] && s[i] <= 'f' {
|
|
n *= 16
|
|
n += int(s[i]-'a') + 10
|
|
} else if 'A' <= s[i] && s[i] <= 'F' {
|
|
n *= 16
|
|
n += int(s[i]-'A') + 10
|
|
} else {
|
|
break
|
|
}
|
|
if n >= big {
|
|
return 0, i, false
|
|
}
|
|
}
|
|
if i == 0 {
|
|
return 0, i, false
|
|
}
|
|
return n, i, true
|
|
}
|