Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading past end of array. #1468

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dlech
Copy link
Contributor

@dlech dlech commented Jan 12, 2025

Fix a bug in the NonDelegatingGetIids() implementation that could cause reading past the end of the returned array.

std::copy() returns a pointer to the next element in the array after the last element copied. The output argument *array was being assinged to this pointer after the first copy, causing it to no longer point to the beginning of the array. If the caller tries to access the full array after this, it will read past the end of the array and will miss the first elements of the array.

To fix, introduce a new temporary _array variable to pass the result of the first copy as the starting point of the second copy. Also add a test that failed before the fix and passes after the fix.

Fix a bug in the `NonDelegatingGetIids()` implementation that could
cause reading past the end of the returned array.

`std::copy()` returns a pointer to the next element in the array after
the last element copied. The output argument `*array` was being assinged
to this pointer after the first copy, causing it to no longer point to
the beginning of the array. If the caller tries to access the full array
after this, it will read past the end of the array and will miss the
first elements of the array.

To fix, introduce a new temporary `_array` variable to pass the result
of the first copy as the starting point of the second copy. Also add a
test that failed before the fix and passes after the fix.
@dlech
Copy link
Contributor Author

dlech commented Jan 12, 2025

If we aren't going to merge #1467, this fixes a bug that was noted there.

@@ -1033,8 +1033,8 @@ namespace winrt::impl
{
return error_bad_alloc;
}
*array = std::copy(local_iids.second, local_iids.second + local_count, *array);
std::copy(inner_iids.cbegin(), inner_iids.cend(), *array);
auto _array = std::copy(local_iids.second, local_iids.second + local_count, *array);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] I think the name _array with the leading underscore is misaligned with the naming pattern in this code base. Something like array_copy or local_array would probably fit better.

auto iids = winrt::get_interfaces(obj);
// BaseOverrides IID gets repeated twice. There are only 4 unique interfaces.
REQUIRE(iids.size() >= 4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe its just me, but >=4 seems overly conservative. If there is a well-known repeated IID then can this assert the size is 5 instead? Perhaps that's not ideal but it is at least testing the current behavior so that if it changes later the test will identify the behavior change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants