@@ -619,12 +619,16 @@ static bool WriteSubstring(string value, ref int inputIndex, ref Encoder? utf8En
619619 if ( nextIndex == - 1 )
620620 nextIndex = value . Length ;
621621
622- utf8Encoder ??= Encoding . UTF8 . GetEncoder ( ) ;
622+ utf8Encoder ??= s_utf8Encoding . GetEncoder ( ) ;
623623#if NETSTANDARD1_3
624+ if ( output . Length <= 4 && utf8Encoder . GetByteCount ( value . ToCharArray ( ) , inputIndex , 1 , flush : false ) > output . Length )
625+ return false ;
624626 var buffer = new byte [ output . Length ] ;
625627 utf8Encoder . Convert ( value . ToCharArray ( ) , inputIndex , nextIndex - inputIndex , buffer , 0 , buffer . Length , nextIndex == value . Length , out var charsUsed , out var bytesUsed , out var completed ) ;
626628 buffer . AsSpan ( ) . CopyTo ( output ) ;
627629#else
630+ if ( output . Length <= 4 && utf8Encoder . GetByteCount ( value . AsSpan ( inputIndex , 1 ) , flush : false ) > output . Length )
631+ return false ;
628632 utf8Encoder . Convert ( value . AsSpan ( inputIndex , nextIndex - inputIndex ) , output , nextIndex == value . Length , out var charsUsed , out var bytesUsed , out var completed ) ;
629633#endif
630634
@@ -659,6 +663,7 @@ static bool WriteBytes(ReadOnlySpan<byte> value, ref int inputIndex, Span<byte>
659663
660664 private static readonly char [ ] s_specialCharacters = new char [ ] { '\t ' , '\\ ' , '\n ' } ;
661665 private static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager . CreateLogger ( nameof ( MySqlBulkCopy ) ) ;
666+ private static readonly Encoding s_utf8Encoding = new UTF8Encoding ( encoderShouldEmitUTF8Identifier : false , throwOnInvalidBytes : true ) ;
662667
663668 readonly MySqlConnection m_connection ;
664669 readonly MySqlTransaction ? m_transaction ;
0 commit comments