valyala/fasthttp · error

empty ip address string

Error message

empty ip address string

What it means

errEmptyIPStr is returned by ParseIPv4 when the input ipStr byte slice has zero length. ParseIPv4 cannot parse an IP from nothing, so it fails fast instead of producing a zero IP. It is an internal sentinel error surfaced to callers of ParseIPv4.

Source

Thrown at bytesconv.go:71

}

// AppendIPv4 appends string representation of the given ip v4 to dst
// and returns the extended dst.
func AppendIPv4(dst []byte, ip net.IP) []byte {
	ip = ip.To4()
	if len(ip) != net.IPv4len {
		return append(dst, "non-v4 ip passed to AppendIPv4"...)
	}

	dst = AppendUint(dst, int(ip[0]))
	for i := 1; i < 4; i++ {
		dst = append(dst, '.')
		dst = AppendUint(dst, int(ip[i]))
	}
	return dst
}

var errEmptyIPStr = errors.New("empty ip address string")

var httpDateGMT = time.FixedZone("GMT", 0)

// ParseIPv4 parses ip address from ipStr into dst and returns the extended dst.
func ParseIPv4(dst net.IP, ipStr []byte) (net.IP, error) {
	if len(ipStr) == 0 {
		return dst, errEmptyIPStr
	}
	if len(dst) < net.IPv4len || len(dst) > net.IPv4len {
		dst = make([]byte, net.IPv4len)
	}
	copy(dst, net.IPv4zero)
	dst = dst.To4() // dst is always non-nil here

	b := ipStr
	for i := range 3 {
		n := bytes.IndexByte(b, '.')
		if uint(n) >= uint(len(b)) {

View on GitHub (pinned to c96f600972)

Solutions

  1. Check len(ipStr) == 0 before calling ParseIPv4 and return a clear 'IP not provided' error or a default value.
  2. Ensure the upstream header/proxy actually populates the field; add the proxy config that sets X-Forwarded-For.
  3. Fall back to ctx.RemoteAddr() (or net.TCPAddrFromAddr) when the header-based IP is empty.
  4. If an empty value is acceptable, wrap the call and treat this error as a no-op with a nil IP.

Example fix

// before
ip, err := fasthttp.ParseIPv4(dst, hdr)
// after
if len(hdr) == 0 {
    return nil, errors.New("client IP header missing")
}
ip, err := fasthttp.ParseIPv4(dst, hdr)
Defensive patterns

Strategy: validation

Validate before calling

func parseIPOrDefault(dst net.IP, b []byte) (net.IP, error) {
    if len(b) == 0 {
        return nil, errors.New("IP address not provided")
    }
    return fasthttp.ParseIPv4(dst, b)
}

Type guard

func nonEmptyBytes(b []byte) bool { return len(b) > 0 }

Try / catch

ip, err := fasthttp.ParseIPv4(dst, hdr)
if err != nil {
    if len(hdr) == 0 {
        ip = defaultIP // header absent
    } else {
        return err
    }
}

Prevention

When it happens

Trigger: ParseIPv4(dst, nil) or ParseIPv4(dst, []byte("")) — e.g. passing an empty RemoteAddr/host header field, or a header that was never set.

Common situations: Extracting the client IP from a header (X-Forwarded-For, X-Real-IP) that the proxy did not set; reading a config value that is empty; splitting an address where the IP part is missing.

Related errors


AI-assisted analysis of valyala/fasthttp@c96f600972 (2026-08-31). Data as JSON: /api/errors/139dc920f9395237. Report an issue: GitHub.