{"record":{"id":"6ccb03450d568a02","repo":"NationalSecurityAgency/ghidra","slug":"could-not-read-enough-bytes","errorCode":null,"errorMessage":"Could not read enough bytes","messagePattern":"Could not read enough bytes","errorType":"exception","errorClass":"MemoryAccessException","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/guest/DBTraceGuestPlatformMappedMemory.java","lineNumber":380,"sourceCode":"\t\t\t\tbuffer.position(buffer.position() + lenToRead);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tint savedLimit = buffer.limit();\n\t\t\ttry {\n\t\t\t\tbuffer.limit(buffer.position() + lenToRead);\n\t\t\t\thostSpace.getViewBytes(snap, hostCur, buffer);\n\t\t\t}\n\t\t\tfinally {\n\t\t\t\tbuffer.limit(savedLimit);\n\t\t\t}\n\t\t}\n\t\treturn buffer.position() - startPos;\n\t}\n\n\tprotected ByteBuffer getBytesInFull(Address address, int len) throws MemoryAccessException {\n\t\tByteBuffer buf = ByteBuffer.allocate(len);\n\t\tif (getBytes(buf, address) != len) {\n\t\t\tthrow new MemoryAccessException(\"Could not read enough bytes\");\n\t\t}\n\t\tif (!isBigEndian()) {\n\t\t\tbuf.order(ByteOrder.LITTLE_ENDIAN);\n\t\t}\n\t\treturn buf;\n\t}\n\n\t@Override\n\tpublic byte getByte(Address addr) throws MemoryAccessException {\n\t\tbyte[] val = new byte[1];\n\t\tif (getBytes(addr, val) < 1) {\n\t\t\tthrow new MemoryAccessException(\"Guest address \" + addr.toString() +\n\t\t\t\t\" is not mapped, or the mapped address does not exist in host memory\");\n\t\t}\n\t\treturn val[0];\n\t}\n\n\t@Override","sourceCodeStart":362,"sourceCodeEnd":398,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/guest/DBTraceGuestPlatformMappedMemory.java#L362-L398","documentation":"getBytesInFull allocates a buffer of len and calls getBytes(buf, address); if fewer than len bytes were read it throws MemoryAccessException(\"Could not read enough bytes\"). The mapped memory could not satisfy the full requested length, typically because the read crosses the end of a mapped host region or runs into unmapped host backing.","triggerScenarios":"Reading a multi-byte value (int/long) that straddles the boundary of a mapped host region; reading past a partial mapping; getBytes returning short due to unmapped host snapshot.","commonSituations":"Emulation dereferencing guest memory near range edges; reading values that cross mapping boundaries; snapshot where part of the host range has no written bytes.","solutions":["Reduce the requested length or read byte-wise across the boundary.","Ensure the full host range backing the guest address is populated for the snapshot before reading.","Catch MemoryAccessException and degrade to a partial read."],"exampleFix":"// before\nByteBuffer buf = mem.getBytesInFull(addr, 8);\n\n// after\nByteBuffer buf = ByteBuffer.allocate(8);\nint got = mem.getBytes(buf, addr);\nif (got != 8) { /* handle partial read */ }","handlingStrategy":"try-catch","validationCode":"ByteBuffer buf = ByteBuffer.allocate(len);\nint got = mem.getBytes(buf, address);\nif (got < len) { /* shorten read or ensure full host backing */ }","typeGuard":null,"tryCatchPattern":"try { ByteBuffer buf = mem.getBytesInFull(addr, len); }\ncatch (MemoryAccessException e) { /* partial/short read: handle gracefully */ }","preventionTips":["Read byte-wise when crossing mapping boundaries.","Populate the full host backing range for the snapshot before multi-byte reads.","Treat short reads as expected near range edges, not as fatal."],"tags":["memory","read","mapping"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}