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

Amend #3043 by allowing a longer hostname and simplifying the code. #3044

Merged
merged 1 commit into from Sep 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions psi4/src/psi4/libciomr/tstart.cc
Expand Up @@ -70,10 +70,10 @@ void PSI_API tstart() {
const long clk_tck = sysconf(_SC_CLK_TCK);
times(&total_tmstime);

// host name has up to HOST_NAME_MAX(==64) + 1 bytes, and must end in the null byte
std::vector<char> name(65);
error = gethostname(name.data(), 65);
if (error != 0) strncpy(name.data(), "nohostname", 11);
// host name has up to HOST_NAME_MAX (64 or 256) + 1 bytes, and must end in the null byte
std::vector<char> name(257);
error = gethostname(name.data(), name.size());
if (error) strncpy(name.data(), "nohostname", name.size());
Copy link
Member

@loriab loriab Sep 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about 11 -> name.size() here, but https://cplusplus.com/reference/cstring/strncpy/ convinced me that it's doing the padding right.

overall, lgtm. any concerns, @zachglick ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no concerns, I agree this is a better way to do things. thanks Susi!

if (name.back() != '\0') name.push_back('\0');

/// start a global timer
Expand Down Expand Up @@ -105,10 +105,10 @@ void PSI_API tstop() {
struct tms total_tmstime;
double user_s, sys_s;

// host name has up to HOST_NAME_MAX(==64) + 1 bytes, and must end in the null byte
std::vector<char> name(65);
error = gethostname(name.data(), 65);
if (error != 0) strncpy(name.data(), "nohostname", 11);
// host name has up to HOST_NAME_MAX (64 or 256) + 1 bytes, and must end in the null byte
std::vector<char> name(257);
error = gethostname(name.data(), name.size());
if (error) strncpy(name.data(), "nohostname", name.size());
if (name.back() != '\0') name.push_back('\0');

time_end = std::time(nullptr);
Expand Down
8 changes: 4 additions & 4 deletions psi4/src/psi4/libqt/timer.cc
Expand Up @@ -1006,10 +1006,10 @@ void timer_done() {
extern Timer_Structure root_timer;
root_timer.turn_off();

// host name has up to HOST_NAME_MAX(==64) + 1 bytes, and must end in the null byte
std::vector<char> host(65);
int error = gethostname(host.data(), 65);
if (error != 0) strncpy(host.data(), "nohostname", 11);
// host name has up to HOST_NAME_MAX (64 or 256) + 1 bytes, and must end in the null byte
std::vector<char> host(257);
int error = gethostname(host.data(), host.size());
if (error) strncpy(host.data(), "nohostname", host.size());
if (host.back() != '\0') host.push_back('\0');

/* Dump the timing data to timer.dat and free the timers */
Expand Down