Skip to content

Commit e9d336d

Browse files
committed
fix(windows): use wcsnlen for defensive programming (CWE-126)
Replace wcslen with wcsnlen in Utf8FromUtf16 function to provide an extra safety bound against potential buffer over-reads. While the current usage is safe (input from Windows APIs is guaranteed null-terminated), this change follows security best practices and resolves warnings from security scanners like Semgrep/GitLab SAST. Fixes flutter#180418
1 parent d81cd3e commit e9d336d

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

  • packages/flutter_tools/templates/app/windows.tmpl/runner

‎packages/flutter_tools/templates/app/windows.tmpl/runner/utils.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4949
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
5050
-1, nullptr, 0, nullptr, nullptr)
5151
-1; // remove the trailing null character
52-
int input_length = (int)wcslen(utf16_string);
52+
// Use wcsnlen for defensive programming (CWE-126). Input is guaranteed
53+
// null-terminated by Windows API, but this provides an extra safety bound.
54+
int input_length = (int)wcsnlen(utf16_string, 32767);
5355
std::string utf8_string;
5456
if (target_length == 0 || target_length > utf8_string.max_size()) {
5557
return utf8_string;

0 commit comments

Comments
 (0)