From 3c5af89d5f8be2e3bcf0b0c334e21485c707de70 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 14 Dec 2017 08:47:04 +0100 Subject: [PATCH] Fix compiler warnings --- agent/windows-setup-agent/Program.cs | 23 ++++++++++++---------- lib/base/array.cpp | 18 ++++++++--------- lib/base/array.hpp | 14 +++++++------- lib/base/initialize.hpp | 9 +++++++-- lib/base/socket.cpp | 29 ++++++++++++++-------------- lib/base/tcpsocket.cpp | 2 +- lib/base/visibility.hpp | 5 ----- lib/base/workqueue.cpp | 6 +++--- lib/base/workqueue.hpp | 7 ++++--- lib/remote/apiclient.cpp | 6 +++--- lib/remote/jsonrpcconnection.cpp | 14 ++++++-------- lib/remote/jsonrpcconnection.hpp | 4 ++-- plugins/check_disk.cpp | 2 +- plugins/thresholds.cpp | 2 +- 14 files changed, 72 insertions(+), 69 deletions(-) diff --git a/agent/windows-setup-agent/Program.cs b/agent/windows-setup-agent/Program.cs index 4b85731f1..b22b042eb 100644 --- a/agent/windows-setup-agent/Program.cs +++ b/agent/windows-setup-agent/Program.cs @@ -7,14 +7,17 @@ using System.Text; namespace Icinga { - static class Program + internal static class NativeMethods + { + [DllImport("msi.dll", CharSet = CharSet.Unicode)] + internal static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf); + + [DllImport("msi.dll", CharSet = CharSet.Unicode)] + internal static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len); + } + + static class Program { - [DllImport("msi.dll", SetLastError = true)] - static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf); - - [DllImport("msi.dll", CharSet = CharSet.Unicode)] - static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len); - public static string Icinga2InstallDir { get @@ -23,13 +26,13 @@ namespace Icinga for (int index = 0; ; index++) { szProduct = new StringBuilder(39); - if (MsiEnumProducts(index, szProduct) != 0) + if (NativeMethods.MsiEnumProducts(index, szProduct) != 0) break; int cbName = 128; StringBuilder szName = new StringBuilder(cbName); - if (MsiGetProductInfo(szProduct.ToString(), "ProductName", szName, ref cbName) != 0) + if (NativeMethods.MsiGetProductInfo(szProduct.ToString(), "ProductName", szName, ref cbName) != 0) continue; if (szName.ToString() != "Icinga 2") @@ -37,7 +40,7 @@ namespace Icinga int cbLocation = 1024; StringBuilder szLocation = new StringBuilder(cbLocation); - if (MsiGetProductInfo(szProduct.ToString(), "InstallLocation", szLocation, ref cbLocation) == 0) + if (NativeMethods.MsiGetProductInfo(szProduct.ToString(), "InstallLocation", szLocation, ref cbLocation) == 0) return szLocation.ToString(); } diff --git a/lib/base/array.cpp b/lib/base/array.cpp index 21dcc7613..32d97a4fd 100644 --- a/lib/base/array.cpp +++ b/lib/base/array.cpp @@ -36,7 +36,7 @@ REGISTER_PRIMITIVE_TYPE(Array, Object, Array::GetPrototype()); * @param index The index. * @returns The value. */ -Value Array::Get(unsigned int index) const +Value Array::Get(SizeType index) const { ObjectLock olock(this); @@ -49,7 +49,7 @@ Value Array::Get(unsigned int index) const * @param index The index. * @param value The value. */ -void Array::Set(unsigned int index, const Value& value) +void Array::Set(SizeType index, const Value& value) { ObjectLock olock(this); @@ -62,7 +62,7 @@ void Array::Set(unsigned int index, const Value& value) * @param index The index. * @param value The value. */ -void Array::Set(unsigned int index, Value&& value) +void Array::Set(SizeType index, Value&& value) { ObjectLock olock(this); @@ -124,7 +124,7 @@ bool Array::Contains(const Value& value) const * @param index The index * @param value The value to add */ -void Array::Insert(unsigned int index, const Value& value) +void Array::Insert(SizeType index, const Value& value) { ObjectLock olock(this); @@ -138,7 +138,7 @@ void Array::Insert(unsigned int index, const Value& value) * * @param index The index. */ -void Array::Remove(unsigned int index) +void Array::Remove(SizeType index) { ObjectLock olock(this); @@ -157,11 +157,11 @@ void Array::Remove(Array::Iterator it) m_Data.erase(it); } -void Array::Resize(size_t new_size) +void Array::Resize(SizeType newSize) { ObjectLock olock(this); - m_Data.resize(new_size); + m_Data.resize(newSize); } void Array::Clear(void) @@ -171,11 +171,11 @@ void Array::Clear(void) m_Data.clear(); } -void Array::Reserve(size_t new_size) +void Array::Reserve(SizeType newSize) { ObjectLock olock(this); - m_Data.reserve(new_size); + m_Data.reserve(newSize); } void Array::CopyTo(const Array::Ptr& dest) const diff --git a/lib/base/array.hpp b/lib/base/array.hpp index 7abb86e63..682fdfe81 100644 --- a/lib/base/array.hpp +++ b/lib/base/array.hpp @@ -57,9 +57,9 @@ public: inline ~Array(void) { } - Value Get(unsigned int index) const; - void Set(unsigned int index, const Value& value); - void Set(unsigned int index, Value&& value); + Value Get(SizeType index) const; + void Set(SizeType index, const Value& value); + void Set(SizeType index, Value&& value); void Add(const Value& value); void Add(Value&& value); @@ -94,14 +94,14 @@ public: size_t GetLength(void) const; bool Contains(const Value& value) const; - void Insert(unsigned int index, const Value& value); - void Remove(unsigned int index); + void Insert(SizeType index, const Value& value); + void Remove(SizeType index); void Remove(Iterator it); - void Resize(size_t new_size); + void Resize(SizeType newSize); void Clear(void); - void Reserve(size_t new_size); + void Reserve(SizeType newSize); void CopyTo(const Array::Ptr& dest) const; Array::Ptr ShallowClone(void) const; diff --git a/lib/base/initialize.hpp b/lib/base/initialize.hpp index 92c8e9571..00162ea48 100644 --- a/lib/base/initialize.hpp +++ b/lib/base/initialize.hpp @@ -25,15 +25,20 @@ namespace icinga { +#define I2_TOKENPASTE(x, y) x ## y +#define I2_TOKENPASTE2(x, y) I2_TOKENPASTE(x, y) + +#define I2_UNIQUE_NAME(prefix) I2_TOKENPASTE2(prefix, __COUNTER__) + I2_BASE_API bool InitializeOnceHelper(void (*func)(void), int priority = 0); #define INITIALIZE_ONCE(func) \ - namespace { namespace UNIQUE_NAME(io) { \ + namespace { namespace I2_UNIQUE_NAME(io) { \ I2_EXPORT bool l_InitializeOnce(icinga::InitializeOnceHelper(func)); \ } } #define INITIALIZE_ONCE_WITH_PRIORITY(func, priority) \ - namespace { namespace UNIQUE_NAME(io) { \ + namespace { namespace I2_UNIQUE_NAME(io) { \ I2_EXPORT bool l_InitializeOnce(icinga::InitializeOnceHelper(func, priority)); \ } } } diff --git a/lib/base/socket.cpp b/lib/base/socket.cpp index 026da03c3..b3811196c 100644 --- a/lib/base/socket.cpp +++ b/lib/base/socket.cpp @@ -330,29 +330,30 @@ size_t Socket::Read(void *buffer, size_t count) */ Socket::Ptr Socket::Accept(void) { - int fd; sockaddr_storage addr; socklen_t addrlen = sizeof(addr); - fd = accept(GetFD(), (sockaddr *)&addr, &addrlen); + SOCKET fd = accept(GetFD(), (sockaddr *)&addr, &addrlen); - if (fd < 0) { #ifndef _WIN32 + if (fd < 0) { Log(LogCritical, "Socket") - << "accept() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\""; + << "accept() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\""; BOOST_THROW_EXCEPTION(socket_error() - << boost::errinfo_api_function("accept") - << boost::errinfo_errno(errno)); -#else /* _WIN32 */ - Log(LogCritical, "Socket") - << "accept() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\""; - - BOOST_THROW_EXCEPTION(socket_error() - << boost::errinfo_api_function("accept") - << errinfo_win32_error(WSAGetLastError())); -#endif /* _WIN32 */ + << boost::errinfo_api_function("accept") + << boost::errinfo_errno(errno)); } +#else /* _WIN32 */ + if (fd == INVALID_SOCKET) { + Log(LogCritical, "Socket") + << "accept() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\""; + + BOOST_THROW_EXCEPTION(socket_error() + << boost::errinfo_api_function("accept") + << errinfo_win32_error(WSAGetLastError())); + } +#endif /* _WIN32 */ return new Socket(fd); } diff --git a/lib/base/tcpsocket.cpp b/lib/base/tcpsocket.cpp index 734f0878d..113306436 100644 --- a/lib/base/tcpsocket.cpp +++ b/lib/base/tcpsocket.cpp @@ -161,7 +161,7 @@ void TcpSocket::Connect(const String& node, const String& service) << errinfo_getaddrinfo_error(rc)); } - int fd = INVALID_SOCKET; + SOCKET fd = INVALID_SOCKET; for (addrinfo *info = result; info != NULL; info = info->ai_next) { fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol); diff --git a/lib/base/visibility.hpp b/lib/base/visibility.hpp index d80dbc4f3..91f2252e5 100644 --- a/lib/base/visibility.hpp +++ b/lib/base/visibility.hpp @@ -29,9 +29,4 @@ # define I2_HIDDEN #endif /* _WIN32 */ -#define TOKENPASTE(x, y) x ## y -#define TOKENPASTE2(x, y) TOKENPASTE(x, y) - -#define UNIQUE_NAME(prefix) TOKENPASTE2(prefix, __COUNTER__) - #endif /* VISIBILITY_H */ diff --git a/lib/base/workqueue.cpp b/lib/base/workqueue.cpp index a59acdcad..c9ee69bed 100644 --- a/lib/base/workqueue.cpp +++ b/lib/base/workqueue.cpp @@ -27,7 +27,7 @@ using namespace icinga; -int WorkQueue::m_NextID = 1; +std::atomic WorkQueue::m_NextID(1); boost::thread_specific_ptr l_ThreadWorkQueue; WorkQueue::WorkQueue(size_t maxItems, int threadCount) @@ -196,7 +196,7 @@ void WorkQueue::StatusTimerHandler(void) ASSERT(!m_Name.IsEmpty()); - int pending = m_Tasks.size(); + size_t pending = m_Tasks.size(); double now = Utility::GetTime(); double gradient = (pending - m_PendingTasks) / (now - m_PendingTasksTimestamp); @@ -295,7 +295,7 @@ void WorkQueue::IncreaseTaskCount(void) m_TaskStats.InsertValue(now, 1); } -int WorkQueue::GetTaskCount(RingBuffer::SizeType span) +size_t WorkQueue::GetTaskCount(RingBuffer::SizeType span) { boost::mutex::scoped_lock lock(m_StatsMutex); return m_TaskStats.UpdateAndGetValues(Utility::GetTime(), span); diff --git a/lib/base/workqueue.hpp b/lib/base/workqueue.hpp index c44e117fd..c88677c02 100644 --- a/lib/base/workqueue.hpp +++ b/lib/base/workqueue.hpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace icinga { @@ -93,7 +94,7 @@ public: bool IsWorkerThread(void) const; size_t GetLength(void) const; - int GetTaskCount(RingBuffer::SizeType span); + size_t GetTaskCount(RingBuffer::SizeType span); void SetExceptionCallback(const ExceptionCallback& callback); @@ -107,7 +108,7 @@ protected: private: int m_ID; String m_Name; - static int m_NextID; + static std::atomic m_NextID; int m_ThreadCount; bool m_Spawned; @@ -128,7 +129,7 @@ private: mutable boost::mutex m_StatsMutex; RingBuffer m_TaskStats; - int m_PendingTasks; + size_t m_PendingTasks; double m_PendingTasksTimestamp; void WorkerThreadProc(void); diff --git a/lib/remote/apiclient.cpp b/lib/remote/apiclient.cpp index 8cf41d38f..3638485d9 100644 --- a/lib/remote/apiclient.cpp +++ b/lib/remote/apiclient.cpp @@ -294,7 +294,7 @@ void ApiClient::ExecuteScriptHttpCompletionCallback(HttpRequest& request, } callback(boost::exception_ptr(), result); - } catch (const std::exception& ex) { + } catch (const std::exception&) { callback(boost::current_exception(), Empty); } } @@ -321,7 +321,7 @@ void ApiClient::AutocompleteScript(const String& session, const String& command, req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password)); req->AddHeader("Accept", "application/json"); m_Connection->SubmitRequest(req, std::bind(AutocompleteScriptHttpCompletionCallback, _1, _2, callback)); - } catch (const std::exception& ex) { + } catch (const std::exception&) { callback(boost::current_exception(), nullptr); } } @@ -362,7 +362,7 @@ void ApiClient::AutocompleteScriptHttpCompletionCallback(HttpRequest& request, } callback(boost::exception_ptr(), suggestions); - } catch (const std::exception& ex) { + } catch (const std::exception&) { callback(boost::current_exception(), nullptr); } } diff --git a/lib/remote/jsonrpcconnection.cpp b/lib/remote/jsonrpcconnection.cpp index 95be46984..cf397f9a9 100644 --- a/lib/remote/jsonrpcconnection.cpp +++ b/lib/remote/jsonrpcconnection.cpp @@ -322,18 +322,17 @@ void JsonRpcConnection::TimeoutTimerHandler(void) } } -int JsonRpcConnection::GetWorkQueueCount(void) +size_t JsonRpcConnection::GetWorkQueueCount(void) { return l_JsonRpcConnectionWorkQueueCount; } -int JsonRpcConnection::GetWorkQueueLength(void) +size_t JsonRpcConnection::GetWorkQueueLength(void) { - int itemCount = 0; + size_t itemCount = 0; - for (int i = 0; i < GetWorkQueueCount(); i++) { + for (size_t i = 0; i < GetWorkQueueCount(); i++) itemCount += l_JsonRpcConnectionWorkQueues[i].GetLength(); - } return itemCount; } @@ -341,15 +340,14 @@ int JsonRpcConnection::GetWorkQueueLength(void) double JsonRpcConnection::GetWorkQueueRate(void) { double rate = 0.0; - int count = GetWorkQueueCount(); + size_t count = GetWorkQueueCount(); /* If this is a standalone environment, we don't have any queues. */ if (count == 0) return 0.0; - for (int i = 0; i < count; i++) { + for (size_t i = 0; i < count; i++) rate += l_JsonRpcConnectionWorkQueues[i].GetTaskCount(60) / 60.0; - } return rate / count; } diff --git a/lib/remote/jsonrpcconnection.hpp b/lib/remote/jsonrpcconnection.hpp index e62799c1e..d4fce95cd 100644 --- a/lib/remote/jsonrpcconnection.hpp +++ b/lib/remote/jsonrpcconnection.hpp @@ -71,8 +71,8 @@ public: static void HeartbeatTimerHandler(void); static Value HeartbeatAPIHandler(const intrusive_ptr& origin, const Dictionary::Ptr& params); - static int GetWorkQueueCount(void); - static int GetWorkQueueLength(void); + static size_t GetWorkQueueCount(void); + static size_t GetWorkQueueLength(void); static double GetWorkQueueRate(void); static void SendCertificateRequest(const JsonRpcConnection::Ptr& aclient, const intrusive_ptr& origin, const String& path); diff --git a/plugins/check_disk.cpp b/plugins/check_disk.cpp index 124236847..f4866c24c 100644 --- a/plugins/check_disk.cpp +++ b/plugins/check_disk.cpp @@ -186,7 +186,7 @@ static INT parseArguments(INT ac, WCHAR **av, po::variables_map& vm, printInfoSt printInfo.unit = BunitB; } - printInfo.showUsed = vm.count("show-used"); + printInfo.showUsed = vm.count("show-used") > 0; if (vm.count("debug")) debug = TRUE; diff --git a/plugins/thresholds.cpp b/plugins/thresholds.cpp index 29aede818..0d0ffc782 100644 --- a/plugins/thresholds.cpp +++ b/plugins/thresholds.cpp @@ -129,7 +129,7 @@ std::wstring threshold::pString(CONST DOUBLE max) std::wstring removeZero(DOUBLE val) { std::wstring ret = boost::lexical_cast(val); - INT pos = ret.length(); + std::wstring::size_type pos = ret.length(); if (ret.find_first_of(L".") == std::string::npos) return ret; for (std::wstring::reverse_iterator rit = ret.rbegin(); rit != ret.rend(); ++rit) {