{"record":{"id":"2455ba3cbc234161","repo":"kubernetes/kops","slug":"unable-to-convert-cidr-subnet-new-bits-to-int-q","errorCode":null,"errorMessage":"unable to convert CIDR subnet new bits to int: %q: %v","messagePattern":"unable to convert CIDR subnet new bits to int: %q: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"upup/pkg/fi/utils/net.go","lineNumber":90,"sourceCode":"\tif ip.To4() != nil {\n\t\treturn false\n\t}\n\n\treturn true\n}\n\n// ParseCIDRNotation parses a string in the format \"/<newSize>#<netNum>\"\n// and returns the values of <newSize> and <netNum>.\nfunc ParseCIDRNotation(subnet string) (int, int64, error) {\n\tre := regexp.MustCompile(`^/(\\d+)#([a-f0-9]+)$`)\n\ts := re.FindStringSubmatch(subnet)\n\tif len(s) != 3 {\n\t\treturn 0, 0, fmt.Errorf(\"unable to parse CIDR subnet string %q using %q\", subnet, re)\n\t}\n\n\tnewSize, err := strconv.Atoi(s[1])\n\tif err != nil {\n\t\treturn 0, 0, fmt.Errorf(\"unable to convert CIDR subnet new bits to int: %q: %v\", s[1], err)\n\t}\n\n\tnetNum, err := strconv.ParseInt(s[2], 16, 64)\n\tif err != nil {\n\t\treturn 0, 0, fmt.Errorf(\"unable to convert CIDR subnet net num to int: %q: %v\", s[2], err)\n\t}\n\n\treturn newSize, netNum, nil\n}\n\n// CIDRSubnet calculates a subnet address within given IP network address prefix.\n// Inspired by the Terraform implementation of the \"cidrsubnet\" function\n// https://www.terraform.io/docs/language/functions/cidrsubnet.html\nfunc CIDRSubnet(prefix string, newSize int, netNum int64) (string, error) {\n\t_, baseCIDR, err := net.ParseCIDR(prefix)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"unable to parse CIDR for %q: %v\", prefix, err)\n\t}","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/upup/pkg/fi/utils/net.go#L72-L108","documentation":"ParseCIDRNotation in upup/pkg/fi/utils/net.go parses expanded IPv6 CIDR notation strings of the form <prefix>:<newbits>:<netnum>. The middle segment (new bits) must be a valid decimal integer; when strconv.Atoi on that segment fails, this error is returned with the offending segment and the underlying strconv error.","triggerScenarios":"Calling ParseCIDRNotation (directly or via validateIPv6CIDR, RenderTerraform, or calculateSubnetCIDR) with a CIDR-subnet string whose middle segment is not a decimal integer, e.g. an empty segment (\"2001:db8::/32::0\"), non-numeric text (\"/32x:0\"), or a segment with whitespace or signs.","commonSituations":"Hand-edited cluster spec or Terraform output where the cidrsubnet-style notation was malformed; copy/paste errors dropping a digit; templating that produced an empty middle segment; confusing the notation with standard CIDR syntax and writing a netmask instead of new-bits.","solutions":["Fix the CIDR-subnet string so the middle segment is a valid decimal integer (e.g. \"2001:db8:0000::/48:0:0\" style <prefix>:<newbits>:<netnum>).","Confirm the string matches the three-segment format expected by ParseCIDRNotation (regex splits into 3 parts separated by ':').","Validate user/cluster-spec input before calling, e.g. with strconv.Atoi on the middle segment yourself to get a clearer message.","Strip whitespace or stray characters (signs, units) from the segment before parsing."],"exampleFix":"// before\ncidr := \"2001:db8:0000::/48x:0\" // '48x' is not an integer\nnewSize, netNum, err := utils.ParseCIDRNotation(cidr)\n// after\ncidr := \"2001:db8:0000::/48:0\"\nnewSize, netNum, err := utils.ParseCIDRNotation(cidr)","handlingStrategy":"validation","validationCode":"func validNewBits(notation string) bool {\n\tparts := strings.Split(notation, \":\")\n\tif len(parts) != 3 {\n\t\treturn false\n\t}\n\t_, err := strconv.Atoi(parts[1])\n\treturn err == nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always build cidrsubnet-style notation programmatically with fmt.Sprintf(\"%s/%d:%d:%d\", ...) instead of string concatenation by hand.","Pre-validate the three segments with strconv.Atoi (base 10) and strconv.ParseInt (base 16) in tests.","Add unit tests covering malformed middle segments for any config parsing that feeds ParseCIDRNotation."],"tags":["network","cidr","parsing","ipv6"],"backgroundTag":"invalid-cidr-notation","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}