Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions zstd/src/main/java/io/github/dfa1/zstd/ZstdSkippableContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
/// @param magicVariant the variant 0..15 the frame was written with
public record ZstdSkippableContent(byte[] content, int magicVariant) {

/// Defensively copies `content` so the record owns its bytes and cannot be
/// mutated through the array the caller passed in.
public ZstdSkippableContent {
content = content.clone();
}

/// The embedded content bytes, as a fresh copy so the record stays immutable.
///
/// @return a copy of the content bytes
@Override
public byte[] content() {
return content.clone();
}

/// Value equality over the payload and variant, comparing `content` by its
/// bytes rather than by array identity (the record default).
///
Expand Down
14 changes: 14 additions & 0 deletions zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ void standardFrameIsNotSkippable() {
assertThat(ZstdFrame.isSkippableFrame(Zstd.compress(PAYLOAD))).isFalse();
}

@Test
void defensivelyCopiesContentInAndOut() {
// Given a backing array wrapped in a skippable-content value
byte[] backing = "metadata".getBytes(StandardCharsets.UTF_8);
ZstdSkippableContent content = new ZstdSkippableContent(backing, 2);

// When the source array and a value returned by the accessor are mutated
backing[0] = 'X';
content.content()[1] = 'X';

// Then the record's own bytes are untouched
assertThat(content.content()).isEqualTo("metadata".getBytes(StandardCharsets.UTF_8));
}

@Test
void contentHasValueEqualityOverTheBytesNotArrayIdentity() {
// Given two separately built payloads with the same bytes and variant, and one differing
Expand Down
Loading