Use RAII for addrinfo lifetime management#5399
Open
KaganCanSit wants to merge 1 commit intorandombit:masterfrom
Open
Use RAII for addrinfo lifetime management#5399KaganCanSit wants to merge 1 commit intorandombit:masterfrom
KaganCanSit wants to merge 1 commit intorandombit:masterfrom
Conversation
Contributor
Author
|
If this development is deemed appropriate, the following simplification can through a separate branch. diff --git a/src/cli/tls_client.cpp b/src/cli/tls_client.cpp
index 56cc4a76a..f18f106ef 100644
--- a/src/cli/tls_client.cpp
+++ b/src/cli/tls_client.cpp
@@ -397,11 +397,8 @@ class TLS_Client final : public Command {
throw CLI_Error("getaddrinfo failed for " + host);
}
- socket_type fd = 0;
- bool success = false;
-
for(const addrinfo* rp = res.get(); rp != nullptr; rp = rp->ai_next) {
- fd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+ socket_type fd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(fd == invalid_socket()) {
continue;
@@ -412,16 +409,10 @@ class TLS_Client final : public Command {
continue;
}
- success = true;
- break;
- }
-
- if(!success) {
- // no address succeeded
- throw CLI_Error("Connecting to host failed");
+ return fd;
}
-
- return fd;
+ // no address succeeded
+ throw CLI_Error("Connecting to host failed");
} |
63f65a0 to
49b3d32
Compare
49b3d32 to
11695c8
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hello,
It has been over a year since my PR #4660, and I honestly don't remember many of the details. While revisiting that branch, I identified changes that could independently contribute to the project and decided to submit them as separate PRs.
This PR applies RAII-based lifetime management to addrinfo resources returned by
getaddrinfo(). In the current codebase,::freeaddrinfo()is called manually, which can leak if an exception is thrown before the cleanup call is reached.The changes are based on the approach discussed with @reneme in this review comment (Since it is marked as resolved, you cannot directly move to the comment.): a
std::unique_ptrwith a stateless lambda deleter combined withBotan::out_ptr()fromstl_util.h.Quotes:
@KaganCanSit
@reneme
The using declarations are defined as private members in each class since there is currently no shared internal header across socket files. (
socket_utils.hexists but is external and not included, I think there is a reason for this.)I edited the
bogo_shim.cppfile to ensure full compatibility with the others and to allow direct intervention whenBotan::out_ptris removed. However, if I am mistaken, please let me know.Compile and Test
I am happy to make further adjustments based on your feedback.
Best regards.