Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix hostname2ip fails when aux_buf is not long enough #1818

Merged
merged 6 commits into from
Jul 1, 2022
Merged
Changes from 3 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
23 changes: 20 additions & 3 deletions src/butil/endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "butil/memory/singleton_on_pthread_once.h"
#include "butil/strings/string_piece.h"
#include <sys/socket.h> // SO_REUSEADDR SO_REUSEPORT
serverglen marked this conversation as resolved.
Show resolved Hide resolved
#include <memory>

//supported since Linux 3.9.
DEFINE_bool(reuse_port, false, "Enable SO_REUSEPORT for all listened sockets");
Expand Down Expand Up @@ -193,12 +194,28 @@ int hostname2ip(const char* hostname, ip_t* ip) {
return -1;
}
#else
char aux_buf[1024];
int aux_buf_len = 1024;
std::unique_ptr<char[]> aux_buf(new char[aux_buf_len]);
int ret = 0;
int error = 0;
struct hostent ent;
struct hostent* result = NULL;
if (gethostbyname_r(hostname, &ent, aux_buf, sizeof(aux_buf),
&result, &error) != 0 || result == NULL) {
do {
result = NULL;
error = 0;
ret = gethostbyname_r(hostname,
&ent,
aux_buf.get(),
aux_buf_len,
&result,
&error);
if (ret != ERANGE) { // aux_buf is not long enough
break;
}
aux_buf_len *= 2;
aux_buf.reset(new char[aux_buf_len]);
} while (1);
if (ret != 0 || result == NULL) {
return -1;
}
#endif // defined(OS_MACOSX)
Expand Down