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

  1. Ensure socket option values (sizes, intervals) are non-negative before calling NewListener
  2. Replace -1 'default' sentinels with 0 or omit the option entirely
  3. 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

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


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