Tencent/matrix · error · std::runtime_error
Invalid type size.
Error message
Invalid type size.
What it means
ReadTyped<T>() in the hprof reader reads a multi-byte value sized via ReadU2/ReadU4/ReadU8 depending on sizeof(T). If sizeof(T) is 1, 3, or anything other than 4 or 8 bytes, the reader cannot know which primitive read to use and throws std::runtime_error("Invalid type size.") to prevent misaligned/incorrect deserialization of the HPROF heap dump.
Solutions
- Use ReadU1()/ReadU2() for 1- and 2-byte fields instead of ReadTyped
- Instantiate ReadTyped only with 4- or 8-byte types (uint32_t, int32_t, uint64_t, float, double)
- Add a static_assert(sizeof(T)==4||sizeof(T)==8) to catch bad instantiations at compile time
- Check the HPROF spec for the exact field size of the record being parsed
Example fix
// before uint8_t flags = ReadTyped<uint8_t>(); // throws Invalid type size. // after uint8_t flags = ReadU1();
Defensive patterns
Strategy: validation
Validate before calling
template <typename T>
bool isReadableSize() { return sizeof(T) == 4 || sizeof(T) == 8; }
// assert before calling:
static_assert(isReadableSize<MyType>(), "ReadTyped requires 4- or 8-byte types"); Type guard
template <typename T> constexpr bool valid_read_type_v = (sizeof(T) == 4 || sizeof(T) == 8);
Try / catch
try {
auto v = reader.ReadTyped<uint32_t>();
} catch (const std::runtime_error &e) {
if (strcmp(e.what(), "Invalid type size.") == 0) {
// fix template instantiation / use ReadU2() for small fields
}
throw;
} Prevention
- Add static_assert(sizeof(T)==4||sizeof(T)==8) inside ReadTyped
- Use dedicated ReadU1/ReadU2 readers for small fields
- Consult the HPROF spec for exact field widths when adding parsers
- Wrap HPROF parsing in try/catch so corrupt/unexpected records fail gracefully
When it happens
Trigger: Calling ReadTyped with a template type whose size is not 4 or 8 bytes (e.g. ReadTyped<uint8_t>, ReadTyped<bool>, ReadTyped<Some3ByteStruct>) while parsing an HPROF record.
Common situations: Adding new HPROF record parsers that read single-byte or 2-byte fields using the generic ReadTyped helper instead of the dedicated U1/U2 readers; refactors changing a field's type to char/bool.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- Unable to obtained scaled bitmap's dimensions
- Could not find char array in
- Field does not exists
- Unexpected type for
- Unknown root type:
AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08).
Data as JSON: /api/errors/7bde3022b6e5f6b0.
Report an issue: GitHub.
Appendix: source
Thrown at matrix/matrix-android/matrix-hprof-analyzer/lib/reader/include/reader.h:62
Skip(sizeof(uint64_t));
}
template<typename T>
T ReadTyped(size_t size) {
if (size == sizeof(uint8_t)) {
const uint8_t value = ReadU1();
return *reinterpret_cast<const T *>(&value);
} else if (size == sizeof(uint16_t)) {
const uint16_t value = ReadU2();
return *reinterpret_cast<const T *>(&value);
} else if (size == sizeof(uint32_t)) {
const uint32_t value = ReadU4();
return *reinterpret_cast<const T *>(&value);
} else if (size == sizeof(uint64_t)) {
const uint64_t value = ReadU8();
return *reinterpret_cast<const T *>(&value);
} else {
throw std::runtime_error("Invalid type size.");
}
}
std::string ReadString(size_t length);
std::string ReadNullTerminatedString();
uint64_t Read(size_t size);
void Skip(size_t size);
void ResetCursor();
const void *Extract(size_t size);
private:
const size_t buffer_size_;
const uint8_t *buffer_;View on GitHub (pinned to 3b8293bd65)