{"record":{"id":"c5afd0139879e54a","repo":"valyala/fasthttp","slug":"value-is-negative-cannot-convert-to-uint32","errorCode":null,"errorMessage":"value is negative, cannot convert to uint32","messagePattern":"value is negative, cannot convert to uint32","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tcplisten/tcplisten.go","lineNumber":191,"sourceCode":"\t\tif tcpAddr.Zone != \"\" {\n\t\t\tifi, err := net.InterfaceByName(tcpAddr.Zone)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, -1, err\n\t\t\t}\n\t\t\tsa6.ZoneId, err = safeIntToUint32(ifi.Index)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, -1, fmt.Errorf(\"unexpected convert net interface index int to uint32: %w\", err)\n\t\t\t}\n\t\t}\n\t\treturn &sa6, unix.AF_INET6, nil\n\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":173,"sourceCodeEnd":206,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/tcplisten/tcplisten.go#L173-L206","documentation":"safeIntToUint32 guards the conversion of an int (e.g. a network interface index used for IPv6 scope) to uint32. If the value is negative it cannot be represented as uint32, so the function returns this error instead of silently wrapping around via the unsigned cast.","triggerScenarios":"getSockaddr passes a negative interface index (from net.InterfaceByIndex or similar) into safeIntToUint32 while building an AF_INET6 sockaddr — typically when an interface lookup failed or returned -1 as an 'unset' sentinel.","commonSituations":"Using a -1 interface index placeholder for 'any interface'; failed or uninitialized interface lookups; a configuration field like zone/interface index set to -1 in yaml/env.","solutions":["Inspect the interface index source; ensure it comes from a successful net.InterfaceByName/InterfaceByIndex lookup","Replace -1 sentinels with 0 or omit the interface index when binding on any interface","Validate the index is >= 0 before calling NewListener"],"exampleFix":"// before\nifaceIdx := -1 // unset\nln, _ := ctl.NewListener(\"tcp6\", addr) // safeIntToUint32(-1) errors\n// after\nifaceIdx := 0 // 0 = any interface\nln, _ := ctl.NewListener(\"tcp6\", addr)","handlingStrategy":"validation","validationCode":"func validateIfaceIndex(idx int) error {\n  if idx < 0 { return fmt.Errorf(\"interface index must be >= 0, got %d\", idx) }\n  return nil\n}","typeGuard":"func isUint32Safe(i int) bool { return i >= 0 && uint64(i) <= math.MaxUint32 }","tryCatchPattern":"ln, err := ctl.NewListener(network, addr)\nif err != nil {\n  if strings.Contains(err.Error(), \"value is negative, cannot convert to uint32\") {\n    return fmt.Errorf(\"invalid interface index %d: %w\", ifaceIdx, err)\n  }\n  return err\n}","preventionTips":["Resolve interface indices via net.InterfaceByName instead of hardcoding","Never use -1 as an interface-index sentinel; use 0 or make it optional","Validate all numeric config fields for sign before use","Handle interface lookup errors instead of propagating a default index"],"tags":["network","ipv6","integer-overflow","validation"],"backgroundTag":"negative-int-to-uint32","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}