thanos-io/thanos · error
proto: negative length found during unmarshaling
Error message
proto: negative length found during unmarshaling
What it means
ErrInvalidLengthRpc is a sentinel generated into gogo-protobuf's rpc.pb.go files. It is returned when a decoded length (e.g. a string/bytes field length or a computed postIndex) is negative, which cannot happen with well-formed protobuf input. It signals that the marshaled data or offset arithmetic is corrupt.
Solutions
- Check the buffer slicing logic that produced the byte slice passed to Unmarshal (correct offset and length).
- Validate payload integrity (checksums, content-length) before unmarshaling.
- Ensure all services use the same generated proto code version.
- Return a 4xx/invalid-data error to the caller rather than retrying, since the input is malformed.
Example fix
// before
if err := proto.Unmarshal(chunk, msg); err != nil { return err }
// after
if len(chunk) == 0 { return errors.New("empty chunk") }
if err := proto.Unmarshal(chunk, msg); err != nil {
if errors.Is(err, errpb.ErrInvalidLengthRpc) { return errors.Wrap(err, "malformed protobuf chunk") }
return err
} Defensive patterns
Strategy: try-catch
Validate before calling
if len(data) < 2 { return errors.New("payload too short to be valid protobuf") } Try / catch
if err := proto.Unmarshal(data, msg); err != nil {
if errors.Is(err, ErrInvalidLengthRpc) { return errors.Wrap(err, "corrupt protobuf: negative length") }
return err
} Prevention
- Checksum payloads in transit
- Slice buffers with verified offsets
- Pin gogo-protobuf codegen versions
When it happens
Trigger: Unmarshaling a message where a length varint decodes to a negative value (int overflow of the length prefix) or iNdEx + length underflows, in any generated Unmarshal/skip function referencing ErrInvalidLengthRpc.
Common situations: Corrupt object-storage payloads, byte-offset bugs when reading message slices from a larger buffer, or schema mismatch producing misaligned varints.
Understand the failure class
Background: "cannot parse invalid wire-format data", "cannot unmarshal", "failed unmarshalling": protobuf unmarshal errors explained — this error's family across 10 libraries.
Related errors
- proto: illegal wireType
- proto: integer overflow
- proto: unexpected end of group
- unmarshal response
- proto: wrong wireType =
AI-assisted analysis of thanos-io/thanos@35b8b99117 (2026-09-07).
Data as JSON: /api/errors/64cfe0022e0f196e.
Report an issue: GitHub.
Appendix: source
Thrown at pkg/status/statuspb/rpc.pb.go:2004
}
depth--
case 5:
iNdEx += 4
default:
return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
}
if iNdEx < 0 {
return 0, ErrInvalidLengthRpc
}
if depth == 0 {
return iNdEx, nil
}
}
return 0, io.ErrUnexpectedEOF
}
var (
ErrInvalidLengthRpc = fmt.Errorf("proto: negative length found during unmarshaling")
ErrIntOverflowRpc = fmt.Errorf("proto: integer overflow")
ErrUnexpectedEndOfGroupRpc = fmt.Errorf("proto: unexpected end of group")
)
View on GitHub (pinned to 35b8b99117)