From 12e1f6be583da66843d0c4ccd366753eadf7fb4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 29 Jan 2019 11:05:55 +0100 Subject: [PATCH] [unicode] Fix stop condition of CopyAndRemoveCodePoint --- ion/include/ion/unicode/utf8_helper.h | 3 ++- ion/src/shared/unicode/utf8_helper.cpp | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ion/include/ion/unicode/utf8_helper.h b/ion/include/ion/unicode/utf8_helper.h index 14b2ab780..ffc37f38d 100644 --- a/ion/include/ion/unicode/utf8_helper.h +++ b/ion/include/ion/unicode/utf8_helper.h @@ -14,7 +14,8 @@ int CountOccurrences(const char * s, CodePoint c); const char * CodePointSearch(const char * s, CodePoint c); /* Copy src into dst while removing all code points c. Also update an index - * that should be lower if code points where removed before it. */ + * that should be lower if code points where removed before it. Ensure null- + * termination of dst. */ void CopyAndRemoveCodePoint(char * dst, size_t dstSize, const char * src, CodePoint c, const char * * indexToDUpdate = nullptr); /* Copy src into dst until end of dst or code point c, with null termination. Return the length of the copy */ diff --git a/ion/src/shared/unicode/utf8_helper.cpp b/ion/src/shared/unicode/utf8_helper.cpp index 4aed034ca..32c2cba58 100644 --- a/ion/src/shared/unicode/utf8_helper.cpp +++ b/ion/src/shared/unicode/utf8_helper.cpp @@ -55,16 +55,18 @@ const char * CodePointSearch(const char * s, CodePoint c) { } void CopyAndRemoveCodePoint(char * dst, size_t dstSize, const char * src, CodePoint c, const char * * pointerToUpdate) { + if (dstSize <= 0) { + return; + } UTF8Decoder decoder(src); const char * currentPointer = src; - const char * maxPointer = src + strlen(src) + 1; CodePoint codePoint = decoder.nextCodePoint(); const char * nextPointer = decoder.stringPosition(); size_t bufferIndex = 0; size_t codePointCharSize = UTF8Decoder::CharSizeOfCodePoint(c); // Remove CodePoint c - while (currentPointer < maxPointer && bufferIndex < dstSize) { + while (codePoint != UCodePointNull && bufferIndex < dstSize) { if (codePoint != c) { int copySize = minInt(nextPointer - currentPointer, dstSize - bufferIndex); memcpy(dst + bufferIndex, currentPointer, copySize); @@ -77,6 +79,7 @@ void CopyAndRemoveCodePoint(char * dst, size_t dstSize, const char * src, CodePo codePoint = decoder.nextCodePoint(); nextPointer = decoder.stringPosition(); } + *(dst + minInt(bufferIndex, dstSize - 1)) = 0; } size_t CopyUntilCodePoint(char * dst, size_t dstSize, const char * src, CodePoint c) {