Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/kf/FltCommunicationPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ namespace kf
PMDL inputMdl = nullptr;
PMDL outputMdl = nullptr;

NTSTATUS status = [&]()
NTSTATUS status = [&]() -> NTSTATUS
{
//
// Lock user buffers so __try/__except is required only here and not in the handler->onMessage
Expand Down
17 changes: 9 additions & 8 deletions include/kf/ScopeFailure.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace kf
{
template<typename F>
template<typename F, typename T> requires std::convertible_to<T, NTSTATUS>
class ScopeFailure
{
public:
ScopeFailure(NTSTATUS& status, F&& f) : m_status(status), m_f(f)
ScopeFailure(T& status, F&& f) : m_status(status), m_f(f)
{
}

Expand All @@ -30,22 +30,23 @@ namespace kf
ScopeFailure& operator=(const ScopeFailure&);

private:
NTSTATUS& m_status;
F m_f;
T& m_status;
F m_f;
};

template<typename T> requires std::convertible_to<T, NTSTATUS>
struct MakeScopeFailure
{
MakeScopeFailure(NTSTATUS& status) : m_status(status)
MakeScopeFailure(T& status) : m_status(status)
{
}

template<typename F>
ScopeFailure<F> operator+=(F&& f)
ScopeFailure<F, T> operator+=(F&& f)
{
return ScopeFailure<F>(m_status, std::move(f));
return ScopeFailure<F, T>(m_status, std::move(f));
}

NTSTATUS& m_status;
T& m_status;
};
}
52 changes: 51 additions & 1 deletion test/ScopeFailureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ SCENARIO("SCOPE_FAILURE macro")
SCOPE_FAILURE(status)
{
value++;
scopedStatus = status;;
scopedStatus = status;
};

status = STATUS_CANT_WAIT;
Expand All @@ -97,4 +97,54 @@ SCENARIO("SCOPE_FAILURE macro")
REQUIRE(scopedStatus == STATUS_CANT_WAIT);
}
}

GIVEN("SCOPE_FAILURE macro with a type convertible to NTSTATUS (unsuccessful case)")
{
struct StatusWrapper
{
NTSTATUS status;
operator NTSTATUS() const { return status; }
};

StatusWrapper status{ STATUS_SUCCESS };
int value = 0;

{
status.status = STATUS_ACCESS_DENIED;

SCOPE_FAILURE(status)
{
value++;
};
}

THEN("Scoped function should be called for unsuccessful value")
{
REQUIRE(value == 1);
}
}

GIVEN("SCOPE_FAILURE macro with a type convertible to NTSTATUS (successful case)")
{
struct StatusWrapper
{
NTSTATUS status;
operator NTSTATUS() const { return status; }
};

StatusWrapper status{ STATUS_SUCCESS };
int value = 0;

{
SCOPE_FAILURE(status)
{
value++;
};
}

THEN("Scoped function should not be called when value is successful")
{
REQUIRE(value == 0);
}
}
}