Skip to content
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

[SUREFIRE-2056] BufferOverflowException when encoding message with null testId #506

Merged
merged 2 commits into from Apr 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -393,7 +393,7 @@ ByteBuffer encodeMessage( ForkedProcessEventType eventType, RunMode runMode, Lon
{
CharsetEncoder encoder = newCharsetEncoder();
int bufferMaxLength = estimateBufferLength( eventType.getOpcode().length(), runMode, encoder, 0,
testRunId == null ? 0 : 1, message );
1, message );
ByteBuffer result = ByteBuffer.allocate( bufferMaxLength );
encode( encoder, result, eventType, runMode, testRunId, message );
return result;
Expand Down
Expand Up @@ -1226,6 +1226,32 @@ public void testStdErrStreamLn() throws IOException
.isEqualTo( expected );
}

@Test
public void testStdErrStreamEmptyMessageNullTestId() throws IOException
{
Stream out = Stream.newStream();
WritableBufferedByteChannel channel = newBufferedChannel( out );
EventChannelEncoder encoder = new EventChannelEncoder( channel );

// This used to produce a BufferOverflowException; see SUREFIRE-2056.
// In essence, we used to under-allocate for the encoding of a null test ID
// (we used to allocate 0 byte instead of 1 byte).
// The message needs to be empty in order to reproduce the bug,
// otherwise we over-allocate for the test message
// (for safety, due to unpredictability of the size of encoded text)
// and this over-allocation ends up compensating the under-allocation for the null test id.
encoder.testOutput( new TestOutputReportEntry( stdErr( "" ), NORMAL_RUN, null ) );
channel.close();

String expected = ":maven-surefire-event:\u000e:std-err-stream:"
+ (char) 10 + ":normal-run:\u0000:"
+ "\u0005:UTF-8:\u0000\u0000\u0000\u0000::";

assertThat( new String( out.toByteArray(), UTF_8 ) )
.isEqualTo( expected );
}


@Test
@SuppressWarnings( "checkstyle:innerassignment" )
public void shouldCountSameNumberOfSystemProperties() throws IOException
Expand Down