{"record":{"id":"a6cbfc68484fd2c7","repo":"Tencent/matrix","slug":"bad-idsize-a6cbfc","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/HprofWriter.java","lineNumber":47,"sourceCode":" * Created by tangyinsheng on 2017/6/27.\n */\n\npublic class HprofWriter extends HprofVisitor {\n    private final OutputStream mStreamOut;\n    private int mIdSize = 0;\n\n    private final ByteArrayOutputStream mHeapDumpOut = new ByteArrayOutputStream();\n\n    public HprofWriter(OutputStream os) {\n        super(null);\n        mStreamOut = os;\n    }\n\n    @Override\n    public void visitHeader(String text, int idSize, long timestamp) {\n        try {\n            if (idSize <= 0 || idSize >= (Integer.MAX_VALUE >> 1)) {\n                throw new IOException(\"bad idSize: \" + idSize);\n            }\n            mIdSize = idSize;\n            IOUtil.writeNullTerminatedString(mStreamOut, text);\n            IOUtil.writeBEInt(mStreamOut, idSize);\n            IOUtil.writeBELong(mStreamOut, timestamp);\n        } catch (Throwable thr) {\n            throw new RuntimeException(thr);\n        }\n    }\n\n    @Override\n    public void visitStringRecord(ID id, String text, int timestamp, long length) {\n        try {\n            mStreamOut.write(HprofConstants.RECORD_TAG_STRING);\n            IOUtil.writeBEInt(mStreamOut, timestamp);\n            IOUtil.writeBEInt(mStreamOut, (int) length);\n            mStreamOut.write(id.getBytes());\n            IOUtil.writeString(mStreamOut, text);","sourceCodeStart":29,"sourceCodeEnd":65,"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/HprofWriter.java#L29-L65","documentation":"HprofWriter.visitHeader() validates the hprof header's idSize field before writing it. An idSize that is zero, negative, or >= Integer.MAX_VALUE>>1 would corrupt every subsequent ID-bearing record, so the writer rejects it with an IOException. This guards against upstream readers producing garbage header data.","triggerScenarios":"Converting or rewriting an hprof whose header was parsed with a bad idSize; feeding an HprofWriter from a reader whose source file header is corrupt; passing idSize read as 0 from an empty/truncated header.","commonSituations":"hprof post-processing pipelines (e.g. trimming or re-serializing dumps) that read a truncated header; hprof files generated by non-standard tools writing idSize 0.","solutions":["Inspect the source hprof header bytes and fix/correct the idSize (typically 4 or 8).","Re-capture the heap dump if the source file header is corrupt.","Ensure the reader feeding the writer was constructed with the correct header parsing (check the first bytes: 'java profile 1.0.1' + idSize int).","If rewriting dumps programmatically, hard-code/validate idSize as 4 or 8 before writing."],"exampleFix":"// before: passing through whatever the reader got\nwriter.visitHeader(text, readerHeaderIdSize, timestamp);\n\n// after: validate before writing\nint idSize = readerHeaderIdSize;\nif (idSize != 4 && idSize != 8) {\n    throw new IOException(\"unexpected hprof idSize: \" + idSize);\n}\nwriter.visitHeader(text, idSize, timestamp);","handlingStrategy":"validation","validationCode":"static void validateIdSize(int idSize) {\n    if (idSize != 4 && idSize != 8)\n        throw new IllegalArgumentException(\"hprof idSize must be 4 or 8, got \" + idSize);\n}","typeGuard":null,"tryCatchPattern":"try {\n    writer.visitHeader(text, idSize, timestamp);\n} catch (IOException e) {\n    if (e.getMessage().startsWith(\"bad idSize\")) {\n        // fix/normalize idSize before retrying\n    }\n}","preventionTips":["Always propagate idSize from a correctly parsed hprof header","Assert idSize is 4 or 8 in pipeline code before writing","Never hand-construct headers from guessed values"],"tags":["hprof","validation","android","memory-analysis"],"backgroundTag":"invalid-argument-value","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"}