microsoft / microsoft/wil

static_cast<UINT>() silently truncates >4GB to UINT32 _MAX

Open
#651 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
3k
Forks
300
Avg merge
19h 12m
Merged PRs (30d)
1

Description

Search for static_cast<UINT and you get several hits, most of which silently truncate size_t to UINT or UINT32. For example

template <>
inline unique_hstring make_unique_string_nothrow<unique_hstring>(
    _When_((source != nullptr) && length != static_cast<size_t>(-1), _In_reads_(length))
        _When_((source != nullptr) && length == static_cast<size_t>(-1), _In_z_) PCWSTR source,
    size_t length) WI_NOEXCEPT
{
    WI_ASSERT(source != nullptr); // the HSTRING version of this function does not support this case
    if (length == static_cast<size_t>(-1))
    {
        length = wcslen(source);
    }

    unique_hstring result;
    ::WindowsCreateString(source, static_cast<UINT32>(length), &result);
    return result;
}

Call will a string >4GB and this succeeds but only copies the first 4GB. This should fail e.g.

...
    unique_hstring result;
    if (length < UINT32_MAX)
    {
        ::WindowsCreateString(source, static_cast<UINT32>(length), &result);
    }
    return result;

I currently see hits in winrt.h, registry_helpers.h, resource.h, and TraceLogging.h. Best to review all instances and very false positives or fix appropriately

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Search for static_cast<UINT and review the instances in winrt.h, registry_helpers.h, resource.h, and TraceLogging.h, checking each size_t-to- UINT or UINT32 conversion. Start with the make_unique_string_nothrow example and identify which cases can exceed the target type. Done means every relevant instance either rejects oversized input or is confirmed safe, with false positives handled appropriately.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.