summaryrefslogtreecommitdiff
path: root/methods
diff options
context:
space:
mode:
authorFaidon Liambotis <paravoid@debian.org>2020-12-23 01:51:50 +0200
committerFaidon Liambotis <paravoid@debian.org>2020-12-23 01:51:50 +0200
commit8d4b3a4fcead0ca534b5d1c5a99ae2a4c95eee21 (patch)
tree005ce0e916b56a920b76021a3cf9dfb0016f1ecc /methods
parent570861fc55ba38c1092fac1d555111bab4577b49 (diff)
connect: convert a C-style string to std::string
Convert the fixed-size (300) char array "ServStr" to a std::string, and simplify the code by removing snprintfs in the process. While at it, rename to the more aptly named "ServiceNameOrPort" and update the comment to reflect what this variable is meant to be.
Diffstat (limited to 'methods')
-rw-r--r--methods/connect.cc19
1 files changed, 8 insertions, 11 deletions
diff --git a/methods/connect.cc b/methods/connect.cc
index 57dfb6299..bb7fba85d 100644
--- a/methods/connect.cc
+++ b/methods/connect.cc
@@ -349,12 +349,9 @@ static ResultState ConnectToHostname(std::string const &Host, int const Port,
{
if (ConnectionAllowed(Service, Host) == false)
return ResultState::FATAL_ERROR;
- // Convert the port name/number
- char ServStr[300];
- if (Port != 0)
- snprintf(ServStr,sizeof(ServStr),"%i", Port);
- else
- snprintf(ServStr,sizeof(ServStr),"%s", Service);
+
+ // Used by getaddrinfo(); prefer port if given, else fallback to service
+ std::string ServiceNameOrPort = Port != 0 ? std::to_string(Port) : Service;
/* We used a cached address record.. Yes this is against the spec but
the way we have setup our rotating dns suggests that this is more
@@ -405,14 +402,14 @@ static ResultState ConnectToHostname(std::string const &Host, int const Port,
while (1)
{
int Res;
- if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 ||
+ if ((Res = getaddrinfo(Host.c_str(), ServiceNameOrPort.c_str(), &Hints, &LastHostAddr)) != 0 ||
LastHostAddr == 0)
{
if (Res == EAI_NONAME || Res == EAI_SERVICE)
{
if (DefPort != 0)
{
- snprintf(ServStr, sizeof(ServStr), "%i", DefPort);
+ ServiceNameOrPort = std::to_string(DefPort);
DefPort = 0;
continue;
}
@@ -431,10 +428,10 @@ static ResultState ConnectToHostname(std::string const &Host, int const Port,
}
if (Res == EAI_SYSTEM)
_error->Errno("getaddrinfo", _("System error resolving '%s:%s'"),
- Host.c_str(), ServStr);
+ Host.c_str(), ServiceNameOrPort.c_str());
else
_error->Error(_("Something wicked happened resolving '%s:%s' (%i - %s)"),
- Host.c_str(), ServStr, Res, gai_strerror(Res));
+ Host.c_str(), ServiceNameOrPort.c_str(), Res, gai_strerror(Res));
return ResultState::TRANSIENT_ERROR;
}
break;
@@ -469,7 +466,7 @@ static ResultState ConnectToHostname(std::string const &Host, int const Port,
return Result;
if (_error->PendingError() == true)
return ResultState::FATAL_ERROR;
- _error->Error(_("Unable to connect to %s:%s:"), Host.c_str(), ServStr);
+ _error->Error(_("Unable to connect to %s:%s:"), Host.c_str(), ServiceNameOrPort.c_str());
return ResultState::TRANSIENT_ERROR;
}
/*}}}*/