@@ -226,16 +226,16 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
226
226
writer . Write ( ( byte ) '\' ' ) ;
227
227
switch ( charValue )
228
228
{
229
- case '\' ' :
230
- writer . Write ( ( byte ) '\' ' ) ;
231
- writer . Write ( ( byte ) charValue ) ;
229
+ case '\0 ' when ! noBackslashEscapes :
230
+ writer . Write ( ( ushort ) 0x305C ) ; // \0
232
231
break ;
233
232
234
- case '\\ ' :
235
- if ( ! noBackslashEscapes )
236
- writer . Write ( ( byte ) ' \\ ' ) ;
233
+ case '\' ' :
234
+ writer . Write ( ( ushort ) 0x2727 ) ; // ''
235
+ break ;
237
236
238
- writer . Write ( ( byte ) charValue ) ;
237
+ case '\\ ' when ! noBackslashEscapes :
238
+ writer . Write ( ( ushort ) 0x5C5C ) ; // \\
239
239
break ;
240
240
241
241
default :
@@ -288,7 +288,7 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
288
288
var length = inputSpan . Length + BinaryBytes . Length + 1 ;
289
289
foreach ( var by in inputSpan )
290
290
{
291
- if ( by is 0x27 || by is 0x5C && ! noBackslashEscapes )
291
+ if ( by is 0x27 || by is 0x00 or 0x5C && ! noBackslashEscapes )
292
292
length ++ ;
293
293
}
294
294
@@ -297,9 +297,18 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
297
297
var index = BinaryBytes . Length ;
298
298
foreach ( var by in inputSpan )
299
299
{
300
- if ( by is 0x27 || by is 0x5C && ! noBackslashEscapes )
300
+ if ( by == 0x00 && ! noBackslashEscapes )
301
+ {
302
+ // \0
303
+ outputSpan [ index ++ ] = 0x5C ;
304
+ outputSpan [ index ++ ] = 0x30 ;
305
+ }
306
+ else
307
+ {
308
+ if ( by is 0x27 || by is 0x5C && ! noBackslashEscapes )
309
+ outputSpan [ index ++ ] = by ;
301
310
outputSpan [ index ++ ] = by ;
302
- outputSpan [ index ++ ] = by ;
311
+ }
303
312
}
304
313
outputSpan [ index ++ ] = 0x27 ;
305
314
Debug . Assert ( index == length , "index == length" ) ;
@@ -394,9 +403,9 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
394
403
writer . Write ( BinaryBytes ) ;
395
404
foreach ( var by in bytes )
396
405
{
397
- if ( by is 0x27 or 0x5C )
406
+ if ( by is 0x00 or 0x27 or 0x5C )
398
407
writer . Write ( ( byte ) 0x5C ) ;
399
- writer . Write ( by ) ;
408
+ writer . Write ( by == 0 ? ( byte ) 0x30 : by ) ;
400
409
}
401
410
writer . Write ( ( byte ) '\' ' ) ;
402
411
}
@@ -471,7 +480,7 @@ static void WriteString(ByteBufferWriter writer, bool noBackslashEscapes, string
471
480
if ( noBackslashEscapes )
472
481
writer . Write ( value . Replace ( "'" , "''" ) ) ;
473
482
else
474
- writer . Write ( value . Replace ( "\\ " , "\\ \\ " ) . Replace ( "'" , "''" ) ) ;
483
+ writer . Write ( value . Replace ( "\\ " , "\\ \\ " ) . Replace ( "'" , "''" ) . Replace ( " \0 " , " \\ 0" ) ) ;
475
484
476
485
writer . Write ( ( byte ) '\' ' ) ;
477
486
}
@@ -485,7 +494,7 @@ static void WriteString(ByteBufferWriter writer, bool noBackslashEscapes, bool w
485
494
while ( charsWritten < value . Length )
486
495
{
487
496
var remainingValue = value . Slice ( charsWritten ) ;
488
- var nextDelimiterIndex = remainingValue . IndexOfAny ( '\' ' , '\\ ' ) ;
497
+ var nextDelimiterIndex = remainingValue . IndexOfAny ( '\0 ' , ' \ ' ', '\\ ' ) ;
489
498
if ( nextDelimiterIndex == - 1 )
490
499
{
491
500
// write the rest of the string
@@ -495,11 +504,17 @@ static void WriteString(ByteBufferWriter writer, bool noBackslashEscapes, bool w
495
504
else
496
505
{
497
506
// write up to (and including) the delimiter, then double it
498
- writer . Write ( remainingValue . Slice ( 0 , nextDelimiterIndex + 1 ) , flush : true ) ;
507
+ writer . Write ( remainingValue . Slice ( 0 , nextDelimiterIndex ) , flush : true ) ;
499
508
if ( remainingValue [ nextDelimiterIndex ] == '\\ ' && ! noBackslashEscapes )
500
- writer . Write ( ( byte ) '\\ ' ) ;
509
+ writer . Write ( ( ushort ) 0x5C5C ) ; // \\
510
+ else if ( remainingValue [ nextDelimiterIndex ] == '\\ ' && noBackslashEscapes )
511
+ writer . Write ( ( byte ) 0x5C ) ; // \
501
512
else if ( remainingValue [ nextDelimiterIndex ] == '\' ' )
502
- writer . Write ( ( byte ) '\' ' ) ;
513
+ writer . Write ( ( ushort ) 0x2727 ) ; // ''
514
+ else if ( remainingValue [ nextDelimiterIndex ] == '\0 ' && ! noBackslashEscapes )
515
+ writer . Write ( ( ushort ) 0x305C ) ; // \0
516
+ else if ( remainingValue [ nextDelimiterIndex ] == '\0 ' && noBackslashEscapes )
517
+ writer . Write ( ( byte ) 0x00 ) ; // (nul)
503
518
charsWritten += nextDelimiterIndex + 1 ;
504
519
}
505
520
}
0 commit comments