{"record":{"id":"73e3609651fef915","repo":"valyala/fasthttp","slug":"value-is-negative-cannot-convert-to-uintptr","errorCode":null,"errorMessage":"value is negative, cannot convert to uintptr","messagePattern":"value is negative, cannot convert to uintptr","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tcplisten/tcplisten.go","lineNumber":202,"sourceCode":"\tdefault:\n\t\treturn nil, -1, errors.New(\"only tcp, tcp4, or tcp6 is supported \" + network)\n\t}\n}\n\nfunc safeIntToUint32(i int) (uint32, error) {\n\tif i < 0 {\n\t\treturn 0, errors.New(\"value is negative, cannot convert to uint32\")\n\t}\n\tui := uint64(i)\n\tif ui > math.MaxUint32 {\n\t\treturn 0, errors.New(\"value exceeds uint32 max value\")\n\t}\n\treturn uint32(ui), nil\n}\n\nfunc safeIntToUintptr(i int) (uintptr, error) {\n\tif i < 0 {\n\t\treturn 0, errors.New(\"value is negative, cannot convert to uintptr\")\n\t}\n\treturn uintptr(i), nil\n}\n","sourceCodeStart":184,"sourceCodeEnd":206,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/tcplisten/tcplisten.go#L184-L206","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nkeepAlive := -1 // 'default' sentinel\nln, _ := ctl.NewListener(\"tcp\", addr) // safeIntToUintptr(-1) errors\n// after\nkeepAlive := 0 // 0 = leave default\nln, _ := ctl.NewListener(\"tcp\", addr)","handlingStrategy":"validation","validationCode":"func validateSockOptValue(v int) error {\n  if v < 0 { return fmt.Errorf(\"socket option value must be >= 0, got %d\", v) }\n  return nil\n}","typeGuard":"func isUintptrSafe(i int) bool { return i >= 0 }","tryCatchPattern":"ln, err := ctl.NewListener(network, addr)\nif err != nil {\n  if strings.Contains(err.Error(), \"cannot convert to uintptr\") {\n    return fmt.Errorf(\"negative socket option value: %w\", err)\n  }\n  return err\n}","preventionTips":["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"],"tags":["network","tcp","syscall","validation"],"backgroundTag":"negative-int-to-uintptr","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}