Skip to content

Windows template utils.cpp: Use wcsnlen instead of wcslen for defensive programming (CWE-126) #180418

Closed

Description

@dbebawy

Description

The Windows runner template file packages/flutter_tools/templates/app/windows.tmpl/runner/utils.cpp uses wcslen at line 52, which doesn't handle non-null-terminated strings. While the current usage is safe (input comes from Windows APIs that guarantee null-termination), using wcsnlen would be better defensive programming and resolves security scanner warnings (CWE-126: Buffer Over-read).

Current Code (line 52)

int input_length = (int)wcslen(utf16_string);

Proposed Fix

int input_length = (int)wcsnlen(utf16_string, 32767);

Why This Matters

  1. Security Scanners: Tools like Semgrep/GitLab SAST flag this as HIGH severity (CWE-126)
  2. Defensive Programming: Even though current usage is safe, wcsnlen provides an extra safety bound
  3. No Performance Impact: wcsnlen has negligible overhead for typical string lengths
  4. Consistency: Aligns with Microsoft's secure CRT recommendations

Related

Risk Assessment

LOW - This is a minor defensive improvement. The existing code is functionally correct because:

  • Input strings come from CommandLineToArgvW which guarantees null-termination
  • The null check on line 45 handles the nullptr case

However, the fix follows security best practices and silences legitimate scanner warnings.

Checklist

  • I have read the contributor guide
  • I have an issue that this PR addresses
  • All tests pass locally

Activity

  1. added 2 commits that reference this issue on Dec 31, 2025
    9bd81c3
    70f2167
  2. added
    in triagePresently being triaged by the triage team
    toolAffects the "flutter" command-line tool. See also t: labels.
    platform-windowsBuilding on or for Windows specifically
    c: proposalA detailed proposal for a change to Flutter
    team-windowsOwned by the Windows platform team
    and removed
    in triagePresently being triaged by the triage team
    on Jan 5, 2026
  3. added 4 commits that reference this issue on Jan 12, 2026
    2e88d72
    a19f278
    e9d336d
    9e68b08
  4. added
    P3Issues that are less important to the Flutter project
    triaged-windowsTriaged by the Windows platform team
    on Jan 14, 2026
  5. github-actions commented on Feb 2, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  6. locked as resolved and limited conversation to collaborators on Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: proposalA detailed proposal for a change to Flutterplatform-windowsBuilding on or for Windows specificallyr: fixedIssue is closed as already fixed in a newer versionteam-windowsOwned by the Windows platform teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-windowsTriaged by the Windows platform team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions