@@ -456,14 +456,28 @@ public static void SerializeString_Special_Characters_Do_Not_Throw(char special)
456456 public static void SerializeString_Buffer_Too_Small_Throws ( NonEmptyString input )
457457 {
458458 // Arrange
459- var str = input . Get . Substring ( 0 , Math . Min ( input . Get . Length , 10 ) ) ; // Limit string size
460- var requiredSize = System . Text . Encoding . UTF8 . GetByteCount ( str ) + sizeof ( short ) ;
459+ var value = input . Get . Substring ( 0 , Math . Min ( input . Get . Length , 10 ) ) ; // Limit string size
460+ var requiredSize = System . Text . Encoding . UTF8 . GetByteCount ( value ) + sizeof ( short ) ;
461461 var bufferSize = Math . Max ( 1 , requiredSize / 2 ) ; // Intentionally too small
462462 var buffer = new byte [ bufferSize ] ;
463463 var offset = 0 ;
464464
465465 // Act
466- var ex = Assert . ThrowsAny < Exception > ( ( ) => MetricSerializer . SerializeString ( buffer , ref offset , str ) ) ;
466+ var ex = Assert . ThrowsAny < Exception > ( ( ) => MetricSerializer . SerializeString ( buffer , ref offset , value ) ) ;
467+
468+ // Assert
469+ Assert . True ( ex is ArgumentException or IndexOutOfRangeException , $ "Unexpected exception type { ex . GetType ( ) } .") ;
470+ }
471+
472+ [ Property ( MaxTest = MaxValue , Replay = "(11934653490780154147,14955749465213992389,100)" ) ]
473+ public static void SerializeString_Buffer_Too_Small_Throws_Repro ( NonEmptyString input , PositiveInt nearEnd )
474+ {
475+ // Arrange
476+ var buffer = new byte [ 100 ] ;
477+ var offset = Math . Min ( nearEnd . Get % 100 , 47 ) ; // Start near or at the end
478+
479+ // Act
480+ var ex = Assert . ThrowsAny < Exception > ( ( ) => MetricSerializer . SerializeString ( buffer , ref offset , input . Get ) ) ;
467481
468482 // Assert
469483 Assert . True ( ex is ArgumentException or IndexOutOfRangeException , $ "Unexpected exception type { ex . GetType ( ) } .") ;
@@ -475,6 +489,12 @@ public static void SerializeString_Offset_Near_Buffer_End_Does_Not_Throw(NonEmpt
475489 // Arrange
476490 var buffer = new byte [ 100 ] ;
477491 var offset = Math . Min ( nearEnd . Get % 100 , 99 ) ; // Start near or at the end
492+ var encodedLength = System . Text . Encoding . UTF8 . GetByteCount ( input . Get ) ;
493+
494+ if ( encodedLength > ( buffer . Length - offset - sizeof ( short ) ) )
495+ {
496+ return ;
497+ }
478498
479499 // Act
480500 MetricSerializer . SerializeString ( buffer , ref offset , input . Get ) ;
0 commit comments