{"record":{"id":"cb7f4bcd76e2d608","repo":"Tencent/matrix","slug":"bad-idsize","errorCode":null,"errorMessage":"bad idSize: ","messagePattern":"bad idSize: ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-resource-canary/matrix-resource-canary-android/src/main/java/com/tencent/matrix/resource/hproflib/HprofReader.java","lineNumber":50,"sourceCode":"public class HprofReader {\n    private final InputStream mStreamIn;\n    private int mIdSize = 0;\n\n    public HprofReader(InputStream in) {\n        mStreamIn = in;\n    }\n\n    public void accept(HprofVisitor hv) throws IOException {\n        acceptHeader(hv);\n        acceptRecord(hv);\n        hv.visitEnd();\n    }\n\n    private void acceptHeader(HprofVisitor hv) throws IOException {\n        final String text = IOUtil.readNullTerminatedString(mStreamIn);\n        final int idSize = IOUtil.readBEInt(mStreamIn);\n        if (idSize <= 0 || idSize >= (Integer.MAX_VALUE >> 1)) {\n            throw new IOException(\"bad idSize: \" + idSize);\n        }\n        final long timestamp = IOUtil.readBELong(mStreamIn);\n        mIdSize = idSize;\n        hv.visitHeader(text, idSize, timestamp);\n    }\n\n    private void acceptRecord(HprofVisitor hv) throws IOException {\n        try {\n            while (true) {\n                final int tag = mStreamIn.read();\n                final int timestamp = IOUtil.readBEInt(mStreamIn);\n                final long length = IOUtil.readBEInt(mStreamIn) & 0x00000000FFFFFFFFL;\n                switch (tag) {\n                    case HprofConstants.RECORD_TAG_STRING:\n                        acceptStringRecord(timestamp, length, hv);\n                        break;\n                    case HprofConstants.RECORD_TAG_LOAD_CLASS:\n                        acceptLoadClassRecord(timestamp, length, hv);","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-resource-canary/matrix-resource-canary-android/src/main/java/com/tencent/matrix/resource/hproflib/HprofReader.java#L32-L68","documentation":"HprofReader.acceptHeader() reads the idSize field from the hprof header. Every object id in the dump is sized by this value, so the reader validates it is a sane positive int (< Integer.MAX_VALUE >> 1) before proceeding; otherwise it throws IOException(\"bad idSize: ...\"), because continuing would corrupt all subsequent ID reads.","triggerScenarios":"Opening a stream with HprofReader.accept() where the bytes at the idSize position of the hprof header decode to <= 0 or >= Integer.MAX_VALUE/2 — i.e. the input is not a valid hprof or the header is misaligned.","commonSituations":"Passing a non-hprof file (log, HTML error page, empty file) to the parser; truncated download where the header read consumes wrong bytes; wrong file path opened; gzipped hprof not decompressed first.","solutions":["Verify the file starts with the hprof magic header 'JAVA PROFILE 1.0.1' / 'JAVA PROFILE 1.0.2' before parsing.","Re-transfer/re-capture the hprof; check file size and that the copy completed.","Decompress .hprof.gz files before handing them to HprofReader.","Wrap accept() in try-catch for IOException and surface a user-friendly 'invalid hprof file' error."],"exampleFix":"// before\nnew HprofReader(new BufferedInputStream(new FileInputStream(file))).accept(visitor);\n// after\ntry (FileInputStream fis = new FileInputStream(file)) {\n    byte[] magic = new byte[17];\n    if (fis.read(magic) < 17 || !new String(magic, 0, 11).equals(\"JAVA PROFILE\")) {\n        throw new IOException(\"not a valid hprof file: \" + file);\n    }\n    new HprofReader(new BufferedInputStream(fis)).accept(visitor);\n}","handlingStrategy":"validation","validationCode":"try (FileInputStream fis = new FileInputStream(file)) {\n    byte[] head = new byte[18];\n    if (fis.read(head) < 18 || !new String(head, 0, 11, \"US-ASCII\").equals(\"JAVA PROFILE\")) {\n        throw new IOException(\"not a valid hprof: \" + file);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    hprofReader.accept(visitor);\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"bad idSize\")) {\n        Log.e(TAG, \"invalid or corrupt hprof file\", e);\n    }\n}","preventionTips":["Check the 'JAVA PROFILE' magic string before parsing any hprof.","Decompress gzipped dumps before handing them to HprofReader.","Verify downloads complete (size/checksum) before parsing."],"tags":["android","hprof","file-format","corrupted-file"],"backgroundTag":"invalid-argument-format","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"}