Repository navigation
Accept messages that exactly fill the send buffer #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vickenty
merged 4 commits into
DataDog:master
from
mar-kolya:mar-kolya/fix-exact-fit-buffer-overflow
Nov 17, 2026
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c23e25e
Accept messages that exactly fill the send buffer
mar-kolya 5e8e788
Test exact-fit writes on StatsDProcessor directly
mar-kolya 4c35740
Extract the UTF-8 encoder and test it directly
mar-kolya 4c7094f
Move the + 4 comment next to the + 4
mar-kolya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/test/java/com/timgroup/statsd/StatsDProcessorEncoderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package com.timgroup.statsd; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import java.nio.BufferOverflowException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import org.junit.Test; | ||
|
|
||
| public class StatsDProcessorEncoderTest { | ||
|
|
||
| private static final int MAX_PACKET_SIZE = 64; | ||
|
|
||
| private final StatsDProcessor.Encoder encoder = new StatsDProcessor.Encoder(MAX_PACKET_SIZE); | ||
| private final ByteBuffer sendBuffer = ByteBuffer.allocate(MAX_PACKET_SIZE); | ||
|
|
||
| @Test | ||
| public void encodes_message_that_exactly_fills_the_packet() { | ||
| assertEncodesExactFit(message("", MAX_PACKET_SIZE)); | ||
| } | ||
|
|
||
| @Test | ||
| public void encodes_two_byte_chars_that_exactly_fill_the_packet() { | ||
| assertEncodesExactFit(message("é", MAX_PACKET_SIZE - 1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void encodes_three_byte_chars_that_exactly_fill_the_packet() { | ||
| assertEncodesExactFit(message("€", MAX_PACKET_SIZE - 2)); | ||
| } | ||
|
|
||
| @Test | ||
| public void encodes_surrogate_pair_that_exactly_fills_the_packet() { | ||
| // Two chars, four bytes. | ||
| assertEncodesExactFit(message("😀", MAX_PACKET_SIZE - 2)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejects_message_one_byte_longer_than_the_packet() { | ||
| assertRejects(message("", MAX_PACKET_SIZE + 1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejects_multibyte_message_one_byte_longer_than_the_packet() { | ||
| // As many chars as fit in a packet, but 'é' takes two bytes. | ||
| assertRejects(message("é", MAX_PACKET_SIZE)); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejects_message_longer_than_the_space_left() { | ||
| sendBuffer.put(new byte[10]); | ||
|
|
||
| assertRejects(message("", MAX_PACKET_SIZE - 9)); | ||
| } | ||
|
|
||
| /** {@code length} chars: {@code head}, then ASCII filler. */ | ||
| private static String message(String head, int length) { | ||
| char[] filler = new char[length - head.length()]; | ||
| Arrays.fill(filler, 'x'); | ||
| return head + new String(filler); | ||
| } | ||
|
|
||
| private void assertEncodesExactFit(String message) { | ||
| assertEquals(MAX_PACKET_SIZE, message.getBytes(StandardCharsets.UTF_8).length); | ||
|
|
||
| encoder.encode(new StringBuilder(message), sendBuffer); | ||
|
|
||
| sendBuffer.flip(); | ||
| assertEquals(message, StandardCharsets.UTF_8.decode(sendBuffer).toString()); | ||
| } | ||
|
|
||
| private void assertRejects(String message) { | ||
| int position = sendBuffer.position(); | ||
| try { | ||
| encoder.encode(new StringBuilder(message), sendBuffer); | ||
| fail("expected BufferOverflowException"); | ||
| } catch (BufferOverflowException expected) { | ||
| // Nothing is written on overflow. | ||
| assertEquals(position, sendBuffer.position()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to move this comment to where we add 4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, moved it to the constructor line in 4c7094f.
🤖 Generated by Claude Opus 5.5 with Claude Code