-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add safety checks for accessing registry pointers
Add a new function pair `checked` and `checked_deref` to the `internal` namespace that check whether a nullable type (such as `std::shared_ptr`) contains a value before forwarding or using it. Both functions throw an exception with a customizable error message if the check fails. We use the new functions to make sure member variables are initialized using a non-null value or to make sure a registry pointer is valid before dereferencing it.
- Loading branch information
Showing
8 changed files
with
53 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <stdexcept> | ||
|
||
namespace broker::internal { | ||
|
||
// Helper function to check a nullable value and throw an exception if it is | ||
// `nullptr` / `nullopt`. This function is used to simplify error handling in | ||
// the constructor when initializing member functions. | ||
template <class Nullable> | ||
auto checked(Nullable what, const char* msg) { | ||
if (!what) | ||
throw std::logic_error(msg); | ||
return what; | ||
} | ||
|
||
template <class Nullable> | ||
auto& checked_deref(Nullable& what, const char* msg) { | ||
if (!what) | ||
throw std::logic_error(msg); | ||
return *what; | ||
} | ||
|
||
} // namespace broker::internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters