Skip to content

Fix ByteBuffer copy offsets for sliced and read-only buffers - #1037

Open
PHJ2000 wants to merge 1 commit into
msgpack:mainfrom
PHJ2000:fix-byte-buffer-copy-offsets
Open

PHJ2000 wants to merge 1 commit into
msgpack:mainfrom
PHJ2000:fix-byte-buffer-copy-offsets

Conversation

@PHJ2000

@PHJ2000 PHJ2000 commented Oct 22, 2026

Copy link
Copy Markdown

Fixes #1036.

MessageBuffer.putByteBuffer can copy the wrong bytes from a sliced heap source, overwrite bytes before a sliced heap destination when reading a read-only source, or repeatedly overwrite the first destination byte when copying a read-only heap source into direct storage.

Include the source/destination array offsets and advance the direct destination address for each byte. Add a 64-case regression matrix across heap/direct storage, read-only sources, source/destination slices, and empty/partial copies. The assertions also cover untouched destination bytes and source position, limit, and mark. Ten cases fail before the fix; all pass afterward with both the default and universal buffer implementations.

Validation:

  • OpenJDK 21: ./sbt -batch scalafmtCheckAll jcheckStyle and ./sbt -batch 'msgpack-core/testFull' passed (257 tests, 1 existing pending test).
  • Java 8: the same 257 core tests passed with TEST_JAVA_HOME set to the Java 8 installation and the session setting set msgpackCore / javacOptions ++= Seq("-bootclasspath", "<jdk8>/jre/lib/rt.jar"). Without this setting, the build reused production classes linked against Java 9+ covariant NIO methods and failed with NoSuchMethodError on Java 8.
  • Universal mode (JAVA_TOOL_OPTIONS=-Dmsgpack.universal-buffer=true): all 64 new cases passed. The full core suite had one existing DirectBufferAccessTest initialization error because MessageBuffer.unsafe is null; an isolated initialization probe fails identically against the original and patched classes.
  • The sbt build did not discover the Jackson JUnit tests. Running them explicitly with JUnit Platform 1.14.4 found 77 tests, with 50 passing and 27 fixture-initialization failures. The exact same 27 failures occur with the original MessageBuffer class: the shared fixture uses JUnit 4 lifecycle annotations while the affected tests use Jupiter annotations.

No build settings or unrelated test fixtures are changed in this PR.

@github-actions github-actions Bot added the bug label Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MessageBuffer.putByteBuffer corrupts copies from sliced or read-only heap buffers

1 participant