Module: check_mk
Branch: master
Commit: e15cc717e4f2776a4ee944864bd8c4c2cef69a79
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e15cc717e4f277…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Fri Apr 6 15:41:29 2018 +0200
Win-agent: improve string handling
Replace (some) occurrences of snprintf and strncpy with std::string
operations. Favour std::string over C char pointers. Mark input string
parameters as const. Throw own character replace implementation where
it belongs to.
---
agents/windows/build_version | 2 +-
agents/windows/check_mk_agent.cc | 14 ++++++------
agents/windows/sections/SectionDF.cc | 43 ++++++++++++++++--------------------
agents/windows/sections/SectionDF.h | 4 ++--
4 files changed, 29 insertions(+), 34 deletions(-)
diff --git a/agents/windows/build_version b/agents/windows/build_version
index 31bd29a..b2ad126 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3230
+3245
diff --git a/agents/windows/check_mk_agent.cc b/agents/windows/check_mk_agent.cc
index d84453d..71f0506 100644
--- a/agents/windows/check_mk_agent.cc
+++ b/agents/windows/check_mk_agent.cc
@@ -365,16 +365,16 @@ void InstallService() {
s_winapi.OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
if (serviceControlManager) {
- char path[_MAX_PATH + 1];
+ char path[_MAX_PATH + 1] = {0};
if (s_winapi.GetModuleFileName(0, path,
sizeof(path) / sizeof(path[0])) > 0) {
- char quoted_path[1024];
- snprintf(quoted_path, sizeof(quoted_path), "\"%s\"",
path);
+ const auto quoted_path = std::string{"\""} + path +
"\"";
ServiceHandle service{
- s_winapi.CreateService(
- serviceControlManager, gszServiceName, gszServiceName,
- SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
- SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, quoted_path),
+ s_winapi.CreateService(serviceControlManager, gszServiceName,
+ gszServiceName, SERVICE_ALL_ACCESS,
+ SERVICE_WIN32_OWN_PROCESS,
+ SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
+ quoted_path.c_str()),
s_winapi};
if (service) {
std::cout << SERVICE_NAME << " Installed
Successfully"
diff --git a/agents/windows/sections/SectionDF.cc b/agents/windows/sections/SectionDF.cc
index 7b738b1..d4c0be7 100644
--- a/agents/windows/sections/SectionDF.cc
+++ b/agents/windows/sections/SectionDF.cc
@@ -42,13 +42,6 @@ struct MountPointHandleTraits {
}
};
-void char_replace(char what, char into, char *in) {
- while (*in) {
- if (*in == what) *in = into;
- in++;
- }
-}
-
} // namespace
SectionDF::SectionDF(const Environment &env, Logger *logger,
@@ -57,51 +50,53 @@ SectionDF::SectionDF(const Environment &env, Logger *logger,
std::make_unique<SectionHeader<'\t',
SectionBrackets>>("df",
logger)) {}
-void SectionDF::output_filesystem(std::ostream &out, char *volid) {
+void SectionDF::output_filesystem(std::ostream &out, const std::string &volid) {
static const int KiloByte = 1024;
- char fsname[128];
- char volume[512];
+ char fsname[128] = {0};
+ char volume[512] = {0};
DWORD dwSysFlags = 0;
- if (!_winapi.GetVolumeInformation(volid, volume, sizeof(volume), 0, 0,
- &dwSysFlags, fsname, sizeof(fsname)))
- fsname[0] = 0;
+ if (!_winapi.GetVolumeInformation(volid.c_str(), volume, sizeof(volume), 0,
+ 0, &dwSysFlags, fsname, sizeof(fsname))) {
+ fsname[0] = '\0'; // May be necessary if partial information returned
+ }
ULARGE_INTEGER free_avail, total, free;
free_avail.QuadPart = 0;
total.QuadPart = 0;
free.QuadPart = 0;
int returnvalue =
- _winapi.GetDiskFreeSpaceEx(volid, &free_avail, &total, &free);
+ _winapi.GetDiskFreeSpaceEx(volid.c_str(), &free_avail, &total,
&free);
if (returnvalue > 0) {
double perc_used = 0;
if (total.QuadPart > 0)
perc_used = 100 - (100 * free_avail.QuadPart / total.QuadPart);
- if (volume[0]) // have a volume name
- char_replace(' ', '_', volume);
+ std::string volumeStr{volume};
+ if (!volumeStr.empty()) // have a volume name
+ std::replace(volumeStr.begin(), volumeStr.end(), ' ', '_');
else
- strncpy(volume, volid, sizeof(volume));
+ volumeStr = volid;
- out << volume << "\t" << fsname <<
"\t" << (total.QuadPart / KiloByte)
- << "\t" << (total.QuadPart - free_avail.QuadPart) /
KiloByte << "\t"
+ out << volumeStr << "\t" << fsname <<
"\t"
+ << (total.QuadPart / KiloByte) << "\t"
+ << (total.QuadPart - free_avail.QuadPart) / KiloByte <<
"\t"
<< (free_avail.QuadPart / KiloByte) << "\t" <<
std::fixed
<< std::setprecision(0) << perc_used << "%\t"
<< volid << "\n";
}
}
-void SectionDF::output_mountpoints(std::ostream &out, char *volid) {
+void SectionDF::output_mountpoints(std::ostream &out,
+ const std::string &volid) {
char mountpoint[512];
WrappedHandle<MountPointHandleTraits> hPt{
- _winapi.FindFirstVolumeMountPoint(volid, mountpoint,
+ _winapi.FindFirstVolumeMountPoint(volid.c_str(), mountpoint,
sizeof(mountpoint)),
_winapi};
if (hPt) {
while (true) {
- char combined_path[1024];
- snprintf(combined_path, sizeof(combined_path), "%s%s", volid,
- mountpoint);
+ const std::string combined_path = volid + mountpoint;
output_filesystem(out, combined_path);
if (!_winapi.FindNextVolumeMountPoint(hPt.get(), mountpoint,
sizeof(mountpoint)))
diff --git a/agents/windows/sections/SectionDF.h b/agents/windows/sections/SectionDF.h
index 4302b0d..d42623b 100644
--- a/agents/windows/sections/SectionDF.h
+++ b/agents/windows/sections/SectionDF.h
@@ -39,8 +39,8 @@ protected:
std::ostream &out, const std::optional<std::string> &) override;
private:
- void output_filesystem(std::ostream &out, char *volid);
- void output_mountpoints(std::ostream &out, char *volid);
+ void output_filesystem(std::ostream &out, const std::string &volid);
+ void output_mountpoints(std::ostream &out, const std::string &volid);
};
#endif // SectionDF_h