Skip to content

Commit a1ee07c

Browse files
committed
Fix Prefast warning in Utf8 code
1 parent cf71a96 commit a1ee07c

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lib/Common/Codex/Utf8Codex.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ namespace utf8
433433
}
434434

435435
template <Utf8EncodingKind encoding, bool countBytesOnly = false>
436-
__range(0, cchSource * 3)
436+
__range(0, cbDest)
437437
size_t EncodeIntoImpl(
438438
_When_(!countBytesOnly, _Out_writes_(cbDest)) utf8char_t *destBuffer,
439439
__range(0, cchSource * 3) size_t cbDest,
@@ -492,7 +492,7 @@ namespace utf8
492492
}
493493

494494
template <Utf8EncodingKind encoding>
495-
__range(0, cchSource * 3)
495+
__range(0, cbDest)
496496
size_t EncodeInto(
497497
_Out_writes_(cbDest) utf8char_t *dest,
498498
__range(0, cchSource * 3) size_t cbDest,
@@ -503,7 +503,7 @@ namespace utf8
503503
}
504504

505505
template <Utf8EncodingKind encoding>
506-
__range(0, cchSource * 3)
506+
__range(0, cbDest)
507507
size_t EncodeIntoAndNullTerminate(
508508
_Out_writes_z_(cbDest) utf8char_t *dest,
509509
__range(1, cchSource * 3 + 1) size_t cbDest, // must be at least large enough to write null terminator
@@ -513,45 +513,46 @@ namespace utf8
513513
size_t destWriteMaxBytes = cbDest - 1; // leave room for null terminator
514514
size_t result = EncodeIntoImpl<encoding>(dest, destWriteMaxBytes, source, cchSource);
515515
dest[result] = 0;
516-
return result;
516+
return result + 1;
517517
}
518518

519519
template
520-
__range(0, cchSource * 3)
520+
__range(0, cbDest)
521521
size_t EncodeInto<Utf8EncodingKind::Cesu8>(
522522
_Out_writes_(cbDest) utf8char_t *dest,
523523
__range(0, cchSource * 3) size_t cbDest,
524524
_In_reads_(cchSource) const char16 *source,
525525
__range(0, INT_MAX) charcount_t cchSource);
526526

527527
template
528-
__range(0, cchSource * 3)
528+
__range(0, cbDest)
529529
size_t EncodeInto<Utf8EncodingKind::TrueUtf8>(
530530
_Out_writes_(cbDest) utf8char_t *dest,
531531
__range(0, cchSource * 3) size_t cbDest,
532532
_In_reads_(cchSource) const char16 *source,
533533
__range(0, INT_MAX) charcount_t cchSource);
534534

535535
template
536-
__range(0, cchSource * 3)
536+
__range(0, cbDest)
537537
size_t EncodeIntoAndNullTerminate<Utf8EncodingKind::Cesu8>(
538538
_Out_writes_z_(cbDest) utf8char_t *dest,
539539
__range(1, cchSource * 3 + 1) size_t cbDest,
540540
_In_reads_(cchSource) const char16 *source,
541541
__range(0, INT_MAX) charcount_t cchSource);
542542

543543
template
544-
__range(0, cchSource * 3)
544+
__range(0, cbDest)
545545
size_t EncodeIntoAndNullTerminate<Utf8EncodingKind::TrueUtf8>(
546546
_Out_writes_z_(cbDest) utf8char_t *dest,
547547
__range(1, cchSource * 3 + 1) size_t cbDest,
548548
_In_reads_(cchSource) const char16 *source,
549549
__range(0, INT_MAX) charcount_t cchSource);
550550

551+
// Since we are not actually encoding, the return value is bounded on cch
551552
__range(0, cch * 3)
552553
size_t CountTrueUtf8(__in_ecount(cch) const char16 *source, charcount_t cch)
553554
{
554-
return EncodeIntoImpl<Utf8EncodingKind::TrueUtf8, true>(nullptr, 0, source, cch);
555+
return EncodeIntoImpl<Utf8EncodingKind::TrueUtf8, true /*count only*/>(nullptr, 0, source, cch);
555556
}
556557

557558
// Convert the character index into a byte index.

lib/Common/Codex/Utf8Helper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ namespace utf8
3232
return E_OUTOFMEMORY;
3333
}
3434

35-
size_t cbDestString = (cchSourceString + 1) * 3;
35+
// Multiply by 3 for max size of encoded character, plus 1 for the null terminator (don't need 3 bytes for the null terminator)
36+
size_t cbDestString = (cchSourceString * 3) + 1;
3637

3738
// Check for overflow- cbDestString should be >= cchSourceString
3839
if (cbDestString < cchSourceString)

0 commit comments

Comments
 (0)