[ion] UTF8Helper: fix CopyAndRemoveCodePoint to avoid copying truncated

code point in the buffer
This commit is contained in:
Émilie Feral
2019-11-25 14:39:58 +01:00
committed by LeaNumworks
parent 967dc0ea15
commit 4c11e73ad3
2 changed files with 21 additions and 3 deletions

View File

@@ -5,7 +5,6 @@
namespace UTF8Helper {
static inline int minInt(int x, int y) { return x < y ? x : y; }
static inline size_t minSizeT(size_t x, size_t y) { return x < y ? x : y; }
int CountOccurrences(const char * s, CodePoint c) {
@@ -110,7 +109,11 @@ void CopyAndRemoveCodePoint(char * dst, size_t dstSize, const char * src, CodePo
// Remove CodePoint c
while (codePoint != UCodePointNull && bufferIndex < dstSize) {
if (codePoint != c) {
int copySize = minInt(nextPointer - currentPointer, dstSize - bufferIndex);
int copySize = nextPointer - currentPointer;
if (copySize > dstSize - 1 - bufferIndex) {
// Copying the current code point to the buffer would overflow the buffer
break;
}
memcpy(dst + bufferIndex, currentPointer, copySize);
bufferIndex+= copySize;
}
@@ -118,7 +121,7 @@ void CopyAndRemoveCodePoint(char * dst, size_t dstSize, const char * src, CodePo
codePoint = decoder.nextCodePoint();
nextPointer = decoder.stringPosition();
}
*(dst + minInt(bufferIndex, dstSize - 1)) = 0;
*(dst + bufferIndex) = 0;
}
void RemoveCodePoint(char * buffer, CodePoint c, const char * * pointerToUpdate, const char * stoppingPosition) {

View File

@@ -87,6 +87,21 @@ QUIZ_CASE(ion_utf8_copy_and_remove_code_point) {
c = UCodePointLatinLetterSmallCapitalE;
result = "124";
assert_copy_and_remove_code_point_gives(buffer, bufferSize, s, c, result);
// The buffer size is to small to hold s
s = "1234ᴇ";
c = '5';
result = "1234"; // "1234ᴇ" size is 7
assert_copy_and_remove_code_point_gives(buffer, 6, s, c, result);
assert_copy_and_remove_code_point_gives(buffer, 7, s, c, result);
result = "1234ᴇ";
assert_copy_and_remove_code_point_gives(buffer, 8, s, c, result);
s = "1234ᴇ";
c = '4';
result = "123ᴇ";
assert_copy_and_remove_code_point_gives(buffer, 7, s, c, result);
}
void assert_remove_code_point_gives(char * buffer, CodePoint c, const char * * indexToUpdate, const char * stoppingPosition, const char * indexToUpdateResult, const char * result) {