Skip to content

Commit e461cff

Browse files
committed
fix(windows): use wcsnlen for defensive programming (CWE-126)
Replace wcslen with wcsnlen using UNICODE_STRING_MAX_CHARS (32767) as the upper bound to prevent potential buffer over-read vulnerabilities. This change updates: - Template file for new Windows apps - 4 integration test runner files - 10 example/dev runner files
1 parent caf78da commit e461cff

15 files changed

Lines changed: 143 additions & 75 deletions

File tree

‎dev/a11y_assessments/windows/runner/utils.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
unsigned int target_length = ::WideCharToMultiByte(
52+
// First, find the length of the string with a safe upper bound (CWE-126).
53+
// UNICODE_STRING_MAX_CHARS (32767) is the maximum length of a UNICODE_STRING.
54+
int input_length = static_cast<int>(wcsnlen(utf16_string, UNICODE_STRING_MAX_CHARS));
55+
// Now use that bounded length to determine the required buffer size.
56+
// When an explicit length is passed, WideCharToMultiByte does not include
57+
// the null terminator in its returned size.
58+
int target_length = ::WideCharToMultiByte(
5359
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr)
55-
-1; // remove the trailing null character
56-
int input_length = (int)wcslen(utf16_string);
60+
input_length, nullptr, 0, nullptr, nullptr);
5761
std::string utf8_string;
58-
if (target_length == 0 || target_length > utf8_string.max_size()) {
62+
if (target_length == 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
5963
return utf8_string;
6064
}
6165
utf8_string.resize(target_length);

‎dev/benchmarks/complex_layout/windows/runner/utils.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
unsigned int target_length = ::WideCharToMultiByte(
52+
// First, find the length of the string with a safe upper bound (CWE-126).
53+
// UNICODE_STRING_MAX_CHARS (32767) is the maximum length of a UNICODE_STRING.
54+
int input_length = static_cast<int>(wcsnlen(utf16_string, UNICODE_STRING_MAX_CHARS));
55+
// Now use that bounded length to determine the required buffer size.
56+
// When an explicit length is passed, WideCharToMultiByte does not include
57+
// the null terminator in its returned size.
58+
int target_length = ::WideCharToMultiByte(
5359
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr)
55-
-1; // remove the trailing null character
60+
input_length, nullptr, 0, nullptr, nullptr);
5661
std::string utf8_string;
57-
if (target_length == 0 || target_length > utf8_string.max_size()) {
62+
if (target_length == 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
5863
return utf8_string;
5964
}
6065
utf8_string.resize(target_length);
6166
int converted_length = ::WideCharToMultiByte(
6267
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
63-
-1, utf8_string.data(), target_length, nullptr, nullptr);
68+
input_length, utf8_string.data(), target_length, nullptr, nullptr);
6469
if (converted_length == 0) {
6570
return std::string();
6671
}

‎dev/integration_tests/flutter_gallery/windows/runner/utils.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
unsigned int target_length = ::WideCharToMultiByte(
52+
// First, find the length of the string with a safe upper bound (CWE-126).
53+
// UNICODE_STRING_MAX_CHARS (32767) is the maximum length of a UNICODE_STRING.
54+
int input_length = static_cast<int>(wcsnlen(utf16_string, UNICODE_STRING_MAX_CHARS));
55+
// Now use that bounded length to determine the required buffer size.
56+
// When an explicit length is passed, WideCharToMultiByte does not include
57+
// the null terminator in its returned size.
58+
int target_length = ::WideCharToMultiByte(
5359
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr)
55-
-1; // remove the trailing null character
56-
int input_length = (int)wcslen(utf16_string);
60+
input_length, nullptr, 0, nullptr, nullptr);
5761
std::string utf8_string;
58-
if (target_length == 0 || target_length > utf8_string.max_size()) {
62+
if (target_length == 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
5963
return utf8_string;
6064
}
6165
utf8_string.resize(target_length);

‎dev/integration_tests/ui/windows/runner/utils.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
unsigned int target_length = ::WideCharToMultiByte(
52+
// First, find the length of the string with a safe upper bound (CWE-126).
53+
// UNICODE_STRING_MAX_CHARS (32767) is the maximum length of a UNICODE_STRING.
54+
int input_length = static_cast<int>(wcsnlen(utf16_string, UNICODE_STRING_MAX_CHARS));
55+
// Now use that bounded length to determine the required buffer size.
56+
// When an explicit length is passed, WideCharToMultiByte does not include
57+
// the null terminator in its returned size.
58+
int target_length = ::WideCharToMultiByte(
5359
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr)
55-
-1; // remove the trailing null character
56-
int input_length = (int)wcslen(utf16_string);
60+
input_length, nullptr, 0, nullptr, nullptr);
5761
std::string utf8_string;
58-
if (target_length == 0 || target_length > utf8_string.max_size()) {
62+
if (target_length == 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
5963
return utf8_string;
6064
}
6165
utf8_string.resize(target_length);

‎dev/integration_tests/windowing_test/windows/runner/utils.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
unsigned int target_length = ::WideCharToMultiByte(
52+
// First, find the length of the string with a safe upper bound (CWE-126).
53+
// UNICODE_STRING_MAX_CHARS (32767) is the maximum length of a UNICODE_STRING.
54+
int input_length = static_cast<int>(wcsnlen(utf16_string, UNICODE_STRING_MAX_CHARS));
55+
// Now use that bounded length to determine the required buffer size.
56+
// When an explicit length is passed, WideCharToMultiByte does not include
57+
// the null terminator in its returned size.
58+
int target_length = ::WideCharToMultiByte(
5359
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr)
55-
-1; // remove the trailing null character
56-
int input_length = (int)wcslen(utf16_string);
60+
input_length, nullptr, 0, nullptr, nullptr);
5761
std::string utf8_string;
58-
if (target_length == 0 || target_length > utf8_string.max_size()) {
62+
if (target_length == 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
5963
return utf8_string;
6064
}
6165
utf8_string.resize(target_length);

‎dev/integration_tests/windows_startup_test/windows/runner/utils.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
unsigned int target_length = ::WideCharToMultiByte(
52+
// First, find the length of the string with a safe upper bound (CWE-126).
53+
// UNICODE_STRING_MAX_CHARS (32767) is the maximum length of a UNICODE_STRING.
54+
int input_length = static_cast<int>(wcsnlen(utf16_string, UNICODE_STRING_MAX_CHARS));
55+
// Now use that bounded length to determine the required buffer size.
56+
// When an explicit length is passed, WideCharToMultiByte does not include
57+
// the null terminator in its returned size.
58+
int target_length = ::WideCharToMultiByte(
5359
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr)
55-
-1; // remove the trailing null character
56-
int input_length = (int)wcslen(utf16_string);
60+
input_length, nullptr, 0, nullptr, nullptr);
5761
std::string utf8_string;
58-
if (target_length == 0 || target_length > utf8_string.max_size()) {
62+
if (target_length == 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
5963
return utf8_string;
6064
}
6165
utf8_string.resize(target_length);

‎dev/manual_tests/windows/runner/utils.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
unsigned int target_length = ::WideCharToMultiByte(
52+
// First, find the length of the string with a safe upper bound (CWE-126).
53+
// UNICODE_STRING_MAX_CHARS (32767) is the maximum length of a UNICODE_STRING.
54+
int input_length = static_cast<int>(wcsnlen(utf16_string, UNICODE_STRING_MAX_CHARS));
55+
// Now use that bounded length to determine the required buffer size.
56+
// When an explicit length is passed, WideCharToMultiByte does not include
57+
// the null terminator in its returned size.
58+
int target_length = ::WideCharToMultiByte(
5359
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr)
55-
-1; // remove the trailing null character
60+
input_length, nullptr, 0, nullptr, nullptr);
5661
std::string utf8_string;
57-
if (target_length == 0 || target_length > utf8_string.max_size()) {
62+
if (target_length == 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
5863
return utf8_string;
5964
}
6065
utf8_string.resize(target_length);
6166
int converted_length = ::WideCharToMultiByte(
6267
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
63-
-1, utf8_string.data(), target_length, nullptr, nullptr);
68+
input_length, utf8_string.data(), target_length, nullptr, nullptr);
6469
if (converted_length == 0) {
6570
return std::string();
6671
}

‎examples/api/windows/runner/utils.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
unsigned int target_length = ::WideCharToMultiByte(
52+
// First, find the length of the string with a safe upper bound (CWE-126).
53+
// UNICODE_STRING_MAX_CHARS (32767) is the maximum length of a UNICODE_STRING.
54+
int input_length = static_cast<int>(wcsnlen(utf16_string, UNICODE_STRING_MAX_CHARS));
55+
// Now use that bounded length to determine the required buffer size.
56+
// When an explicit length is passed, WideCharToMultiByte does not include
57+
// the null terminator in its returned size.
58+
int target_length = ::WideCharToMultiByte(
5359
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr)
55-
-1; // remove the trailing null character
60+
input_length, nullptr, 0, nullptr, nullptr);
5661
std::string utf8_string;
57-
if (target_length == 0 || target_length > utf8_string.max_size()) {
62+
if (target_length == 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
5863
return utf8_string;
5964
}
6065
utf8_string.resize(target_length);
6166
int converted_length = ::WideCharToMultiByte(
6267
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
63-
-1, utf8_string.data(), target_length, nullptr, nullptr);
68+
input_length, utf8_string.data(), target_length, nullptr, nullptr);
6469
if (converted_length == 0) {
6570
return std::string();
6671
}

‎examples/flutter_view/windows/runner/utils.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
unsigned int target_length = ::WideCharToMultiByte(
52+
// First, find the length of the string with a safe upper bound (CWE-126).
53+
// UNICODE_STRING_MAX_CHARS (32767) is the maximum length of a UNICODE_STRING.
54+
int input_length = static_cast<int>(wcsnlen(utf16_string, UNICODE_STRING_MAX_CHARS));
55+
// Now use that bounded length to determine the required buffer size.
56+
// When an explicit length is passed, WideCharToMultiByte does not include
57+
// the null terminator in its returned size.
58+
int target_length = ::WideCharToMultiByte(
5359
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr)
55-
-1; // remove the trailing null character
60+
input_length, nullptr, 0, nullptr, nullptr);
5661
std::string utf8_string;
57-
if (target_length == 0 || target_length > utf8_string.max_size()) {
62+
if (target_length == 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
5863
return utf8_string;
5964
}
6065
utf8_string.resize(target_length);
6166
int converted_length = ::WideCharToMultiByte(
6267
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
63-
-1, utf8_string.data(), target_length, nullptr, nullptr);
68+
input_length, utf8_string.data(), target_length, nullptr, nullptr);
6469
if (converted_length == 0) {
6570
return std::string();
6671
}

‎examples/hello_world/windows/runner/utils.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
if (utf16_string == nullptr) {
5050
return std::string();
5151
}
52-
unsigned int target_length = ::WideCharToMultiByte(
52+
// First, find the length of the string with a safe upper bound (CWE-126).
53+
// UNICODE_STRING_MAX_CHARS (32767) is the maximum length of a UNICODE_STRING.
54+
int input_length = static_cast<int>(wcsnlen(utf16_string, UNICODE_STRING_MAX_CHARS));
55+
// Now use that bounded length to determine the required buffer size.
56+
// When an explicit length is passed, WideCharToMultiByte does not include
57+
// the null terminator in its returned size.
58+
int target_length = ::WideCharToMultiByte(
5359
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
54-
-1, nullptr, 0, nullptr, nullptr)
55-
-1; // remove the trailing null character
60+
input_length, nullptr, 0, nullptr, nullptr);
5661
std::string utf8_string;
57-
if (target_length == 0 || target_length > utf8_string.max_size()) {
62+
if (target_length == 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
5863
return utf8_string;
5964
}
6065
utf8_string.resize(target_length);
6166
int converted_length = ::WideCharToMultiByte(
6267
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
63-
-1, utf8_string.data(), target_length, nullptr, nullptr);
68+
input_length, utf8_string.data(), target_length, nullptr, nullptr);
6469
if (converted_length == 0) {
6570
return std::string();
6671
}

0 commit comments

Comments
 (0)