{"record":{"id":"7bde3022b6e5f6b0","repo":"Tencent/matrix","slug":"invalid-type-size","errorCode":null,"errorMessage":"Invalid type size.","messagePattern":"Invalid type size\\.","errorType":"exception","errorClass":"std::runtime_error","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-hprof-analyzer/lib/reader/include/reader.h","lineNumber":62,"sourceCode":"            Skip(sizeof(uint64_t));\n        }\n\n        template<typename T>\n        T ReadTyped(size_t size) {\n            if (size == sizeof(uint8_t)) {\n                const uint8_t value = ReadU1();\n                return *reinterpret_cast<const T *>(&value);\n            } else if (size == sizeof(uint16_t)) {\n                const uint16_t value = ReadU2();\n                return *reinterpret_cast<const T *>(&value);\n            } else if (size == sizeof(uint32_t)) {\n                const uint32_t value = ReadU4();\n                return *reinterpret_cast<const T *>(&value);\n            } else if (size == sizeof(uint64_t)) {\n                const uint64_t value = ReadU8();\n                return *reinterpret_cast<const T *>(&value);\n            } else {\n                throw std::runtime_error(\"Invalid type size.\");\n            }\n        }\n\n        std::string ReadString(size_t length);\n\n        std::string ReadNullTerminatedString();\n\n        uint64_t Read(size_t size);\n\n        void Skip(size_t size);\n\n        void ResetCursor();\n\n        const void *Extract(size_t size);\n\n    private:\n        const size_t buffer_size_;\n        const uint8_t *buffer_;","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-hprof-analyzer/lib/reader/include/reader.h#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nuint8_t flags = ReadTyped<uint8_t>(); // throws Invalid type size.\n// after\nuint8_t flags = ReadU1();","handlingStrategy":"validation","validationCode":"template <typename T>\nbool isReadableSize() { return sizeof(T) == 4 || sizeof(T) == 8; }\n// assert before calling:\nstatic_assert(isReadableSize<MyType>(), \"ReadTyped requires 4- or 8-byte types\");","typeGuard":"template <typename T>\nconstexpr bool valid_read_type_v = (sizeof(T) == 4 || sizeof(T) == 8);","tryCatchPattern":"try {\n  auto v = reader.ReadTyped<uint32_t>();\n} catch (const std::runtime_error &e) {\n  if (strcmp(e.what(), \"Invalid type size.\") == 0) {\n    // fix template instantiation / use ReadU2() for small fields\n  }\n  throw;\n}","preventionTips":["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"],"tags":["cpp","hprof","deserialization","template","heap-dump"],"backgroundTag":"internal-invariant-violation","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}