valyala/fasthttp · error
ip part cannot exceed 255
Error message
ip part cannot exceed 255
What it means
errIPv4PartTooLarge is returned when an octet parsed during ParseIPv4 (or parseIPv4Octet) exceeds 255, which is impossible for a valid IPv4 address. Each of the four dot-separated parts must fit in one byte. The error indicates malformed IP input rather than a library fault.
Source
Thrown at bytesconv.go:276
return strconv.AppendUint(dst, uint64(n), 10)
}
// ParseUint parses uint from buf.
//
// A value too large for an int is an error rather than a wrapped result, so
// ParseUint accepts exactly the unsigned decimal strings whose value fits in an
// int on the current platform.
func ParseUint(buf []byte) (int, error) {
v, n, err := parseUintBuf(buf)
if n != len(buf) {
return -1, errUnexpectedTrailingChar
}
return v, err
}
var (
errEmptyInt = errors.New("empty integer")
errIPv4PartTooLarge = errors.New("ip part cannot exceed 255")
errUnexpectedFirstChar = errors.New("unexpected first char found: expecting 0-9")
errUnexpectedTrailingChar = errors.New("unexpected trailing char found: expecting 0-9")
errTooLongInt = errors.New("too long int")
)
const (
// maxIntDiv10 is the largest accumulator that can still take another digit.
// Anything above it overflows an int when multiplied by 10.
maxIntDiv10 = math.MaxInt / 10
// maxSafeIntDigits is how many leading decimal digits can never overflow an
// int, whatever the word size: 10**18-1 fits a 64-bit int and 10**9-1 fits a
// 32-bit one. Go defines strconv.IntSize as 32 or 64 and nothing else.
// TestMaxSafeIntDigits checks both halves of that claim on the build's own
// int size.
maxSafeIntDigits = 9 * (strconv.IntSize / 32)
)
View on GitHub (pinned to c96f600972)
Solutions
- Sanitize the input before parsing: split on '.' and verify each part is a number in 0..255, or pre-validate with net.ParseIP(string(ipStr)) != nil.
- Return a 400 Bad Request when the value comes from user input, since it is client-supplied malformed data.
- Strip a port suffix (e.g. '1.2.3.4:8080') before parsing — the ':8080' part makes parsing fail.
- Trim whitespace and surrounding brackets from header values before parsing.
Example fix
// before
ip, err := fasthttp.ParseIPv4(dst, []byte(userInput))
// after
if net.ParseIP(userInput) == nil {
return errors.New("invalid IPv4 address")
}
ip, err := fasthttp.ParseIPv4(dst, []byte(userInput)) Defensive patterns
Strategy: validation
Validate before calling
func validIPv4(s string) bool {
ip := net.ParseIP(s)
return ip != nil && ip.To4() != nil
}
// call site
if !validIPv4(string(b)) {
return errors.New("invalid IPv4 address")
}
_, err := fasthttp.ParseIPv4(dst, b) Type guard
func isIPv4Bytes(b []byte) bool {
ip := net.ParseIP(string(b))
return ip != nil && ip.To4() != nil
} Try / catch
ip, err := fasthttp.ParseIPv4(dst, b)
if err != nil {
return fmt.Errorf("bad IPv4 %q: %w", b, err) // includes 'ip part cannot exceed 255'
} Prevention
- Pre-validate with net.ParseIP before fasthttp parsing
- Strip ':port' and brackets from address strings first
- Never feed raw user input to IP parsers without validation
- Split on '.' and range-check octets for clear error messages
When it happens
Trigger: ParseIPv4(dst, []byte("256.1.1.1")) or any address where a dot-separated component is >255 (also huge components like "999999999999.1.1.1").
Common situations: Typo'd or hand-crafted IPs in config files; untrusted user input passed as an IP; header values that contain port numbers, hostnames, or garbage instead of a dotted quad.
Related errors
- empty ip address string
- fasthttp: no args value for the given key
- empty integer
- unexpected first char found: expecting 0-9
- unexpected trailing char found: expecting 0-9
AI-assisted analysis of valyala/fasthttp@c96f600972 (2026-08-31).
Data as JSON: /api/errors/a68bb7fedbe0e0c4.
Report an issue: GitHub.