{"record":{"id":"0385767f97546f84","repo":"valyala/fasthttp","slug":"cannot-parse-ip-string-q-w","errorCode":null,"errorMessage":"cannot parse ip string %q: %w","messagePattern":"cannot parse ip string %q: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"bytesconv.go","lineNumber":97,"sourceCode":"\t}\n\tif len(dst) < net.IPv4len || len(dst) > net.IPv4len {\n\t\tdst = make([]byte, net.IPv4len)\n\t}\n\tcopy(dst, net.IPv4zero)\n\tdst = dst.To4() // dst is always non-nil here\n\n\tb := ipStr\n\tfor i := range 3 {\n\t\tn := bytes.IndexByte(b, '.')\n\t\tif uint(n) >= uint(len(b)) {\n\t\t\treturn dst, fmt.Errorf(\"cannot find dot in ip string %q\", ipStr)\n\t\t}\n\t\toctet, parsed, err := parseIPv4Octet(b[:n])\n\t\tif err != nil {\n\t\t\tif errors.Is(err, errIPv4PartTooLarge) {\n\t\t\t\treturn dst, fmt.Errorf(\"cannot parse ip string %q: ip part cannot exceed 255: parsed %d\", ipStr, parsed)\n\t\t\t}\n\t\t\treturn dst, fmt.Errorf(\"cannot parse ip string %q: %w\", ipStr, err)\n\t\t}\n\t\tdst[i] = octet\n\t\tb = b[n+1:]\n\t}\n\toctet, parsed, err := parseIPv4Octet(b)\n\tif err != nil {\n\t\tif errors.Is(err, errIPv4PartTooLarge) {\n\t\t\treturn dst, fmt.Errorf(\"cannot parse ip string %q: ip part cannot exceed 255: parsed %d\", ipStr, parsed)\n\t\t}\n\t\treturn dst, fmt.Errorf(\"cannot parse ip string %q: %w\", ipStr, err)\n\t}\n\tdst[3] = octet\n\n\treturn dst, nil\n}\n\n// AppendHTTPDate appends HTTP-compliant (RFC1123) representation of date\n// to dst and returns the extended dst.","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/bytesconv.go#L79-L115","documentation":"ParseIPv4 parses a dotted-quad IPv4 string into a 4-byte array. When parsing an octet via parseIPv4Octet fails with an error other than the 'part too large' sentinel (e.g. empty part, non-digit characters, too many digits), the error is wrapped with the original input string via %w so callers can errors.Is/As the underlying cause.","triggerScenarios":"Calling fasthttp.ParseIPv4 (or code that uses it, e.g. AcquireURI/host parsing) with a string containing an invalid octet: empty components like \"1..2.3\", non-numeric characters like \"1.2.3.a\", or an octet with more than 3 digits.","commonSituations":"Hardcoded IPs with typos, IPs read from config files or environment variables with stray characters or double dots, values coming from user input or DNS text records that were not validated.","solutions":["Print the wrapped inner error (%w) to see the exact per-octet failure","Validate the string matches ^\\d{1,3}(\\.\\d{1,3}){3}$ before parsing","Trim whitespace and BOM characters from the input before parsing","Use net.ParseIP as a pre-check if you only need validation, not the fast byte form"],"exampleFix":"// before\nvar ip [4]byte\n_, err := fasthttp.ParseIPv4(ip[:], []byte(strings.TrimSpace(cfg.Host))) // err: cannot parse ip string \"1..2.3\": ...\n// after\nhost := strings.TrimSpace(cfg.Host)\nif net.ParseIP(host) == nil {\n    return fmt.Errorf(\"invalid ipv4 in config: %q\", host)\n}\n_, err := fasthttp.ParseIPv4(ip[:], []byte(host))","handlingStrategy":"validation","validationCode":"var ipv4Re = regexp.MustCompile(`^(\\d{1,3})(\\.\\d{1,3}){3}$`)\nif !ipv4Re.MatchString(ipStr) { return fmt.Errorf(\"not an ipv4 literal: %q\", ipStr) }","typeGuard":"func isIPv4Literal(s string) bool {\n    parts := strings.Split(s, \".\")\n    if len(parts) != 4 { return false }\n    for _, p := range parts {\n        if len(p) == 0 || len(p) > 3 { return false }\n        n, err := strconv.Atoi(p)\n        if err != nil || n > 255 { return false }\n    }\n    return true\n}","tryCatchPattern":"dst := [4]byte{}\nif _, err := fasthttp.ParseIPv4(dst[:], []byte(ipStr)); err != nil {\n    if errors.Is(err, errIPv4PartTooLarge) { /* octet > 255 */ }\n    return fmt.Errorf(\"parse %q: %w\", ipStr, err)\n}","preventionTips":["Trim whitespace before parsing values from env/config","Pre-validate with net.ParseIP for non-performance-critical paths","Keep raw values in error logs to trace config source"],"tags":["go","parsing","ipv4","input-validation"],"backgroundTag":"invalid-ip-address","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}