@@ -619,12 +619,16 @@ static bool WriteSubstring(string value, ref int inputIndex, ref Encoder? utf8En
619
619
if ( nextIndex == - 1 )
620
620
nextIndex = value . Length ;
621
621
622
- utf8Encoder ??= Encoding . UTF8 . GetEncoder ( ) ;
622
+ utf8Encoder ??= s_utf8Encoding . GetEncoder ( ) ;
623
623
#if NETSTANDARD1_3
624
+ if ( output . Length <= 4 && utf8Encoder . GetByteCount ( value . ToCharArray ( ) , inputIndex , 1 , flush : false ) > output . Length )
625
+ return false ;
624
626
var buffer = new byte [ output . Length ] ;
625
627
utf8Encoder . Convert ( value . ToCharArray ( ) , inputIndex , nextIndex - inputIndex , buffer , 0 , buffer . Length , nextIndex == value . Length , out var charsUsed , out var bytesUsed , out var completed ) ;
626
628
buffer . AsSpan ( ) . CopyTo ( output ) ;
627
629
#else
630
+ if ( output . Length <= 4 && utf8Encoder . GetByteCount ( value . AsSpan ( inputIndex , 1 ) , flush : false ) > output . Length )
631
+ return false ;
628
632
utf8Encoder . Convert ( value . AsSpan ( inputIndex , nextIndex - inputIndex ) , output , nextIndex == value . Length , out var charsUsed , out var bytesUsed , out var completed ) ;
629
633
#endif
630
634
@@ -659,6 +663,7 @@ static bool WriteBytes(ReadOnlySpan<byte> value, ref int inputIndex, Span<byte>
659
663
660
664
private static readonly char [ ] s_specialCharacters = new char [ ] { '\t ' , '\\ ' , '\n ' } ;
661
665
private static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager . CreateLogger ( nameof ( MySqlBulkCopy ) ) ;
666
+ private static readonly Encoding s_utf8Encoding = new UTF8Encoding ( encoderShouldEmitUTF8Identifier : false , throwOnInvalidBytes : true ) ;
662
667
663
668
readonly MySqlConnection m_connection ;
664
669
readonly MySqlTransaction ? m_transaction ;
0 commit comments