Skip to content

Commit

Permalink
ensure robustness against malformed runtime class name (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scottj1s committed Oct 28, 2020
1 parent 892817e commit fab6067
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
43 changes: 28 additions & 15 deletions TestComponentCSharp/Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,16 +1274,16 @@ namespace winrt::TestComponentCSharp::implementation
throw hresult_not_implemented();
}

struct native_properties1 : winrt::implements<native_properties1, TestComponentCSharp::IProperties1>
TestComponentCSharp::IProperties1 Class::NativeProperties1()
{
int32_t ReadWriteProperty()
struct native_properties1 : winrt::implements<native_properties1, TestComponentCSharp::IProperties1>
{
return 42;
}
};
int32_t ReadWriteProperty()
{
return 42;
}
};

TestComponentCSharp::IProperties1 Class::NativeProperties1()
{
return winrt::make<native_properties1>();
}

Expand All @@ -1293,17 +1293,17 @@ namespace winrt::TestComponentCSharp::implementation
virtual HRESULT __stdcall GetService(int32_t* type, int32_t* service) noexcept = 0;
};

struct service_provider : winrt::implements<service_provider, WF::IInspectable, IServiceProviderInterop>
WF::IInspectable Class::ServiceProvider()
{
HRESULT __stdcall GetService(int32_t* type, int32_t* service) noexcept override
struct service_provider : winrt::implements<service_provider, WF::IInspectable, IServiceProviderInterop>
{
*service = 42;
return 0;
}
};
HRESULT __stdcall GetService(int32_t* type, int32_t* service) noexcept override
{
*service = 42;
return 0;
}
};

WF::IInspectable Class::ServiceProvider()
{
return winrt::make<service_provider>();
}

Expand All @@ -1330,5 +1330,18 @@ namespace winrt::TestComponentCSharp::implementation
DataErrorsChangedEventArgs args(detach_abi(mock), take_ownership_from_abi_t());
_dataErrorsChanged(*this, args);
}

WF::IInspectable Class::BadRuntimeClassName()
{
struct bad_runtime_classname : winrt::implements<bad_runtime_classname, WF::IInspectable>
{
hstring GetRuntimeClassName()
{
return L"BadRuntimeClassName<T>";
}
};

return winrt::make<bad_runtime_classname>();
}
}

2 changes: 2 additions & 0 deletions TestComponentCSharp/Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ namespace winrt::TestComponentCSharp::implementation
void ErrorsChanged(winrt::event_token const& token) noexcept;
Windows::Foundation::Collections::IIterable<Windows::Foundation::IInspectable> GetErrors(hstring const& propertyName);
void RaiseDataErrorChanged();

static Windows::Foundation::IInspectable BadRuntimeClassName();
};
}

Expand Down
2 changes: 2 additions & 0 deletions TestComponentCSharp/TestComponentCSharp.idl
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ namespace TestComponentCSharp

// INotifyDataErrorInfo
void RaiseDataErrorChanged();

static Object BadRuntimeClassName{ get; };
}

[threading(sta), marshaling_behavior(standard)]
Expand Down
5 changes: 4 additions & 1 deletion UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,11 @@ public void TestCustomProjections()
IntPtr service;
serviceProvider.GetService(IntPtr.Zero, out service);
Assert.Equal(new IntPtr(42), service);
}

// Ensure robustness with bad runtime class names (parsing errors, type not found, etc)
var badRuntimeClassName = Class.BadRuntimeClassName;
Assert.NotNull(badRuntimeClassName);
}

[Fact]
public void TestKeyValuePair()
Expand Down

0 comments on commit fab6067

Please sign in to comment.