-
Notifications
You must be signed in to change notification settings - Fork 10.3k
lang/funcs: update cidrsubnet and cidrhost for 64-bit systems #25517
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package funcs | |
|
|
||
| import ( | ||
| "fmt" | ||
| "math/big" | ||
| "net" | ||
|
|
||
| "github.com/apparentlymart/go-cidr/cidr" | ||
|
|
@@ -25,7 +26,7 @@ var CidrHostFunc = function.New(&function.Spec{ | |
| }, | ||
| Type: function.StaticReturnType(cty.String), | ||
| Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) { | ||
| var hostNum int | ||
| var hostNum *big.Int | ||
| if err := gocty.FromCtyValue(args[1], &hostNum); err != nil { | ||
| return cty.UnknownVal(cty.String), err | ||
| } | ||
|
|
@@ -34,7 +35,7 @@ var CidrHostFunc = function.New(&function.Spec{ | |
| return cty.UnknownVal(cty.String), fmt.Errorf("invalid CIDR expression: %s", err) | ||
| } | ||
|
|
||
| ip, err := cidr.Host(network, hostNum) | ||
| ip, err := cidr.HostBig(network, hostNum) | ||
| if err != nil { | ||
| return cty.UnknownVal(cty.String), err | ||
| } | ||
|
|
@@ -86,7 +87,7 @@ var CidrSubnetFunc = function.New(&function.Spec{ | |
| if err := gocty.FromCtyValue(args[1], &newbits); err != nil { | ||
| return cty.UnknownVal(cty.String), err | ||
| } | ||
| var netnum int | ||
| var netnum *big.Int | ||
| if err := gocty.FromCtyValue(args[2], &netnum); err != nil { | ||
| return cty.UnknownVal(cty.String), err | ||
| } | ||
|
|
@@ -96,15 +97,11 @@ var CidrSubnetFunc = function.New(&function.Spec{ | |
| return cty.UnknownVal(cty.String), fmt.Errorf("invalid CIDR expression: %s", err) | ||
| } | ||
|
|
||
| // For portability with 32-bit systems where the subnet number | ||
| // will be a 32-bit int, we only allow extension of 32 bits in | ||
| // one call even if we're running on a 64-bit machine. | ||
| // (Of course, this is significant only for IPv6.) | ||
| if newbits > 32 { | ||
| return cty.UnknownVal(cty.String), fmt.Errorf("may not extend prefix by more than 32 bits") | ||
| if newbits > 64 { | ||
|
||
| return cty.UnknownVal(cty.String), fmt.Errorf("may not extend prefix by more than 64 bits") | ||
| } | ||
|
|
||
| newNetwork, err := cidr.Subnet(network, newbits, netnum) | ||
| newNetwork, err := cidr.SubnetBig(network, newbits, netnum) | ||
| if err != nil { | ||
| return cty.UnknownVal(cty.String), err | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had honestly completely forgotten that
goctyknows how to decode intobig.Int! I'm glad to remember now that it does, cause I was originally expecting this change to be a bit more invasive.One thing I notice looking back at the test cases for these functions is that they aren't testing the situation where the
newbits,hostnum,netnumarguments are fractional numbers. I'm expecting that all of those would be properly rejected as invalid by thegoctyconversions here (it should say something like "value must be a whole number") but might be worth adding some "expected error" test cases as insurance to future changes that might inadvertently make those panic otherwise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done and done!