diff --git a/ion/src/shared/unicode/utf8_helper.cpp b/ion/src/shared/unicode/utf8_helper.cpp index f8c6345fc..d69016075 100644 --- a/ion/src/shared/unicode/utf8_helper.cpp +++ b/ion/src/shared/unicode/utf8_helper.cpp @@ -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) { diff --git a/ion/test/utf8_helper.cpp b/ion/test/utf8_helper.cpp index 6398df9cd..cd1d1f36b 100644 --- a/ion/test/utf8_helper.cpp +++ b/ion/test/utf8_helper.cpp @@ -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) {