From 8b19f42cedfaaa8e0338b2e8d5b1032b093aa409 Mon Sep 17 00:00:00 2001 From: Niels Bugel <bugel.niels@gmail.com> Date: Fri, 22 Nov 2024 10:04:10 +0100 Subject: [PATCH 1/2] Replaced usage of strlen --- catalogue/CmdLineTool.cpp | 16 ++++++---------- common/CmdLineTool.cpp | 11 ++++------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/catalogue/CmdLineTool.cpp b/catalogue/CmdLineTool.cpp index d0847f2eba..8af8b91330 100644 --- a/catalogue/CmdLineTool.cpp +++ b/catalogue/CmdLineTool.cpp @@ -44,28 +44,24 @@ CmdLineTool::~CmdLineTool() = default; // getUsername //------------------------------------------------------------------------------ std::string CmdLineTool::getUsername() { - std::string buf(256, '\0'); + char buf[256]; - if(getlogin_r(&buf[0], buf.size())) { + if(getlogin_r(buf, sizeof(buf)) != 0) { return "UNKNOWN"; - } else { - buf.resize(strlen(buf.c_str())); // Resize the string to remove extra null characters - return buf; } + return std::string(buf); } //------------------------------------------------------------------------------ // getHostname //------------------------------------------------------------------------------ std::string CmdLineTool::getHostname() { - std::string buf(256, '\0'); + char buf[256]; - if(gethostname(&buf[0], buf.size())) { + if(gethostname(buf, sizeof(buf)) != 0) { return "UNKNOWN"; - } else { - buf.resize(strlen(buf.c_str())); // Resize the string to remove extra null characters - return buf; } + return std::string(buf); } //------------------------------------------------------------------------------ diff --git a/common/CmdLineTool.cpp b/common/CmdLineTool.cpp index 1565b03c3f..6b5bf361ba 100644 --- a/common/CmdLineTool.cpp +++ b/common/CmdLineTool.cpp @@ -46,11 +46,10 @@ CmdLineTool::~CmdLineTool() = default; std::string CmdLineTool::getUsername() { char buf[256]; - if(getlogin_r(buf, sizeof(buf))) { + if(getlogin_r(buf, sizeof(buf)) != 0) { return "UNKNOWN"; - } else { - return buf; } + return std::string(buf); } //------------------------------------------------------------------------------ @@ -59,12 +58,10 @@ std::string CmdLineTool::getUsername() { std::string CmdLineTool::getHostname() { char buf[256]; - if(gethostname(buf, sizeof(buf))) { + if(gethostname(buf, sizeof(buf)) != 0) { return "UNKNOWN"; - } else { - buf[sizeof(buf) - 1] = '\0'; - return buf; } + return std::string(buf); } //------------------------------------------------------------------------------ -- GitLab From b775ffaf451fadbcf4469a7faa59822d10772259 Mon Sep 17 00:00:00 2001 From: nbugel <niels.alexander.bugel@cern.ch> Date: Fri, 22 Nov 2024 10:09:09 +0100 Subject: [PATCH 2/2] Applied clang format to changed lines --- catalogue/CmdLineTool.cpp | 4 ++-- common/CmdLineTool.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/catalogue/CmdLineTool.cpp b/catalogue/CmdLineTool.cpp index 8af8b91330..e70d90ae2b 100644 --- a/catalogue/CmdLineTool.cpp +++ b/catalogue/CmdLineTool.cpp @@ -46,7 +46,7 @@ CmdLineTool::~CmdLineTool() = default; std::string CmdLineTool::getUsername() { char buf[256]; - if(getlogin_r(buf, sizeof(buf)) != 0) { + if (getlogin_r(buf, sizeof(buf)) != 0) { return "UNKNOWN"; } return std::string(buf); @@ -58,7 +58,7 @@ std::string CmdLineTool::getUsername() { std::string CmdLineTool::getHostname() { char buf[256]; - if(gethostname(buf, sizeof(buf)) != 0) { + if (gethostname(buf, sizeof(buf)) != 0) { return "UNKNOWN"; } return std::string(buf); diff --git a/common/CmdLineTool.cpp b/common/CmdLineTool.cpp index 6b5bf361ba..72bf67e32c 100644 --- a/common/CmdLineTool.cpp +++ b/common/CmdLineTool.cpp @@ -46,7 +46,7 @@ CmdLineTool::~CmdLineTool() = default; std::string CmdLineTool::getUsername() { char buf[256]; - if(getlogin_r(buf, sizeof(buf)) != 0) { + if (getlogin_r(buf, sizeof(buf)) != 0) { return "UNKNOWN"; } return std::string(buf); @@ -58,7 +58,7 @@ std::string CmdLineTool::getUsername() { std::string CmdLineTool::getHostname() { char buf[256]; - if(gethostname(buf, sizeof(buf)) != 0) { + if (gethostname(buf, sizeof(buf)) != 0) { return "UNKNOWN"; } return std::string(buf); -- GitLab