{"record":{"id":"996302512d0cd4d9","repo":"golang/go","slug":"writeuleb128fixedlength-length-too-small","errorCode":null,"errorMessage":"writeUleb128FixedLength: length too small","messagePattern":"writeUleb128FixedLength: length too small","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/link/internal/ld/data.go","lineNumber":3461,"sourceCode":"// writeUleb128FixedLength writes out value v in LEB128 encoded\n// format, ensuring that the space written takes up length bytes. When\n// extra space is needed, we write initial bytes with just the\n// continuation bit set. For example, if val is 1 and length is 3,\n// we'll write 0x80 0x80 0x1 (first two bytes with zero val but\n// continuation bit set). NB: this function adapted from a similar\n// function in cmd/link/internal/wasm, they could be commoned up if\n// needed.\nfunc writeUleb128FixedLength(b []byte, v uint64, length int) error {\n\tfor i := 0; i < length; i++ {\n\t\tc := uint8(v & 0x7f)\n\t\tv >>= 7\n\t\tif i < length-1 {\n\t\t\tc |= 0x80\n\t\t}\n\t\tb[i] = c\n\t}\n\tif v != 0 {\n\t\treturn fmt.Errorf(\"writeUleb128FixedLength: length too small\")\n\t}\n\treturn nil\n}\n","sourceCodeStart":3443,"sourceCodeEnd":3465,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/link/internal/ld/data.go#L3443-L3465","documentation":"The writeUleb128FixedLength function encodes a uint64 value as ULEB128 (Unsigned Little Endian Base 128) into a fixed-size byte slice of exactly 'length' bytes. Each ULEB128 byte encodes 7 bits of the value, so 'length' bytes can represent values up to (2^(7*length)) - 1. If the value exceeds this capacity after consuming all bytes, the error fires.","triggerScenarios":"The function writes 'length' bytes of ULEB128 encoding into buffer b. After the loop, if v != 0 (the value has not been fully consumed), it means the value requires more ULEB128 bytes than provided. For example, encoding 300 (which needs 2 ULEB128 bytes) into a 1-byte buffer triggers this error.","commonSituations":"Internal linker code miscalculates the required ULEB128 length for a value; a buffer size assumption is violated by an unexpectedly large value; changes to data structures that increase value ranges without updating the encoding length.","solutions":["Calculate the required ULEB128 length before calling: length = (bits.Len64(v) + 6) / 7","Increase the length parameter to accommodate the value","Verify the value is within the expected range before encoding","Report as a Go linker bug if the caller is internal linker code"],"exampleFix":"// before\nerr := writeUleb128FixedLength(buf, 300, 1) // fails: 300 needs 2 bytes\n\n// after\nerr := writeUleb128FixedLength(buf, 300, 2) // succeeds","handlingStrategy":"validation","validationCode":"// Validate that the value fits in the given ULEB128 length before encoding\nfunc fitsInULEB128(v uint64, length int) bool {\n    for i := 0; i < length; i++ {\n        v >>= 7\n    }\n    return v == 0\n}\n\n// Or compute the required length:\nfunc requiredULEB128Length(v uint64) int {\n    if v == 0 {\n        return 1\n    }\n    return (bits.Len64(v) + 6) / 7\n}","typeGuard":null,"tryCatchPattern":"// Handle encoding failures by resizing\nif err := writeUleb128FixedLength(buf, v, length); err != nil {\n    length = requiredULEB128Length(v)\n    if err := writeUleb128FixedLength(buf, v, length); err != nil {\n        return err // genuinely too large\n    }\n}","preventionTips":["Always calculate the required ULEB128 length before encoding: (bits.Len64(v) + 6) / 7","Use variable-length ULEB128 when the exact length is not constrained","Add assertions in tests that verify encoding lengths match expectations"],"tags":["encoding","uleb128","linker","go-toolchain","data-encoding"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}