valyala/fasthttp · error
value is negative, cannot convert to uintptr
Error message
value is negative, cannot convert to uintptr
What it means
safeIntToUintptr converts an int control value to uintptr for raw syscalls (e.g. setsockopt/setockopt arguments in NewListener). A negative int cannot be meaningfully passed as uintptr — casting would produce a huge wrapped value — so the function returns this error first.
Source
Thrown at tcplisten/tcplisten.go:202
default:
return nil, -1, errors.New("only tcp, tcp4, or tcp6 is supported " + network)
}
}
func safeIntToUint32(i int) (uint32, error) {
if i < 0 {
return 0, errors.New("value is negative, cannot convert to uint32")
}
ui := uint64(i)
if ui > math.MaxUint32 {
return 0, errors.New("value exceeds uint32 max value")
}
return uint32(ui), nil
}
func safeIntToUintptr(i int) (uintptr, error) {
if i < 0 {
return 0, errors.New("value is negative, cannot convert to uintptr")
}
return uintptr(i), nil
}
View on GitHub (pinned to c96f600972)
Solutions
- Ensure socket option values (sizes, intervals) are non-negative before calling NewListener
- Replace -1 'default' sentinels with 0 or omit the option entirely
- Add a config-level validation that rejects negative numeric values for these fields
Example fix
// before
keepAlive := -1 // 'default' sentinel
ln, _ := ctl.NewListener("tcp", addr) // safeIntToUintptr(-1) errors
// after
keepAlive := 0 // 0 = leave default
ln, _ := ctl.NewListener("tcp", addr) Defensive patterns
Strategy: validation
Validate before calling
func validateSockOptValue(v int) error {
if v < 0 { return fmt.Errorf("socket option value must be >= 0, got %d", v) }
return nil
} Type guard
func isUintptrSafe(i int) bool { return i >= 0 } Try / catch
ln, err := ctl.NewListener(network, addr)
if err != nil {
if strings.Contains(err.Error(), "cannot convert to uintptr") {
return fmt.Errorf("negative socket option value: %w", err)
}
return err
} Prevention
- Reject negative numeric values for socket options in config loading
- Use 0 or absence of the field to mean 'default', never -1
- Validate env-supplied numeric overrides (e.g. SO_ buffers) at startup
- Keep option values in typed structs with clamping applied once, at parse time
When it happens
Trigger: NewListener calls safeIntToUintptr with a negative option value (e.g. a TCP keepalive/buffer-size setting read from config as -1) when applying socket options.
Common situations: Config using -1 as 'use default' sentinel for a socket option; unvalidated user/env input for buffer sizes; sign-flipped parsing of an unsigned config value.
Related errors
- only tcp, tcp4, or tcp6 is supported
- fasthttp: dialing to the given tcp address timed out
- couldn't find dns entries for the given domain: try using du
- value is negative, cannot convert to uint32
- value exceeds uint32 max value
AI-assisted analysis of valyala/fasthttp@c96f600972 (2026-08-31).
Data as JSON: /api/errors/73e3609651fef915.
Report an issue: GitHub.