Skip to content

Commit 1c07e1b

Browse files
authored
Avoid exception in observable IndexOf implementation (#1017)
1 parent 032effe commit 1c07e1b

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

strings/base_collections_vector.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,26 @@ namespace winrt::impl
115115

116116
bool IndexOf(Windows::Foundation::IInspectable const& value, uint32_t& index) const
117117
{
118-
try
118+
if constexpr (is_com_interface_v<T>)
119119
{
120-
return IndexOf(unbox_value<T>(value), index);
120+
if (!value)
121+
{
122+
return base_type::IndexOf(nullptr, index);
123+
}
124+
else if (auto as = value.try_as<T>())
125+
{
126+
return base_type::IndexOf(as, index);
127+
}
121128
}
122-
catch (hresult_no_interface const&)
129+
else
123130
{
124-
index = 0;
125-
return false;
131+
if (auto as = value.try_as<T>())
132+
{
133+
return base_type::IndexOf(as.value(), index);
134+
}
126135
}
136+
137+
return false;
127138
}
128139

129140
using base_type::GetMany;

test/test/observable_index_of.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "pch.h"
2+
3+
using namespace winrt;
4+
using namespace Windows::Foundation;
5+
using namespace Windows::Foundation::Collections;
6+
7+
TEST_CASE("observable_index_of")
8+
{
9+
{
10+
auto v = single_threaded_observable_vector<int>().as<IVector<IInspectable>>();
11+
v.Append(box_value(123));
12+
uint32_t index = 0;
13+
REQUIRE(v.IndexOf(box_value(123), index));
14+
REQUIRE(!v.IndexOf(nullptr, index));
15+
REQUIRE(!v.IndexOf(box_value(456), index));
16+
REQUIRE(!v.IndexOf(Uri(L"http://kennykerr.ca"), index));
17+
}
18+
{
19+
auto value = Uri(L"http://kennykerr.ca");
20+
21+
auto v = single_threaded_observable_vector<IStringable>().as<IVector<IInspectable>>();
22+
v.Append(value);
23+
uint32_t index = 0;
24+
REQUIRE(v.IndexOf(value, index));
25+
REQUIRE(!v.IndexOf(nullptr, index));
26+
REQUIRE(!v.IndexOf(box_value(456), index));
27+
REQUIRE(!v.IndexOf(Uri(L"http://kennykerr.ca"), index));
28+
}
29+
{
30+
auto v = single_threaded_observable_vector<IStringable>().as<IVector<IInspectable>>();
31+
v.Append(nullptr);
32+
uint32_t index = 0;
33+
REQUIRE(v.IndexOf(nullptr, index));
34+
REQUIRE(!v.IndexOf(box_value(456), index));
35+
REQUIRE(!v.IndexOf(Uri(L"http://kennykerr.ca"), index));
36+
}
37+
}

test/test/test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@
417417
<ClCompile Include="notify_awaiter.cpp" />
418418
<ClCompile Include="no_make_detection.cpp" />
419419
<ClCompile Include="numerics.cpp" />
420+
<ClCompile Include="observable_index_of.cpp" />
420421
<ClCompile Include="out_params.cpp" />
421422
<ClCompile Include="out_params_abi.cpp" />
422423
<ClCompile Include="out_params_bad.cpp" />

0 commit comments

Comments
 (0)