From 2aa3dbef78a3c1075817b0fabfdc8ce1aebe1932 Mon Sep 17 00:00:00 2001 From: chenkeyu Date: Fri, 23 Aug 2024 14:32:04 +0800 Subject: [PATCH 1/3] check utf8Len to prevent add overflow Issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/IAIJLO?from=project-issue Signed-off-by: chenkeyu --- base/src/string_ex.cpp | 2 +- base/src/unicode_ex.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/base/src/string_ex.cpp b/base/src/string_ex.cpp index f54e7bf..eb56b0d 100644 --- a/base/src/string_ex.cpp +++ b/base/src/string_ex.cpp @@ -297,7 +297,7 @@ int Char16ToChar8(const u16string& str16, char *buffer, int bufferLen) } const char16_t *utf16Str = str16.c_str(); int utf8Len = Utf16ToUtf8Length(utf16Str, str16Len); - if (utf8Len < 0 || (utf8Len + 1) > bufferLen) { + if (utf8Len < 0 || utf8Len >= INT_MAX || (utf8Len + 1) > bufferLen) { UTILS_LOGD("utf8buffer len:%{public}d, actual buffer len:%{public}d!", utf8Len + 1, bufferLen); return CHAR16_TO_CHAR8_INSUFFICIENT_BUFFER; } diff --git a/base/src/unicode_ex.cpp b/base/src/unicode_ex.cpp index c21e39e..ff455be 100644 --- a/base/src/unicode_ex.cpp +++ b/base/src/unicode_ex.cpp @@ -181,7 +181,7 @@ char* Char16ToChar8(const char16_t* str16, size_t str16Len) { char* str8 = nullptr; int utf8Len = Utf16ToUtf8Length(str16, str16Len); - if (utf8Len < 0) { + if (utf8Len < 0 || utf8Len >= INT_MAX) { return nullptr; } -- Gitee From d2ffdea257692977a2848420e5d2249fe4cce995 Mon Sep 17 00:00:00 2001 From: chenkeyu Date: Tue, 27 Aug 2024 09:30:52 +0800 Subject: [PATCH 2/3] use macro to exclude WriteStringDataLength in 32bit machine Issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/IAMO4E?from=project-issue Signed-off-by: chenkeyu --- base/test/unittest/common/utils_parcel_test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/base/test/unittest/common/utils_parcel_test.cpp b/base/test/unittest/common/utils_parcel_test.cpp index 65556dd..0961b58 100644 --- a/base/test/unittest/common/utils_parcel_test.cpp +++ b/base/test/unittest/common/utils_parcel_test.cpp @@ -2202,6 +2202,7 @@ HWTEST_F(UtilsParcelTest, test_VectorDataPadding_004, TestSize.Level0) } } +#ifdef __aarch64__ HWTEST_F(UtilsParcelTest, test_WriteStringDataLength_001, TestSize.Level0) { Parcel parcel1(nullptr); @@ -2248,5 +2249,6 @@ HWTEST_F(UtilsParcelTest, test_WriteStringDataLength_001, TestSize.Level0) EXPECT_EQ(result, true); parcel1.FlushBuffer(); } +#endif } // namespace } // namespace OHOS -- Gitee From 18aae7b32b061dc1491b35707290c62fcecd1ec8 Mon Sep 17 00:00:00 2001 From: chenkeyu Date: Mon, 9 Sep 2024 12:12:10 +0800 Subject: [PATCH 3/3] add flush after write Issue: https://gitee.com/openharmony/commonlibrary_c_utils/issues/IAPVNB?from=project-issue Signed-off-by: chenkeyu --- base/src/file_ex.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/base/src/file_ex.cpp b/base/src/file_ex.cpp index 897cc41..7ad317c 100644 --- a/base/src/file_ex.cpp +++ b/base/src/file_ex.cpp @@ -345,6 +345,7 @@ bool SaveBufferToFile(const string& filePath, const vector& content, bool } file.write(&content[0], content.size()); + file.flush(); return true; } -- Gitee