valyala/fasthttp · error
empty integer
Error message
empty integer
What it means
errEmptyInt is returned by parseUintBuf (used by ParseUint and integer Args getters) when the input buffer contains no digits at all. Since there is nothing to parse, the function returns -1 and this sentinel error. Callers hitting it passed an empty or digit-less string where an unsigned integer was expected.
Source
Thrown at bytesconv.go:275
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
- Guard with len(buf) == 0 before calling ParseUint and apply a default or return a clearer error.
- Use errors.Is(err, ...) to distinguish 'empty' from other parse failures (first/trailing char, too long).
- Validate the raw string with a quick regexp like ^[0-9]+$ when input shape is uncertain.
- For Args, check args.Has(key) first so a missing key and an empty value are handled separately.
Example fix
// before
n, err := fasthttp.ParseUint(b)
// after
if len(b) == 0 {
return 0, errors.New("numeric field is empty")
}
n, err := fasthttp.ParseUint(b) Defensive patterns
Strategy: validation
Validate before calling
func parseUintNonEmpty(b []byte) (int, error) {
if len(b) == 0 {
return 0, errors.New("numeric value required")
}
return fasthttp.ParseUint(b)
} Type guard
func digitsOnly(b []byte) bool {
return len(b) > 0 && bytes.IndexFunc(b, func(r rune) bool { return r < '0' || r > '9' }) < 0
} Try / catch
n, err := fasthttp.ParseUint(b)
if err != nil {
if err.Error() == "empty integer" {
return 0, errors.New("field must not be empty")
}
return err
} Prevention
- Apply defaults for optional numeric fields before parsing
- Check len(b) > 0 whenever input may be nil
- Distinguish missing keys from empty values when reading Args
- Validate numeric headers with a digits-only check
When it happens
Trigger: fasthttp.ParseUint(nil) or ParseUint([]byte("")); args.GetUint on a key whose value is empty; parsing a header like Content-Length that arrived empty.
Common situations: Optional numeric query parameters the client omitted; headers stripped by a proxy; config values that are empty strings rather than unset.
Related errors
- empty ip address string
- unexpected first char found: expecting 0-9
- unexpected trailing char found: expecting 0-9
- too long int
- fasthttp: no args value for the given key
AI-assisted analysis of valyala/fasthttp@c96f600972 (2026-08-31).
Data as JSON: /api/errors/20d5ca2c3c6d51f1.
Report an issue: GitHub.