{"record":{"id":"05bd4f5700f09171","repo":"hyperledger/fabric","slug":"value-s-overflows-uint32","errorCode":null,"errorMessage":"value '%s' overflows uint32","messagePattern":"value '(.+?)' overflows uint32","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/viperutil/config_util.go","lineNumber":261,"sourceCode":"\tre := regexp.MustCompile(`^(?P<size>[0-9]+)\\s*(?i)(?P<unit>(k|m|g))b?$`)\n\tif re.MatchString(raw) {\n\t\tsize, err := strconv.ParseUint(re.ReplaceAllString(raw, \"${size}\"), 0, 64)\n\t\tif err != nil {\n\t\t\treturn data, nil\n\t\t}\n\t\tunit := re.ReplaceAllString(raw, \"${unit}\")\n\t\tswitch strings.ToLower(unit) {\n\t\tcase \"g\":\n\t\t\tsize = size << 10\n\t\t\tfallthrough\n\t\tcase \"m\":\n\t\t\tsize = size << 10\n\t\t\tfallthrough\n\t\tcase \"k\":\n\t\t\tsize = size << 10\n\t\t}\n\t\tif size > math.MaxUint32 {\n\t\t\treturn size, fmt.Errorf(\"value '%s' overflows uint32\", raw)\n\t\t}\n\t\treturn size, nil\n\t}\n\treturn data, nil\n}\n\nfunc stringFromFileDecodeHook(f reflect.Kind, t reflect.Kind, data any) (any, error) {\n\t// \"to\" type should be string\n\tif t != reflect.String {\n\t\treturn data, nil\n\t}\n\t// \"from\" type should be map\n\tif f != reflect.Map {\n\t\treturn data, nil\n\t}\n\tv := reflect.ValueOf(data)\n\tswitch v.Kind() {\n\tcase reflect.String:","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/common/viperutil/config_util.go#L243-L279","documentation":"byteSizeDecodeHook is a viper decode hook that converts size strings like \"128mb\" into a uint32 byte count. If the converted value exceeds math.MaxUint32 (about 4 GiB), the hook returns this error because the target config field is a uint32 and cannot hold the value.","triggerScenarios":"Setting a viper config value (YAML file, flag, or env var via viperutil) that maps to a uint32 size field with a value like \"5gb\" or \"4294967297b\" — any size that, after unit scaling (k/m/g shifted <<10), exceeds 4294967295 bytes.","commonSituations":"Configuring large ledger/block sizes (e.g. absoluteMaxBytes: 2gb is fine, but 8gb overflows), copying cloud-provider sizes into Fabric config, or typos like \"40gb\" intended as \"40mb\".","solutions":["Lower the configured size to at most ~4 GiB (e.g. \"2gb\" or a plain byte count <= 4294967295).","Express the value in a smaller unit (\"4096mb\" instead of \"4gb\" still overflows — use \"2048mb\") only if the numeric result fits.","Double-check the unit letter; a stray 'g' where 'm' was intended causes the overflow.","If a larger limit is truly needed, change the target struct field type to uint64 and update the decode hook target kind."],"exampleFix":"// before (config.yaml)\npeer.gossip.state.checkInterval: 5gb  # if mapped to a uint32 size field\n# after\nsize: 2048mb","handlingStrategy":"validation","validationCode":"// Validate a byte-size config string fits uint32 before it reaches viper decode\nvar sizeRe = regexp.MustCompile(`^([0-9]+)\\s*([kKmMgG])b?$`)\nfunc fitsUint32(raw string) bool {\n\tm := sizeRe.FindStringSubmatch(raw)\n\tif m == nil { return true } // not unit-scaled, hook passes through\n\tn, _ := strconv.ParseUint(m[1], 10, 64)\n\tswitch strings.ToLower(m[2]) {\n\tcase \"g\": n <<= 20\n\tcase \"m\": n <<= 10\n\tcase \"k\": n <<= 0\n\t}\n\treturn n <= math.MaxUint32\n}","typeGuard":"func isSafeByteSize(v any) bool {\n\ts, ok := v.(string)\n\tif !ok { return true }\n\treturn fitsUint32(s)\n}","tryCatchPattern":"err := viper.Unmarshal(cfg, viperutil.DecodeHookFunc) // includes byteSizeDecodeHook\nif err != nil {\n\tif strings.Contains(err.Error(), \"overflows uint32\") {\n\t\treturn fmt.Errorf(\"config size too large (max ~4GiB): %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Keep uint32 size settings at or below 4294967295 bytes (~4 GiB).","Double-check unit letters in YAML (m vs g).","Add config-schema validation at startup that rejects sizes over MaxUint32 with a clear message.","Document per-field maxima next to the config keys."],"tags":["go","viper","config","integer-overflow"],"backgroundTag":"uint32-overflow","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}