{"record":{"id":"4540d76c2e21f162","repo":"MuntashirAkon/AppManager","slug":"invalid-message-length","errorCode":null,"errorMessage":"Invalid message length: ","messagePattern":"Invalid message length: ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"libserver/src/main/java/io/github/muntashirakon/AppManager/server/common/DataTransmission.java","lineNumber":137,"sourceCode":"                throw new IOException(\"Message is too large: \" + messageBytes.length);\n            }\n            mOutputStream.writeInt(messageBytes.length);\n            mOutputStream.write(messageBytes);\n            mOutputStream.flush();\n        }\n    }\n\n    /**\n     * Read response as bytes after sending a message\n     *\n     * @return The bytes to be read\n     * @throws IOException When it fails to read the message\n     */\n    @NonNull\n    private byte[] readMessage() throws IOException {\n        int len = mInputStream.readInt();\n        if (len < 0 || len > MAX_MESSAGE_SIZE) {\n            throw new IOException(\"Invalid message length: \" + len);\n        }\n        byte[] bytes = new byte[len];\n        mInputStream.readFully(bytes, 0, len);\n        return bytes;\n    }\n\n    /**\n     * Send and receive messages at the same time (half-duplex)\n     *\n     * @param messageBytes Bytes to be sent\n     * @return Bytes to be read\n     * @throws IOException When it fails to send or read the message\n     * @see #sendMessage(String)\n     * @see #sendMessage(byte[])\n     */\n    @NonNull\n    public synchronized byte[] sendAndReceiveMessage(@NonNull byte[] messageBytes) throws IOException {\n        sendMessage(messageBytes);","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libserver/src/main/java/io/github/muntashirakon/AppManager/server/common/DataTransmission.java#L119-L155","documentation":"readMessage() reads a 4-byte length prefix and validates it before allocating; a negative length or one exceeding MAX_MESSAGE_SIZE indicates a corrupted or desynchronized frame, so it throws IOException(\"Invalid message length: \" + len). This prevents huge or nonsensical allocations from untrusted input.","triggerScenarios":"The peer sends malformed frames: stream desynchronization (offset misalignment after a failed write), a corrupted length header, a hostile client/server sending garbage, or reading with a stale/different framing version.","commonSituations":"Protocol version mismatch between client and server; data written without flush causing byte misalignment; MITM or injected garbage on the socket; reading after the peer died mid-frame.","solutions":["Verify both ends use the same DataTransmission framing/protocol version","Ensure every sendMessage is flushed and lengths written on frame boundaries; resync or restart the connection on error","Validate/sanitize any untrusted input before sending to keep frames well-formed","Catch the IOException and re-establish the session (re-run shakeHands)"],"exampleFix":"// before\nbyte[] msg = transmission.sendAndReceiveMessage(request);\n// after\nbyte[] msg;\ntry {\n    msg = transmission.sendAndReceiveMessage(request);\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Invalid message length\")) {\n        transmission = recreateAndHandshake();\n        msg = transmission.sendAndReceiveMessage(request);\n    } else throw e;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    byte[] msg = transmission.sendAndReceiveMessage(request);\n} catch (IOException e) {\n    // Invalid message length: connection is desynchronized; re-establish session\n    transmission = recreateAndHandshake();\n}","preventionTips":["Keep client and server protocol versions in sync","Always write and flush complete frames","Re-handshake on any framing error rather than continuing to read","Never feed untrusted, unvalidated input through the channel"],"tags":["ipc","protocol","corrupt-frame"],"backgroundTag":"unexpected-response-shape","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"}