This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git


The following commit(s) were added to refs/heads/master by this push:
     new 8e3019491 SUREFIRE-2056 BufferOverflowException when encoding message 
with null testId (#506)
8e3019491 is described below

commit 8e301949173f20971c288484e5711b5496e5df49
Author: Yoann Rodière <yo...@hibernate.org>
AuthorDate: Wed Apr 6 11:06:18 2022 +0200

    SUREFIRE-2056 BufferOverflowException when encoding message with null 
testId (#506)
    
    [SUREFIRE-2056] Reproduce buffer overflow in EventChannelEncoder when the 
test ID is null
---
 .../surefire/booter/spi/EventChannelEncoder.java   |  2 +-
 .../booter/spi/EventChannelEncoderTest.java        | 26 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git 
a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/EventChannelEncoder.java
 
b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/EventChannelEncoder.java
index b4a234d49..dbc3d52f4 100644
--- 
a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/EventChannelEncoder.java
+++ 
b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/EventChannelEncoder.java
@@ -393,7 +393,7 @@ public class EventChannelEncoder extends EventEncoder 
implements MasterProcessCh
     {
         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;
diff --git 
a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/spi/EventChannelEncoderTest.java
 
b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/spi/EventChannelEncoderTest.java
index 5b28d3a13..b96a26622 100644
--- 
a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/spi/EventChannelEncoderTest.java
+++ 
b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/spi/EventChannelEncoderTest.java
@@ -1226,6 +1226,32 @@ public class EventChannelEncoderTest
                 .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

Reply via email to