{"record":{"id":"bca8e84179757abf","repo":"MuntashirAkon/AppManager","slug":"unexpected-magic-magic","errorCode":null,"errorMessage":"Unexpected magic ${magic}","messagePattern":"Unexpected magic (.+?)","errorType":"exception","errorClass":"java.io.IOException","httpStatus":null,"severity":"error","filePath":"libcore/compat/src/main/java/io/github/muntashirakon/compat/xml/BinaryXmlPullParser.java","lineNumber":106,"sourceCode":"\n        mIn = FastDataInput.obtainUsing4ByteSequences(is);\n\n        mCurrentToken = START_DOCUMENT;\n        mCurrentDepth = 0;\n        mCurrentName = null;\n        mCurrentText = null;\n\n        mAttributeCount = 0;\n        mAttributes = new Attribute[8];\n        for (int i = 0; i < mAttributes.length; i++) {\n            mAttributes[i] = new Attribute();\n        }\n\n        try {\n            final byte[] magic = new byte[4];\n            mIn.readFully(magic);\n            if (!Arrays.equals(magic, PROTOCOL_MAGIC_VERSION_0)) {\n                throw new IOException(\"Unexpected magic \" + bytesToHexString(magic));\n            }\n\n            // We're willing to immediately consume a START_DOCUMENT if present,\n            // but we're okay if it's missing\n            if (peekNextExternalToken() == START_DOCUMENT) {\n                consumeToken();\n            }\n        } catch (IOException e) {\n            throw new XmlPullParserException(e.toString());\n        }\n    }\n\n    @Override\n    public void setInput(Reader in) throws XmlPullParserException {\n        throw new UnsupportedOperationException();\n    }\n\n    @Override","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/compat/src/main/java/io/github/muntashirakon/compat/xml/BinaryXmlPullParser.java#L88-L124","documentation":"setInput validates the 4-byte magic header of the binary XML stream against PROTOCOL_MAGIC_VERSION_0. If the first four bytes differ, the stream is not a (v0) binary XML document and an IOException \"Unexpected magic <hex>\" is thrown.","triggerScenarios":"Calling setInput(InputStream, encoding) on a stream whose first 4 bytes are not the binary XML magic: plain-text XML, ZIP/APK data, a truncated or corrupted file, or a binary XML produced by a different/protocol-version writer.","commonSituations":"Pointing the parser at a regular XML file by mistake; reading a resource that was compressed or transformed; version drift where the writer emitted a newer magic than this parser understands; reading from an offset that skips the header.","solutions":["Verify the InputStream actually contains binary XML (check the first bytes / how the file was produced).","Use a standard XmlPullParser for plain-text XML documents.","Ensure the stream position is 0 so the magic bytes are read first; rewind or re-open the stream.","Upgrade the library if the file was written with a newer binary XML protocol version."],"exampleFix":"// before\nparser.setInput(new FileInputStream(\"doc.xml\"), null); // plain XML -> bad magic\n// after\nif (isBinaryXml(file)) parser.setInput(new FileInputStream(\"doc.binxml\"), null);\nelse parser.setInput(new InputStreamReader(new FileInputStream(\"doc.xml\"), StandardCharsets.UTF_8));","handlingStrategy":"validation","validationCode":"byte[] head = new byte[4];\nInputStream in = new FileInputStream(file);\nif (in.read(head) != 4 || !Arrays.equals(head, BinaryXmlPullParser.PROTOCOL_MAGIC_VERSION_0)) {\n    // not binary XML — use a text XML parser\n    in.close();\n}","typeGuard":null,"tryCatchPattern":"try {\n    parser.setInput(is, null);\n} catch (IOException e) {\n    // wrong format: fall back to text XML parser or report corrupt file\n}","preventionTips":["Check the file's origin/format before choosing a parser.","Ensure the stream is positioned at byte 0 when handed to setInput.","Keep writer and library versions in sync to avoid magic/protocol drift."],"tags":["xml","magic-bytes","format","io"],"backgroundTag":"checksum-mismatch","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}