microsoft / microsoft/Windows-classic-samples

AMGetWideString is bugged

Open Beginner friendly
#234 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
5.7k
Forks
3.3k
PR merge metrics
No merged PRs in 30d

Description

AMGetWideString in Samples/Win7Samples/multimedia/directshow/baseclasses/wxutil.cpp is bugged:

// Return a wide string - allocating memory for it
// Returns:
//    S_OK          - no error
//    E_POINTER     - ppszReturn == NULL
//    E_OUTOFMEMORY - can't allocate memory for returned string
STDAPI AMGetWideString(LPCWSTR psz, __deref_out LPWSTR *ppszReturn)
{
    CheckPointer(ppszReturn, E_POINTER);
    ValidateReadWritePtr(ppszReturn, sizeof(LPWSTR));
    *ppszReturn = NULL;
    ASSERT(psz);
    const size_t nameLen = wcslen(psz);
    *ppszReturn = (LPWSTR)CoTaskMemAlloc(nameLen + sizeof(WCHAR));
    if (*ppszReturn == NULL) {
       return E_OUTOFMEMORY;
    }
    CopyMemory(*ppszReturn, psz, nameLen + sizeof(WCHAR));
    return NOERROR;
}

There's two lines that are problematic:

*ppszReturn = (LPWSTR)CoTaskMemAlloc(nameLen + sizeof(WCHAR));
...
CopyMemory(*ppszReturn, psz, nameLen + sizeof(WCHAR));

These should be * sizeof(WCHAR), not + sizeof(WCHAR). As a result this function won't copy the full string over, rather it will copy a small section of it and won't null-terminate it correctly.

This function is used in CBasePropertyPage::GetPageInfo in DirectShow, causing any property pages by custom DirectShow filters to have their titles render incorrectly.

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

Open Samples/Win7Samples/multimedia/directshow/baseclasses/wxutil.cpp and inspect AMGetWideString, then review its use from CBasePropertyPage::GetPageInfo. Verify that allocation and copying account for the full wide-character string and its terminator, and confirm that custom DirectShow property-page titles render correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
desktop
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.