{"record":{"id":"b6d7b5747a73116f","repo":"XIU2/CloudflareSpeedTest","slug":"parsecidr-err","errorCode":null,"errorMessage":"ParseCIDR err","messagePattern":"ParseCIDR err","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"task/ip.go","lineNumber":72,"sourceCode":"\t// 如果不含有 '/' 则代表不是 IP 段，而是一个单独的 IP，因此需要加上 /32 /128 子网掩码\n\tif i := strings.IndexByte(ip, '/'); i < 0 {\n\t\tif isIPv4(ip) {\n\t\t\tr.mask = \"/32\"\n\t\t} else {\n\t\t\tr.mask = \"/128\"\n\t\t}\n\t\tip += r.mask\n\t} else {\n\t\tr.mask = ip[i:]\n\t}\n\treturn ip\n}\n\n// 解析 IP 段，获得 IP、IP 范围、子网掩码\nfunc (r *IPRanges) parseCIDR(ip string) {\n\tvar err error\n\tif r.firstIP, r.ipNet, err = net.ParseCIDR(r.fixIP(ip)); err != nil {\n\t\tlog.Fatalln(\"ParseCIDR err\", err)\n\t}\n}\n\nfunc (r *IPRanges) appendIPv4(d byte) {\n\tr.appendIP(net.IPv4(r.firstIP[12], r.firstIP[13], r.firstIP[14], d))\n}\n\nfunc (r *IPRanges) appendIP(ip net.IP) {\n\tr.ips = append(r.ips, &net.IPAddr{IP: ip})\n}\n\n// 返回第四段 ip 的最小值及可用数目\nfunc (r *IPRanges) getIPRange() (minIP, hosts byte) {\n\tminIP = r.firstIP[15] & r.ipNet.Mask[3] // IP 第四段最小值\n\n\t// 根据子网掩码获取主机数量\n\tm := net.IPv4Mask(255, 255, 255, 255)\n\tfor i, v := range r.ipNet.Mask {","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/XIU2/CloudflareSpeedTest/blob/1da0c025d7b6f9bf0b2b0cf47a547f65430deab2/task/ip.go#L54-L90","documentation":"task/ip.go:69-74 wraps net.ParseCIDR in log.Fatalln, so any IP/CIDR that Go cannot parse terminates the whole process with 'ParseCIDR err'. fixIP() first appends /32 or /128 to bare addresses, so this fires only for genuinely malformed input (bad octets, invalid mask, non-IP text). It is called once per line of ip.txt / -f and once per comma element of -ip, from loadIPRanges().","triggerScenarios":"A line in the IP file such as '1.2.3.256/24', '10.0.0.0/33', 'not-an-ip', or a comment line; or a malformed element in -ip 1.1.1.1,foo/24. Empty lines are skipped, but any other non-CIDR text is fatal. Note fixIP's isIPv4 check failing makes it append /128, which then fails ParseCIDR for garbage strings.","commonSituations":"Editing ip.txt and leaving a comment like '# Cloudflare'; a Windows editor saving with stray characters; pasting a URL instead of a CIDR; mixing in an IP with a hostname; using a /33+ mask or an octet > 255.","solutions":["Open the file/argument named in the log output and fix or delete the offending line (the error text includes net.ParseCIDR's reason, e.g. 'invalid CIDR address').","Remove comment lines and trailing whitespace/CR characters from ip.txt; only one CIDR per line is supported.","If you inject IP data from another tool, validate each entry with net.ParseCIDR (or `prips`/`python ipaddress`) before writing it.","As a code-level hardening, downgrade log.Fatalln to a warning that skips the bad entry so one typo cannot kill a long run."],"exampleFix":"// before (task/ip.go:71)\nif r.firstIP, r.ipNet, err = net.ParseCIDR(r.fixIP(ip)); err != nil {\n    log.Fatalln(\"ParseCIDR err\", err)\n}\n// after\nif r.firstIP, r.ipNet, err = net.ParseCIDR(r.fixIP(ip)); err != nil {\n    utils.Yellow.Printf(\"[跳过] 无效 IP 段 [%s]: %v\\n\", ip, err)\n    r.firstIP, r.ipNet = nil, nil\n    return\n}\n// callers must then skip entries where r.ipNet == nil","handlingStrategy":"type-guard","validationCode":"# shell: validate the IP file before running\nwhile IFS= read -r line; do\n  case \"$line\" in ''|'#'*) continue;; esac\n  ipcalc -c \"$line\" >/dev/null 2>&1 || { echo \"bad CIDR: $line\"; exit 1; }\ndone < ip.txt","typeGuard":"// isCIDR reports whether s is a bare IP or a valid CIDR,\n// mirroring fixIP()'s /32,/128 append before net.ParseCIDR.\nfunc isCIDR(s string) bool {\n\tif !strings.Contains(s, \"/\") {\n\t\ts += \"/32\"\n\t}\n\t_, _, err := net.ParseCIDR(s)\n\treturn err == nil\n}","tryCatchPattern":null,"preventionTips":["Keep ip.txt to one CIDR per line; strip comments and BOM/CR when editing.","Validate generated lists (net.ParseCIDR / python ipaddress) before feeding them to the tool.","Patch log.Fatalln to skip-and-warn so one bad line cannot kill a run."],"tags":["go","fatal","cidr","parsing","ip-input"],"backgroundTag":null,"analyzedSha":"1da0c025d7b6f9bf0b2b0cf47a547f65430deab2","analyzedAt":"2026-08-15T22:22:10.737Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}