{"record":{"id":"3dd9429a49b16983","repo":"kubernetes/kops","slug":"unable-to-convert-cidr-subnet-net-num-to-int-q","errorCode":null,"errorMessage":"unable to convert CIDR subnet net num to int: %q: %v","messagePattern":"unable to convert CIDR subnet net num to int: %q: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"upup/pkg/fi/utils/net.go","lineNumber":95,"sourceCode":"}\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}\n\n\toldSize, totalSize := baseCIDR.Mask.Size()\n\tif oldSize == 0 && totalSize == 0 {\n\t\treturn \"\", fmt.Errorf(\"unable to calculate CIDR mask size for %q: %q\", prefix, baseCIDR.Mask)\n\t}","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/upup/pkg/fi/utils/net.go#L77-L113","documentation":"ParseCIDRNotation parses the third segment of the expanded IPv6 CIDR-subnet notation as a base-16 (hex) integer (the subnet net number). When strconv.ParseInt(s, 16, 64) fails, this error wraps the strconv error with the offending segment.","triggerScenarios":"Calling ParseCIDRNotation (via validateIPv6CIDR, RenderTerraform, or calculateSubnetCIDR) with a third segment that is not valid hex, e.g. \"2001:db8::/48:8:zz\", a segment containing 'g'-'z', or an empty/whitespace netnum.","commonSituations":"Typos when hand-writing cidrsubnet-style IPv6 subnet notation; generating the notation from a template that emitted decimal or placeholder text for the netnum; copy/paste mangling hex digits.","solutions":["Correct the netnum segment to a valid hex value (0-9, a-f), e.g. \"2001:db8:0000::/48:8:0\".","Remember the netnum is parsed in base 16 — supply hex, not decimal with invalid digits.","Ensure the segment is non-empty and free of whitespace or stray characters.","Pre-validate with strconv.ParseInt(segment, 16, 64) before calling to localize the problem."],"exampleFix":"// before\ncidr := \"2001:db8:0000::/48:8:zz\" // 'zz' is not hex\nnewSize, netNum, err := utils.ParseCIDRNotation(cidr)\n// after\ncidr := \"2001:db8:0000::/48:8:ff\"\nnewSize, netNum, err := utils.ParseCIDRNotation(cidr)","handlingStrategy":"validation","validationCode":"func validNetNum(notation string) bool {\n\tparts := strings.Split(notation, \":\")\n\tif len(parts) != 3 {\n\t\treturn false\n\t}\n\t_, err := strconv.ParseInt(parts[2], 16, 64)\n\treturn err == nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember the netnum segment is hex; generate it with strconv.FormatInt(n, 16).","Reject non-hex characters ([^0-9a-fA-F]) in user input before calling ParseCIDRNotation.","Test the notation round-trip: parse, re-encode, compare equality."],"tags":["network","cidr","parsing","ipv6","hex"],"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"}