Skip to content

Commit 9a34225

Browse files
committed
Use std::find + iterator approach instead of potential error prone std::strlen (Codacy).
1 parent 2b3611b commit 9a34225

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

src/plssvm/detail/tracking/performance_tracker.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@
6868
#endif
6969
#endif
7070

71-
#include <algorithm> // std::max
71+
#include <algorithm> // std::max, std::find
7272
#include <chrono> // std::chrono::steady_clock::time_point
7373
#include <cstddef> // std::size_t
74-
#include <cstring> // std::strlen
7574
#include <fstream> // std::ofstream
7675
#include <iostream> // std::ios_base::app, std::ostream, std::clog, std::endl
7776
#include <map> // std::map
@@ -94,7 +93,9 @@ namespace {
9493
hostname = "not available";
9594
}
9695
// resize to actual string length (truncate trailing '\0's)
97-
hostname.resize(std::strlen(hostname.c_str()));
96+
if (const auto it = std::find(hostname.cbegin(), hostname.cend(), '\0'); it != hostname.cend()) {
97+
hostname.erase(it, hostname.cend());
98+
}
9899
if (hostname.empty()) {
99100
hostname = "not available";
100101
}
@@ -116,7 +117,9 @@ namespace {
116117
username = "not available";
117118
}
118119
// resize to actual string length (truncate trailing '\0's)
119-
username.resize(std::strlen(username.c_str()));
120+
if (const auto it = std::find(username.cbegin(), username.cend(), '\0'); it != username.cend()) {
121+
username.erase(it, username.cend());
122+
}
120123
if (username.empty()) {
121124
username = "not available";
122125
}

0 commit comments

Comments
 (0)