diff --git a/src/common/cmdHelper/include/cmdHelper.h b/src/common/cmdHelper/include/cmdHelper.h index dd1653813e..b60159d043 100644 --- a/src/common/cmdHelper/include/cmdHelper.h +++ b/src/common/cmdHelper/include/cmdHelper.h @@ -9,16 +9,21 @@ * Foundation. */ -#ifndef _CMD_HELPER_H -#define _CMD_HELPER_H +#pragma once #include #include #include #include +#include +#ifndef WIN32 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" +#else +FILE *popen(const char *command, const char *mode) { return NULL; } +int pclose(FILE *stream){ return 0; } +#endif namespace Utils { @@ -32,14 +37,14 @@ namespace Utils static std::string exec(const std::string& cmd, const size_t bufferSize = 128) { const std::unique_ptr file{popen(cmd.c_str(), "r")}; - char buffer[bufferSize]; + std::vector buffer(bufferSize); std::string result; if (file) { - while (fgets(buffer, bufferSize, file.get())) + while (fgets(buffer.data(), bufferSize, file.get())) { - result += buffer; + result += buffer.data(); } } @@ -49,4 +54,3 @@ namespace Utils #pragma GCC diagnostic pop -#endif // _CMD_HELPER_H \ No newline at end of file diff --git a/src/common/data_provider/CMakeLists.txt b/src/common/data_provider/CMakeLists.txt index c4907b3d0f..f1c347d690 100644 --- a/src/common/data_provider/CMakeLists.txt +++ b/src/common/data_provider/CMakeLists.txt @@ -92,6 +92,20 @@ include_directories(${SRC_FOLDER}/common/jsonHelper/include/) include_directories(${SRC_FOLDER}/common/linuxHelper/include/) include_directories(${SRC_FOLDER}/common/stringHelper/include/) include_directories(${SRC_FOLDER}/common/timeHelper/include/) +include_directories(${SRC_FOLDER}/common/time_op/include/) +include_directories(${SRC_FOLDER}/common/file_op/include/) +include_directories(${SRC_FOLDER}/common/regex_op/include/) +include_directories(${SRC_FOLDER}/common/debug_op/include/) +include_directories(${SRC_FOLDER}/common/string_op/include/) +include_directories(${SRC_FOLDER}/common/bzip2_op/include/) +include_directories(${SRC_FOLDER}/common/validate_op/include/) +include_directories(${SRC_FOLDER}/common/binaries_op/include/) +include_directories(${SRC_FOLDER}/common/os_xml/include/) +include_directories(${SRC_FOLDER}/common/os_regex/include/) +include_directories(${SRC_FOLDER}/common/os_net/include/) +include_directories(${SRC_FOLDER}/common/expression/include/) +include_directories(${SRC_FOLDER}/common/randombytes/include/) +include_directories(${SRC_FOLDER}/common/error_messages/include/) include_directories(${SRC_FOLDER}/shared_modules/common/) include_directories(${SRC_FOLDER}/external/libdb/build_unix/) if(LINUX) @@ -104,9 +118,12 @@ if(LINUX) include_directories(${SRC_FOLDER}/common/http-request/shared/) endif() -if(CMAKE_SYSTEM_NAME STREQUAL "HP-UX") - link_directories(${INSTALL_PREFIX}/lib) -endif(CMAKE_SYSTEM_NAME STREQUAL "HP-UX") +if(WIN32) + include_directories(${SRC_FOLDER}/common/registryHelper/include/) + include_directories(${SRC_FOLDER}/common/windowsHelper/include/) + include_directories(${SRC_FOLDER}/common/encodingHelper/include/) + include_directories(${SRC_FOLDER}/common/mem_op/include/) +endif() link_directories(${SRC_FOLDER}) link_directories(${SRC_FOLDER}/external/procps/) @@ -196,9 +213,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows") set_target_properties(sysinfo PROPERTIES PREFIX "" SUFFIX ".dll" - LINK_FLAGS "-Wl,--add-stdcall-alias" - POSITION_INDEPENDENT_CODE 0 # this is to avoid MinGW warning; - # MinGW generates position-independent-code for DLL by default ) elseif(UNIX AND NOT APPLE) string(APPEND CMAKE_SHARED_LINKER_FLAGS " -Wl,-rpath=$ORIGIN") diff --git a/src/common/data_provider/src/network/networkWindowsWrapper.h b/src/common/data_provider/src/network/networkWindowsWrapper.h index dedec064c3..5deb35317e 100644 --- a/src/common/data_provider/src/network/networkWindowsWrapper.h +++ b/src/common/data_provider/src/network/networkWindowsWrapper.h @@ -19,7 +19,6 @@ #include #include "windowsHelper.h" #include "inetworkWrapper.h" -#include "makeUnique.h" #include "sharedDefs.h" static const std::map NETWORK_INTERFACE_TYPES = diff --git a/src/common/data_provider/src/ports/portWindowsWrapper.h b/src/common/data_provider/src/ports/portWindowsWrapper.h index d8d95b6b6c..51bc34dce7 100644 --- a/src/common/data_provider/src/ports/portWindowsWrapper.h +++ b/src/common/data_provider/src/ports/portWindowsWrapper.h @@ -16,6 +16,8 @@ #include "sharedDefs.h" #include "stringHelper.h" #include "windowsHelper.h" +#include "shared.h" +#include "cust_types.h" static const std::map STATE_TYPE = { diff --git a/src/common/dbsync/CMakeLists.txt b/src/common/dbsync/CMakeLists.txt index f37dc700e1..52a39403f7 100644 --- a/src/common/dbsync/CMakeLists.txt +++ b/src/common/dbsync/CMakeLists.txt @@ -28,7 +28,7 @@ if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set(CMAKE_CXX_FLAGS_DEBUG "-g -fsanitize=address,leak,undefined") endif(FSANITIZE) else() - set(CMAKE_CXX_FLAGS "/W4 /permissive- /std:c++14 /MT") +set(CMAKE_CXX_FLAGS "/W4 /permissive- /std:c++14 /MT /EHsc") endif() set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) diff --git a/src/common/dbsync/example/CMakeLists.txt b/src/common/dbsync/example/CMakeLists.txt index 6aef6ad456..f8ac73f6cb 100644 --- a/src/common/dbsync/example/CMakeLists.txt +++ b/src/common/dbsync/example/CMakeLists.txt @@ -21,7 +21,6 @@ add_executable(dbsync_example if(CMAKE_SYSTEM_NAME STREQUAL "Windows") target_link_libraries(dbsync_example dbsync - pthread -static-libgcc -static-libstdc++ ) elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") diff --git a/src/common/dbsync/include/db_exception.h b/src/common/dbsync/include/db_exception.h index 1295089cad..bee9eb3b69 100644 --- a/src/common/dbsync/include/db_exception.h +++ b/src/common/dbsync/include/db_exception.h @@ -14,6 +14,12 @@ #include #include +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_RET_NONNULL __attribute__((__returns_nonnull__)) +#else +#define ATTR_RET_NONNULL +#endif + using DBSyncExceptionType = const std::pair; DBSyncExceptionType FACTORY_INSTANTATION { std::make_pair(1, "Unspecified type during factory instantiation") }; @@ -48,7 +54,7 @@ namespace DbSync class dbsync_error : public std::exception { public: - __attribute__((__returns_nonnull__)) + ATTR_RET_NONNULL const char* what() const noexcept override { return m_error.what(); @@ -82,7 +88,7 @@ namespace DbSync class max_rows_error : public std::exception { public: - __attribute__((__returns_nonnull__)) + ATTR_RET_NONNULL const char* what() const noexcept override { return m_error.what(); diff --git a/src/common/dbsync/src/sqlite/sqlite_wrapper.cpp b/src/common/dbsync/src/sqlite/sqlite_wrapper.cpp index 7f8a549150..9546a0f0cb 100644 --- a/src/common/dbsync/src/sqlite/sqlite_wrapper.cpp +++ b/src/common/dbsync/src/sqlite/sqlite_wrapper.cpp @@ -11,7 +11,6 @@ #include "sqlite_wrapper.h" #include "db_exception.h" -#include "makeUnique.h" #include "customDeleter.hpp" #include #include diff --git a/src/common/dbsync/src/sqlite/sqlite_wrapper_factory.h b/src/common/dbsync/src/sqlite/sqlite_wrapper_factory.h index 60cf1ac527..d65a71fc81 100644 --- a/src/common/dbsync/src/sqlite/sqlite_wrapper_factory.h +++ b/src/common/dbsync/src/sqlite/sqlite_wrapper_factory.h @@ -13,7 +13,7 @@ #define _SQLITE_WRAPPER_FACTORY_H #include "sqlite_wrapper.h" -#include "makeUnique.h" + class ISQLiteFactory { public: diff --git a/src/common/dbsync/testtool/CMakeLists.txt b/src/common/dbsync/testtool/CMakeLists.txt index 67eb81e7f0..7f67cf6164 100644 --- a/src/common/dbsync/testtool/CMakeLists.txt +++ b/src/common/dbsync/testtool/CMakeLists.txt @@ -14,7 +14,7 @@ endif(COVERITY) if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set(CMAKE_CXX_FLAGS "-Wall -Wextra -std=c++14 -pthread") else() - set(CMAKE_CXX_FLAGS "/W4 /permissive- /std:c++14 /MT") + set(CMAKE_CXX_FLAGS "/W4 /permissive- /std:c++14 /MT /EHsc") if(FSANITIZE) set(CMAKE_CXX_FLAGS_DEBUG "-fsanitize=address,leak,undefined") endif(FSANITIZE) diff --git a/src/common/dbsync/testtool/action.h b/src/common/dbsync/testtool/action.h index 1ab62fd3b0..a7b77cdb4f 100644 --- a/src/common/dbsync/testtool/action.h +++ b/src/common/dbsync/testtool/action.h @@ -8,12 +8,10 @@ * License (version 2) as published by the FSF - Free Software * Foundation. */ -#ifndef _ACTION_H -#define _ACTION_H +#pragma once #include #include #include "dbsync.h" -#include "makeUnique.h" #include "cjsonSmartDeleter.hpp" namespace TestDeleters @@ -828,5 +826,3 @@ struct SelectRowsActionCPP final : public IAction } }; - -#endif //_ACTION_H diff --git a/src/common/dbsync/testtool/main.cpp b/src/common/dbsync/testtool/main.cpp index c40e5310b2..35452813e9 100644 --- a/src/common/dbsync/testtool/main.cpp +++ b/src/common/dbsync/testtool/main.cpp @@ -13,7 +13,6 @@ #include #include #include -#include "makeUnique.h" #include "dbsync.h" #include "cmdArgsHelper.h" #include "testContext.h" diff --git a/src/common/debug_op/include/debug_op.h b/src/common/debug_op/include/debug_op.h index 4472556bc6..4cdbef0933 100644 --- a/src/common/debug_op/include/debug_op.h +++ b/src/common/debug_op/include/debug_op.h @@ -42,6 +42,20 @@ extern "C" { #endif #endif +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_NONNULL __attribute__((nonnull)) +#define ATTR_NORETURN __attribute__ ((noreturn)) +#define ATTR_PRINTF_FIRST_SEC __attribute__((format(_PRINTF_FORMAT, 1, 2))) +#define ATTR_PRINTF_FOURTH_FIFTH __attribute__((format(_PRINTF_FORMAT, 4, 5))) +#define ATTR_PRINTF_FIFTH_SIXTH __attribute__((format(_PRINTF_FORMAT, 5, 6))) +#else +#define ATTR_NONNULL +#define ATTR_NORETURN +#define ATTR_PRINTF_FIRST_SEC +#define ATTR_PRINTF_FOURTH_FIFTH +#define ATTR_PRINTF_FIFTH_SIXTH +#endif + #define mdebug1(msg, ...) _mdebug1(__FILE__, __LINE__, __func__, msg, ##__VA_ARGS__) #define plain_mdebug1(msg, ...) _plain_mdebug1(__FILE__, __LINE__, __func__, msg, ##__VA_ARGS__) #define mtdebug1(tag, msg, ...) _mtdebug1(tag, __FILE__, __LINE__, __func__, msg, ##__VA_ARGS__) @@ -63,30 +77,30 @@ extern "C" { #define mterror_exit(tag, msg, ...) _mterror_exit(tag, __FILE__, __LINE__, __func__, msg, ##__VA_ARGS__) #define mlerror_exit(level, msg, ...) _mlerror_exit(level, __FILE__, __LINE__, __func__, msg, ##__VA_ARGS__) -void _mdebug1(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)); -void _plain_mdebug1(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)); -void _mtdebug1(const char *tag, const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 5, 6))) __attribute__((nonnull)); -void _mdebug2(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)); -void _mtdebug2(const char *tag, const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 5, 6))) __attribute__((nonnull)); -void _merror(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)); -void _plain_merror(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)); -void _mterror(const char *tag, const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 5, 6))) __attribute__((nonnull)); -void _mverror(const char * file, int line, const char * func, const char *msg, va_list args) __attribute__((nonnull)); -void _mwarn(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)); -void _plain_mwarn(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)); -void _mtwarn(const char *tag, const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 5, 6))) __attribute__((nonnull)); -void _mvwarn(const char * file, int line, const char * func, const char *msg, va_list args) __attribute__((nonnull)); -void _minfo(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)); -void _plain_minfo(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)); -void _mtinfo(const char *tag, const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 5, 6))) __attribute__((nonnull)); -void _mvinfo(const char * file, int line, const char * func, const char *msg, va_list args) __attribute__((nonnull)); -void print_out(const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 1, 2))) __attribute__((nonnull)); -void _mferror(const char * file, int line, const char * func, const char *msg, ... ) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)); -void _mtferror(const char *tag, const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 5, 6))) __attribute__((nonnull)); -void _merror_exit(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)) __attribute__ ((noreturn)); -void _plain_merror_exit(const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 4, 5))) __attribute__((nonnull)) __attribute__ ((noreturn)); -void _mterror_exit(const char *tag, const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 5, 6))) __attribute__((nonnull)) __attribute__ ((noreturn)); -void _mlerror_exit(const int level, const char * file, int line, const char * func, const char *msg, ...) __attribute__((format(_PRINTF_FORMAT, 5, 6))) __attribute__((nonnull)) __attribute__ ((noreturn)); +void _mdebug1(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL; +void _plain_mdebug1(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL; +void _mtdebug1(const char *tag, const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FIFTH_SIXTH ATTR_NONNULL; +void _mdebug2(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL; +void _mtdebug2(const char *tag, const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FIFTH_SIXTH ATTR_NONNULL; +void _merror(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL; +void _plain_merror(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL; +void _mterror(const char *tag, const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FIFTH_SIXTH ATTR_NONNULL; +void _mverror(const char * file, int line, const char * func, const char *msg, va_list args) ATTR_NONNULL; +void _mwarn(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL; +void _plain_mwarn(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL; +void _mtwarn(const char *tag, const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FIFTH_SIXTH ATTR_NONNULL; +void _mvwarn(const char * file, int line, const char * func, const char *msg, va_list args) ATTR_NONNULL; +void _minfo(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL; +void _plain_minfo(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL; +void _mtinfo(const char *tag, const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FIFTH_SIXTH ATTR_NONNULL; +void _mvinfo(const char * file, int line, const char * func, const char *msg, va_list args) ATTR_NONNULL; +void print_out(const char *msg, ...) ATTR_PRINTF_FIRST_SEC ATTR_NONNULL; +void _mferror(const char * file, int line, const char * func, const char *msg, ... ) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL; +void _mtferror(const char *tag, const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FIFTH_SIXTH ATTR_NONNULL; +void _merror_exit(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL ATTR_NORETURN; +void _plain_merror_exit(const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FOURTH_FIFTH ATTR_NONNULL ATTR_NORETURN; +void _mterror_exit(const char *tag, const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FIFTH_SIXTH ATTR_NONNULL ATTR_NORETURN; +void _mlerror_exit(const int level, const char * file, int line, const char * func, const char *msg, ...) ATTR_PRINTF_FIFTH_SIXTH ATTR_NONNULL ATTR_NORETURN; /** * @brief Logging module initializer diff --git a/src/common/debug_op/src/debug_op.c b/src/common/debug_op/src/debug_op.c index efe8cc9a35..b74ed82e91 100644 --- a/src/common/debug_op/src/debug_op.c +++ b/src/common/debug_op/src/debug_op.c @@ -11,8 +11,19 @@ #include "shared.h" #include -#ifdef WIN32 +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_NONNULL __attribute__((nonnull)) +#define ATTR_PRINTF_FIFTH_ZERO __attribute__((format(_PRINTF_FORMAT, 5, 0))) +#else #define localtime_r(x, y) localtime_s(y, x) +#define ATTR_NONNULL +#define ATTR_PRINTF_FIFTH_ZERO +static int pthread_mutex_init(pthread_mutex_t*m, void *p){ return 0; } +static void w_mutex_lock(pthread_mutex_t*m){} +static void w_mutex_unlock(pthread_mutex_t*m){} +static int pthread_mutex_lock(pthread_mutex_t*m){} +static int pthread_mutex_unlock(pthread_mutex_t*m){} +static void w_mutex_init(pthread_mutex_t*m, void *p){} #endif static int dbg_flag = 0; @@ -29,17 +40,17 @@ static struct{ static pthread_mutex_t logging_mutex; -static void _log_function(int level, const char *tag, const char * file, int line, const char * func, const char *msg, bool plain_only, va_list args) __attribute__((format(printf, 5, 0))) __attribute__((nonnull)); +static void _log_function(int level, const char *tag, const char * file, int line, const char * func, const char *msg, bool plain_only, va_list args) ATTR_PRINTF_FIFTH_ZERO ATTR_NONNULL; // Wrapper for the real _log_function -static void _log(int level, const char *tag, const char * file, int line, const char * func, const char *msg, va_list args) __attribute__((format(printf, 5, 0))) __attribute__((nonnull)); +static void _log(int level, const char *tag, const char * file, int line, const char * func, const char *msg, va_list args) ATTR_PRINTF_FIFTH_ZERO ATTR_NONNULL; static void _log(int level, const char *tag, const char * file, int line, const char * func, const char *msg, va_list args) { _log_function(level, tag, file, line, func, msg, true, args); } #ifdef WIN32 -void WinSetError(); +void WinSetError(){} #endif static void print_stderr_msg(char* timestamp, const char *tag, const char * file, int line, const char * func, const char* level, const char *msg, bool use_va_list, va_list args2) { diff --git a/src/common/file_op/include/file_op.h b/src/common/file_op/include/file_op.h index 3a1e35e9cc..ff4a902597 100644 --- a/src/common/file_op/include/file_op.h +++ b/src/common/file_op/include/file_op.h @@ -10,8 +10,7 @@ /* Functions to handle operation with files */ -#ifndef FILE_OP_H -#define FILE_OP_H +#pragma once #include #include @@ -41,6 +40,16 @@ extern int isVista; typedef ino_t wino_t; #endif +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_NONNULL __attribute__((nonnull)) +#define ATTR_NONNULL_ONE __attribute__((nonnull(1))) +#define ATTR_NONNULL_ONE_TWO __attribute__((nonnull(1, 2))) +#else +#define ATTR_NONNULL +#define ATTR_NONNULL_ONE +#define ATTR_NONNULL_ONE_TWO +#endif + typedef struct File { char *name; FILE *fp; @@ -51,7 +60,7 @@ typedef struct File { * * @param name Program name. */ -void OS_SetName(const char *name) __attribute__((nonnull)); +void OS_SetName(const char *name) ATTR_NONNULL; /** @@ -68,7 +77,7 @@ cJSON* getunameJSON(); * @param file File name. * @return Time of last modification or -1 on error. */ -time_t File_DateofChange(const char *file) __attribute__((nonnull)); +time_t File_DateofChange(const char *file) ATTR_NONNULL; /** @@ -77,7 +86,7 @@ time_t File_DateofChange(const char *file) __attribute__((nonnull)); * @param file File name. * @return File inode or 0 on error. */ -ino_t File_Inode(const char *file) __attribute__((nonnull)); +ino_t File_Inode(const char *file) ATTR_NONNULL; /** @@ -135,7 +144,7 @@ long get_fp_size(FILE * fp); * @param file File path. * @return 0 if it is a directory, -1 otherwise. */ -int IsDir(const char *file) __attribute__((nonnull)); +int IsDir(const char *file) ATTR_NONNULL; /** @@ -144,7 +153,7 @@ int IsDir(const char *file) __attribute__((nonnull)); * @param file File path. * @return 0 if it is a regular file, -1 otherwise. */ -int IsFile(const char *file) __attribute__((nonnull)); +int IsFile(const char *file) ATTR_NONNULL; /** @@ -153,7 +162,7 @@ int IsFile(const char *file) __attribute__((nonnull)); * @param file File path. * @return 0 if it is a socket, -1 otherwise. */ -int IsSocket(const char * file) __attribute__((nonnull)); +int IsSocket(const char * file) ATTR_NONNULL; /** @@ -162,7 +171,7 @@ int IsSocket(const char * file) __attribute__((nonnull)); * @param dir File path. * @return 1 if it is a file, 2 if it is a directory, 0 otherwise. */ -int check_path_type(const char *dir) __attribute__((nonnull)); +int check_path_type(const char *dir) ATTR_NONNULL; #ifndef WIN32 @@ -172,7 +181,7 @@ int check_path_type(const char *dir) __attribute__((nonnull)); * @param file File path. * @return 0 if it is a link, -1 otherwise. */ -int IsLink(const char * file) __attribute__((nonnull)); +int IsLink(const char * file) ATTR_NONNULL; #endif @@ -191,7 +200,7 @@ char *GetRandomNoise(); * @param pid Service PID. * @return 0 if the file was created, -1 on error. */ -int CreatePID(const char *name, int pid) __attribute__((nonnull)); +int CreatePID(const char *name, int pid) ATTR_NONNULL; /** @@ -200,7 +209,7 @@ int CreatePID(const char *name, int pid) __attribute__((nonnull)); * @param name Service name. * @return 0 if the file was deleted, -1 on error. */ -int DeletePID(const char *name) __attribute__((nonnull)); +int DeletePID(const char *name) ATTR_NONNULL; /** @@ -219,7 +228,7 @@ void DeleteState(); * @param path_offset Offset for recursion. * @return 1 if the merged file was created, 0 on error. */ -int MergeAppendFile(FILE *finalfp, const char *files, int path_offset) __attribute__((nonnull(1, 2))); +int MergeAppendFile(FILE *finalfp, const char *files, int path_offset) ATTR_NONNULL_ONE_TWO; /** @@ -230,7 +239,7 @@ int MergeAppendFile(FILE *finalfp, const char *files, int path_offset) __attribu * @param mode Indicates if the merged file must be readed as a binary file or not. Use `#OS_TEXT`, `#OS_BINARY`. * @return 1 if the file was unmerged, 0 on error. */ -int UnmergeFiles(const char *finalpath, const char *optdir, int mode, char ***unmerged_files) __attribute__((nonnull(1))); +int UnmergeFiles(const char *finalpath, const char *optdir, int mode, char ***unmerged_files) ATTR_NONNULL_ONE; /** @@ -240,7 +249,7 @@ int UnmergeFiles(const char *finalpath, const char *optdir, int mode, char ***un * @param mode Indicates if the merged file must be readed as a binary file or not. Use `#OS_TEXT`, `#OS_BINARY`. * @return 1 if the merged file is valid, 0 if not. */ -int TestUnmergeFiles(const char *finalpath, int mode) __attribute__((nonnull(1))); +int TestUnmergeFiles(const char *finalpath, int mode) ATTR_NONNULL_ONE; /** @@ -274,7 +283,7 @@ const char *getuname(void); * string such as "/usr/". * @return Pointer to the path basename. */ -char *basename_ex(char *path) __attribute__((nonnull)); +char *basename_ex(char *path) ATTR_NONNULL; /** @@ -284,7 +293,7 @@ char *basename_ex(char *path) __attribute__((nonnull)); * @param destination Path of the renamed file/folder. * @return 0 on success and -1 on error. */ -int rename_ex(const char *source, const char *destination) __attribute__((nonnull)); +int rename_ex(const char *source, const char *destination) ATTR_NONNULL; /** @@ -293,7 +302,7 @@ int rename_ex(const char *source, const char *destination) __attribute__((nonnul * @param tmp_path Temporary file path. * @return 0 on success and -1 on error. */ -int mkstemp_ex(char *tmp_path) __attribute__((nonnull)); +int mkstemp_ex(char *tmp_path) ATTR_NONNULL; /** @@ -638,4 +647,4 @@ int w_is_compressed_bz2_file(const char * path); * @return Pointer to the Wazuh installation path on success */ char *w_homedir(char *arg); -#endif /* FILE_OP_H */ + diff --git a/src/common/file_op/src/file_op.c b/src/common/file_op/src/file_op.c index 4b8e9e8aab..d0deb6c432 100644 --- a/src/common/file_op/src/file_op.c +++ b/src/common/file_op/src/file_op.c @@ -28,6 +28,23 @@ #include #else #include +typedef int DIR; +struct dirent { + unsigned short d_reclen; /* length of this record */ + unsigned char d_type; /* type of file; not supported + by all file system types */ + char d_name[256]; /* filename */ +}; +DIR * opendir(DIR *dir){ return NULL; } +int closedir(DIR *dir){ return 0; } +static char *dirname(char *s){ return NULL; } +static struct dirent *readdir(DIR *dir){ return NULL; } +static int64_t ftello64(FILE *x){ return 0; } +static int64_t fseeko64(FILE *x, int64_t pos, int mode){ return 0; } +static int64_t S_ISREG(int64_t flags){ return 0; } +static int64_t S_ISDIR(int64_t flags){ return 0; } +static char * PathFindFileNameA_(char *s){ return NULL; } +static void PathRemoveFileSpec_(char *path){} #endif /* Vista product information */ @@ -701,7 +718,11 @@ int UnmergeFiles(const char *finalpath, const char *optdir, int mode, char ***un free(copy); /* Create temporary file */ - char tmp_file[strlen(final_name) + 7]; + char *tmp_file = malloc(strlen(final_name) + 7); + if(!tmp_file){ + merror("Unmerging '%s': could not reserve temporary memory for '%s'", finalpath, files); + state_ok = 0; + } snprintf(tmp_file, sizeof(tmp_file), "%sXXXXXX", final_name); if (mkstemp_ex(tmp_file) == -1) { @@ -775,6 +796,7 @@ int UnmergeFiles(const char *finalpath, const char *optdir, int mode, char ***un os_strdup(file_name, *(*unmerged_files + file_count)); file_count++; } + free(tmp_file); } if (unmerged_files != NULL) { @@ -1226,7 +1248,7 @@ time_t get_UTC_modification_time(const char *file){ char *basename_ex(char *path) { - return (PathFindFileNameA(path)); + return (PathFindFileNameA_(path)); } @@ -1628,9 +1650,9 @@ const char *getuname() // Read Windows Version from registry DWORD dwRet; HKEY RegistryKey; - const DWORD size = 1024; - TCHAR value[size]; - DWORD dwCount = size; + #define VALUE_SIZE 1024 + TCHAR value[VALUE_SIZE]; + DWORD dwCount = VALUE_SIZE; add_infoEx = 0; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), 0, KEY_READ | KEY_WOW64_64KEY , &RegistryKey) != ERROR_SUCCESS) { @@ -1846,13 +1868,13 @@ const char *getuname() memset(__wp, '\0', 64); DWORD dwRet; HKEY RegistryKey; - const DWORD size = 30; - TCHAR winver[size]; - TCHAR wincomp[size]; + #define WIN_SIZE 30 + TCHAR winver[WIN_SIZE]; + TCHAR wincomp[WIN_SIZE]; DWORD winMajor = 0; DWORD winMinor = 0; DWORD buildRevision = 0; - DWORD dwCount = size; + DWORD dwCount = WIN_SIZE; unsigned long type=REG_DWORD; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), 0, KEY_READ | KEY_WOW64_64KEY, &RegistryKey) != ERROR_SUCCESS) { @@ -1862,20 +1884,20 @@ const char *getuname() // Windows 10 dwRet = RegQueryValueEx(RegistryKey, TEXT("CurrentMajorVersionNumber"), NULL, &type, (LPBYTE)&winMajor, &dwCount); if (dwRet == ERROR_SUCCESS) { - dwCount = size; + dwCount = WIN_SIZE; dwRet = RegQueryValueEx(RegistryKey, TEXT("CurrentMinorVersionNumber"), NULL, &type, (LPBYTE)&winMinor, &dwCount); if (dwRet != ERROR_SUCCESS) { merror("Error reading 'CurrentMinorVersionNumber' from Windows registry. (Error %u)",(unsigned int)dwRet); } else { - dwCount = size; + dwCount = WIN_SIZE; dwRet = RegQueryValueEx(RegistryKey, TEXT("CurrentBuildNumber"), NULL, NULL, (LPBYTE)wincomp, &dwCount); if (dwRet != ERROR_SUCCESS) { merror("Error reading 'CurrentBuildNumber' from Windows registry. (Error %u)",(unsigned int)dwRet); snprintf(__wp, 63, " [Ver: %d.%d]", (unsigned int)winMajor, (unsigned int)winMinor); } else { - dwCount = size; + dwCount = WIN_SIZE; dwRet = RegQueryValueEx(RegistryKey, TEXT("UBR"), NULL, &type, (LPBYTE)&buildRevision, &dwCount); if (dwRet != ERROR_SUCCESS) { snprintf(__wp, sizeof(__wp), " [Ver: %d.%d.%s]", (unsigned int)winMajor, (unsigned int)winMinor, wincomp); @@ -1904,14 +1926,14 @@ const char *getuname() snprintf(__wp, 63, " [Ver: 6.2]"); } else { - dwCount = size; + dwCount = WIN_SIZE; dwRet = RegQueryValueEx(RegistryKey, TEXT("CurrentBuildNumber"), NULL, NULL, (LPBYTE)wincomp, &dwCount); if (dwRet != ERROR_SUCCESS) { merror("Error reading 'CurrentBuildNumber' from Windows registry. (Error %u)",(unsigned int)dwRet); snprintf(__wp, 63, " [Ver: 6.2]"); } else { - dwCount = size; + dwCount = WIN_SIZE; dwRet = RegQueryValueEx(RegistryKey, TEXT("UBR"), NULL, &type, (LPBYTE)&buildRevision, &dwCount); if (dwRet != ERROR_SUCCESS) { snprintf(__wp, sizeof(__wp), " [Ver: %s.%s]", winver,wincomp); @@ -2004,7 +2026,7 @@ void w_ch_exec_dir() { } /* Remove file name from path */ - PathRemoveFileSpec(path); + PathRemoveFileSpec_(path); /* Move to correct directory */ if (chdir(path)) { diff --git a/src/common/mem_op/include/mem_op.h b/src/common/mem_op/include/mem_op.h index 07d87b526e..56e0fb2863 100644 --- a/src/common/mem_op/include/mem_op.h +++ b/src/common/mem_op/include/mem_op.h @@ -8,12 +8,16 @@ * Foundation */ -#ifndef MEM_H -#define MEM_H +#pragma once #include -#ifdef WIN32 +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_NONNULL __attribute__((nonnull)) +#define ATTR_NONNULL_TWO __attribute__((nonnull(2))) +#else +#define ATTR_NONNULL +#define ATTR_NONNULL_TWO #define win_alloc(x) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (x)) #define win_free(x) HeapFree(GetProcessHeap(), 0, (x)) #endif @@ -24,7 +28,6 @@ void **os_AddPtArray(void *pt, void **array); char **os_AddStrArray(const char *str, char **array); void os_FreeArray(char *ch1, char **ch2); int os_IsStrOnArray(const char *str, char **array); -char *os_LoadString(char *at, const char *str) __attribute__((nonnull(2))); -void *memset_secure(void *v, int c, size_t n) __attribute__((nonnull)); +char *os_LoadString(char *at, const char *str) ATTR_NONNULL_TWO; +void *memset_secure(void *v, int c, size_t n) ATTR_NONNULL; -#endif /* MEM_H */ diff --git a/src/common/networkHelper/include/networkHelper.h b/src/common/networkHelper/include/networkHelper.h index 9ca12b124f..4fe675302a 100644 --- a/src/common/networkHelper/include/networkHelper.h +++ b/src/common/networkHelper/include/networkHelper.h @@ -9,14 +9,12 @@ * Foundation. */ -#ifndef _NETWORK_HELPER_H -#define _NETWORK_HELPER_H +#pragma once #include #include #include #include -#include "makeUnique.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" @@ -83,4 +81,3 @@ namespace Utils #pragma GCC diagnostic pop -#endif // _NETWORK_HELPER_H \ No newline at end of file diff --git a/src/common/os_crypto/tests/unit/tests/sha1/test_sha1_op.c b/src/common/os_crypto/tests/unit/tests/sha1/test_sha1_op.c index 317a6b0461..1cc8f4ff29 100644 --- a/src/common/os_crypto/tests/unit/tests/sha1/test_sha1_op.c +++ b/src/common/os_crypto/tests/unit/tests/sha1/test_sha1_op.c @@ -51,7 +51,7 @@ void OS_SHA1_File_Nbytes_unable_open_file (void **state) const char *path = "/home/test_file"; EVP_MD_CTX *context = EVP_MD_CTX_new(); os_sha1 output; - ssize_t nbytes = 4096; + int64_t nbytes = 4096; int mode = OS_BINARY; diff --git a/src/common/os_net/include/os_net.h b/src/common/os_net/include/os_net.h index 1ed488874a..98a1e5f5ab 100644 --- a/src/common/os_net/include/os_net.h +++ b/src/common/os_net/include/os_net.h @@ -12,8 +12,19 @@ * APIs for many network operations */ -#ifndef OS_NET_H -#define OS_NET_H +#pragma once + +#include "cust_types.h" + +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_NONNULL __attribute__((nonnull)) +#define ATTR_UNUSED __attribute__((unused)) +#define UNREFERENCED_PARAMETER(P) +#else +#define ATTR_NONNULL +#define ATTR_UNUSED +#define UNREFERENCED_PARAMETER(P) (P) +#endif #define IPV6_LINK_LOCAL_PREFIX "FE80:0000:0000:0000:" @@ -31,9 +42,9 @@ int OS_Bindportudp(u_int16_t _port, const char *_ip, int ipv6); * Bind to a specific file, using the "mode" permissions in * a Unix Domain socket. */ -int OS_BindUnixDomain(const char *path, int type, int max_msg_size) __attribute__((nonnull)); -int OS_BindUnixDomainWithPerms(const char *path, int type, int max_msg_size, uid_t uid, gid_t gid, mode_t mode) __attribute__((nonnull)); -int OS_ConnectUnixDomain(const char *path, int type, int max_msg_size) __attribute__((nonnull)); +int OS_BindUnixDomain(const char *path, int type, int max_msg_size) ATTR_NONNULL; +int OS_BindUnixDomainWithPerms(const char *path, int type, int max_msg_size, uid_t uid, gid_t gid, mode_t mode) ATTR_NONNULL; +int OS_ConnectUnixDomain(const char *path, int type, int max_msg_size) ATTR_NONNULL; int OS_getsocketsize(int ossock); /* OS_Connect @@ -46,29 +57,29 @@ int OS_ConnectUDP(u_int16_t _port, const char *_ip, int ipv6, uint32_t network_i * Receive a UDP packet. Return NULL if failed */ char *OS_RecvUDP(int socket, int sizet); -int OS_RecvConnUDP(int socket, char *buffer, int buffer_size) __attribute__((nonnull)); +int OS_RecvConnUDP(int socket, char *buffer, int buffer_size) ATTR_NONNULL; /* OS_RecvUnix * Receive a message via a Unix socket */ -int OS_RecvUnix(int socket, int sizet, char *ret) __attribute__((nonnull)); +int OS_RecvUnix(int socket, int sizet, char *ret) ATTR_NONNULL; /* OS_RecvTCP * Receive a TCP packet */ -int OS_AcceptTCP(int socket, char *srcip, size_t addrsize) __attribute__((nonnull)); +int OS_AcceptTCP(int socket, char *srcip, size_t addrsize) ATTR_NONNULL; char *OS_RecvTCP(int socket, int sizet); -int OS_RecvTCPBuffer(int socket, char *buffer, int sizet) __attribute__((nonnull)); +int OS_RecvTCPBuffer(int socket, char *buffer, int sizet) ATTR_NONNULL; /* OS_SendTCP * Send a TCP/UDP/UnixSocket packet (in a open socket) */ -int OS_SendTCP(int socket, const char *msg) __attribute__((nonnull)); -int OS_SendTCPbySize(int socket, int size, const char *msg) __attribute__((nonnull)); +int OS_SendTCP(int socket, const char *msg) ATTR_NONNULL; +int OS_SendTCPbySize(int socket, int size, const char *msg) ATTR_NONNULL; -int OS_SendUnix(int socket, const char *msg, int size) __attribute__((nonnull)); +int OS_SendUnix(int socket, const char *msg, int size) ATTR_NONNULL; -int OS_SendUDPbySize(int socket, int size, const char *msg) __attribute__((nonnull)); +int OS_SendUDPbySize(int socket, int size, const char *msg) ATTR_NONNULL; /* * OS_GetHost retrieves the IP of a host @@ -100,7 +111,7 @@ int OS_SetKeepalive(int socket); * @param intvl Interval between probes, in seconds. * @param cnt Number of probes sent before closing the connection. */ -void OS_SetKeepalive_Options(__attribute__((unused)) int socket, int idle, int intvl, int cnt); +void OS_SetKeepalive_Options(ATTR_UNUSED int socket, int idle, int intvl, int cnt); /* Set the delivery timeout for a socket * Returns 0 on success, else -1 @@ -159,7 +170,7 @@ int OS_SetSocketSize(int sock, int mode, int max_msg_size); * Returns -1 on socket error. * Returns 0 on socket disconnected or timeout. */ -ssize_t os_recv_waitall(int sock, void * buf, size_t size); +int64_t os_recv_waitall(int sock, void * buf, size_t size); // Wrapper for select() int wnet_select(int sock, int timeout); @@ -221,4 +232,3 @@ int get_ipv4_string(struct in_addr addr, char *address, size_t address_size); * */ int get_ipv6_string(struct in6_addr addr6, char *address, size_t address_size); -#endif /* OS_NET_H */ diff --git a/src/common/os_net/src/os_net.c b/src/common/os_net/src/os_net.c index 25d26dcd20..e6581f010e 100644 --- a/src/common/os_net/src/os_net.c +++ b/src/common/os_net/src/os_net.c @@ -664,9 +664,9 @@ int OS_SendSecureTCP(int sock, uint32_t size, const void * msg) { os_malloc(bufsz, buffer); *(uint32_t *)buffer = wnet_order(size); - memcpy(buffer + sizeof(uint32_t), msg, size); + memcpy((void *)((unsigned char *)buffer + sizeof(uint32_t)), msg, size); errno = 0; - retval = send(sock, buffer, bufsz, 0) == (ssize_t)bufsz ? 0 : OS_SOCKTERR; + retval = send(sock, buffer, bufsz, 0) == (int64_t)bufsz ? 0 : OS_SOCKTERR; free(buffer); return retval; } @@ -677,7 +677,7 @@ int OS_SendSecureTCP(int sock, uint32_t size, const void * msg) { * Return recvval on success or OS_SOCKTERR on error. */ int OS_RecvSecureTCP(int sock, char * ret, uint32_t size) { - ssize_t recvval, recvb; + int64_t recvval, recvb; uint32_t msgsize; /* Get header */ @@ -810,7 +810,7 @@ int OS_SendSecureTCPCluster(int sock, const void * command, const void * payload memset(buffer + HEADER_SIZE + cmd_length + 1, '-', COMMAND_SIZE - cmd_length - 1); memcpy(buffer + HEADER_SIZE + COMMAND_SIZE, payload, length); - retval = send(sock, buffer, buffer_size, 0) == (ssize_t)buffer_size ? 0 : OS_SOCKTERR; + retval = send(sock, buffer, buffer_size, 0) == (int64_t)buffer_size ? 0 : OS_SOCKTERR; free(buffer); return retval; @@ -820,8 +820,8 @@ int OS_SendSecureTCPCluster(int sock, const void * command, const void * payload /* Receive secure TCP Cluster message */ int OS_RecvSecureClusterTCP(int sock, char * ret, size_t length) { int recvval; - const unsigned CMD_SIZE = 12; - const uint32_t HEADER_SIZE = 8 + CMD_SIZE; + #define CMD_SIZE 12 + #define HEADER_SIZE (8 + CMD_SIZE) uint32_t size = 0; char buffer[HEADER_SIZE]; @@ -861,12 +861,12 @@ int OS_RecvSecureClusterTCP(int sock, char * ret, size_t length) { * Returns -1 on socket error. * Returns 0 on socket disconnected or timeout. */ -ssize_t os_recv_waitall(int sock, void * buf, size_t size) { +int64_t os_recv_waitall(int sock, void * buf, size_t size) { size_t offset; - ssize_t recvb; + int64_t recvb; for (offset = 0; offset < size; offset += recvb) { - recvb = recv(sock, buf + offset, size - offset, 0); + recvb = recv(sock, (void *)((unsigned char *)buf + offset), size - offset, 0); if (recvb <= 0) { return recvb; diff --git a/src/common/os_regex/include/os_regex.h b/src/common/os_regex/include/os_regex.h index 87f1e039b9..0733dd6bd7 100644 --- a/src/common/os_regex/include/os_regex.h +++ b/src/common/os_regex/include/os_regex.h @@ -16,8 +16,26 @@ /* size_t */ #include #include + +#ifdef WIN32 +typedef void * pthread_mutex_t; +#ifndef ATTR_NONNULL_TWO +#define ATTR_NONNULL_TWO +#endif +#ifndef ATTR_NONNULL +#define ATTR_NONNULL +#endif +#else #include +#ifndef ATTR_NONNULL_TWO +#define ATTR_NONNULL_TWO __attribute__((nonnull(2))) +#endif +#ifndef ATTR_NONNULL +#define ATTR_NONNULL __attribute__((nonnull)) +#endif +#endif + /* OSRegex_Compile flags */ #define OS_RETURN_SUBSTRING 0000200 #define OS_CASE_SENSITIVE 0000400 @@ -93,7 +111,7 @@ int OSRegex_Compile(const char *pattern, OSRegex *reg, int flags); * @param reg compiled pattern * @return Returns the end of the string on success or NULL otherwise */ -const char *OSRegex_Execute(const char *str, OSRegex *reg) __attribute__((nonnull(2))); +const char *OSRegex_Execute(const char *str, OSRegex *reg) ATTR_NONNULL_TWO; /** * @brief Compares an already compiled OSRegex regular expression with a string @@ -110,7 +128,7 @@ const char *OSRegex_Execute(const char *str, OSRegex *reg) __attribute__((nonnul * @param regex_match Structure to manage pattern matches * @return Returns the end of the string on success or NULL otherwise */ - const char *OSRegex_Execute_ex(const char *str, OSRegex *reg, regex_matching *regex_match) __attribute__((nonnull(2))); + const char *OSRegex_Execute_ex(const char *str, OSRegex *reg, regex_matching *regex_match) ATTR_NONNULL_TWO; /* Release all the memory created by the compilation/execution phases */ void OSRegex_FreePattern(OSRegex *reg); @@ -139,10 +157,10 @@ int OSMatch_Execute(const char *str, size_t str_len, OSMatch *reg); /* Release all the memory created by the compilation/execution phases */ void OSMatch_FreePattern(OSMatch *reg); -int OS_Match2(const char *pattern, const char *str) __attribute__((nonnull(2))); +int OS_Match2(const char *pattern, const char *str) ATTR_NONNULL_TWO; /* Searches for pattern in the string */ -int OS_WordMatch(const char *pattern, const char *str) __attribute__((nonnull)); +int OS_WordMatch(const char *pattern, const char *str) ATTR_NONNULL; #define OS_Match OS_WordMatch /* Split a string into multiples pieces, divided by a char "match". @@ -160,7 +178,7 @@ size_t OS_StrHowClosedMatch(const char *str1, const char *str2); /* Verifies if a string starts with the provided pattern. * Returns 1 on success or 0 on failure. */ -int OS_StrStartsWith(const char *str, const char *pattern) __attribute__((nonnull)); +int OS_StrStartsWith(const char *str, const char *pattern) ATTR_NONNULL; /* Checks if a specific string is numeric (like "129544") */ int OS_StrIsNum(const char *str); diff --git a/src/common/os_regex/include/os_regex_internal.h b/src/common/os_regex/include/os_regex_internal.h index 5dd972e32b..faa557fd9c 100644 --- a/src/common/os_regex/include/os_regex_internal.h +++ b/src/common/os_regex/include/os_regex_internal.h @@ -8,15 +8,20 @@ * Foundation. */ -#ifndef OS_INTERNAL_H -#define OS_INTERNAL_H +#pragma once + +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_NONNULL __attribute__((nonnull)) +#else +#define ATTR_NONNULL +#endif /* Prototype for the _OsMatch */ -int _OS_Match(const char *pattern, const char *str, size_t str_len, size_t size) __attribute__((nonnull)); -int _os_strncmp(const char *pattern, const char *str, size_t str_len, size_t size) __attribute__((nonnull)); -int _os_strcmp_last(const char *pattern, const char *str, size_t str_len, size_t size) __attribute__((nonnull)); -int _os_strcmp(const char *pattern, const char *str, size_t str_len, size_t size) __attribute__((nonnull)); -int _os_strmatch(const char *pattern, const char *str, size_t str_len, size_t size) __attribute__((nonnull)); +int _OS_Match(const char *pattern, const char *str, size_t str_len, size_t size) ATTR_NONNULL; +int _os_strncmp(const char *pattern, const char *str, size_t str_len, size_t size) ATTR_NONNULL; +int _os_strcmp_last(const char *pattern, const char *str, size_t str_len, size_t size) ATTR_NONNULL; +int _os_strcmp(const char *pattern, const char *str, size_t str_len, size_t size) ATTR_NONNULL; +int _os_strmatch(const char *pattern, const char *str, size_t str_len, size_t size) ATTR_NONNULL; #define BACKSLASH '\\' #define ENDSTR '\0' @@ -109,4 +114,3 @@ extern const uchar charmap[256]; */ extern const uchar regexmap[][256]; -#endif /* OS_INTERNAL_H */ diff --git a/src/common/os_regex/src/os_match_execute.c b/src/common/os_regex/src/os_match_execute.c index dd1efceade..77fba54511 100644 --- a/src/common/os_regex/src/os_match_execute.c +++ b/src/common/os_regex/src/os_match_execute.c @@ -15,6 +15,15 @@ #include "os_regex.h" #include "os_regex_internal.h" +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_NONNULL __attribute__((nonnull)) +#define UNREFERENCED_PARAMETER(P) +#define ATTR_UNUSED __attribute__((unused)) +#else +#define UNREFERENCED_PARAMETER(P) (P) +#define ATTR_UNUSED +#define ATTR_NONNULL +#endif int _OS_Match(const char *pattern, const char *str, size_t str_len, size_t size) { @@ -55,8 +64,9 @@ int _OS_Match(const char *pattern, const char *str, size_t str_len, size_t size) return (FALSE); } -int _os_strncmp(const char *pattern, const char *str, __attribute__((unused)) size_t str_len, size_t size) +int _os_strncmp(const char *pattern, const char *str, ATTR_UNUSED size_t str_len, size_t size) { + UNREFERENCED_PARAMETER(str_len); if (strncasecmp(pattern, str, size) == 0) { return (TRUE); } @@ -64,8 +74,9 @@ int _os_strncmp(const char *pattern, const char *str, __attribute__((unused)) si return (FALSE); } -int _os_strcmp(const char *pattern, const char *str, __attribute__((unused)) size_t str_len, __attribute__((unused)) size_t size) +int _os_strcmp(const char *pattern, const char *str, ATTR_UNUSED size_t str_len, ATTR_UNUSED size_t size) { + UNREFERENCED_PARAMETER(str_len); if (strcasecmp(pattern, str) == 0) { return (TRUE); } @@ -73,9 +84,13 @@ int _os_strcmp(const char *pattern, const char *str, __attribute__((unused)) siz return (FALSE); } -int _os_strmatch(__attribute__((unused)) const char *pattern, __attribute__((unused)) const char *str, - __attribute__((unused)) size_t str_len, __attribute__((unused)) size_t size) +int _os_strmatch(ATTR_UNUSED const char *pattern, ATTR_UNUSED const char *str, + ATTR_UNUSED size_t str_len, ATTR_UNUSED size_t size) { + UNREFERENCED_PARAMETER(pattern); + UNREFERENCED_PARAMETER(str); + UNREFERENCED_PARAMETER(str_len); + UNREFERENCED_PARAMETER(size); return (TRUE); } diff --git a/src/common/os_regex/src/os_regex_compile.c b/src/common/os_regex/src/os_regex_compile.c index 92bf2bc525..dad924b765 100644 --- a/src/common/os_regex/src/os_regex_compile.c +++ b/src/common/os_regex/src/os_regex_compile.c @@ -13,10 +13,15 @@ #include #include -#include #include "shared.h" #include "os_regex_internal.h" +#if defined(__GNUC__) || defined(__clang__) +#include +#else + +#endif + /* Compile a regular expression to be used later * Allowed flags are: diff --git a/src/common/os_regex/src/os_regex_execute.c b/src/common/os_regex/src/os_regex_execute.c index f69524ce1d..57fa325688 100644 --- a/src/common/os_regex/src/os_regex_execute.c +++ b/src/common/os_regex/src/os_regex_execute.c @@ -50,7 +50,7 @@ const char *OSRegex_Execute_ex(const char *str, OSRegex *reg, regex_matching *re os_calloc(1, reg->d_size.sub_strings_size, *sub_strings); } else { os_realloc(*sub_strings, reg->d_size.sub_strings_size, *sub_strings); - memset((void*)*sub_strings + str_sizes->sub_strings_size, 0, reg->d_size.sub_strings_size - str_sizes->sub_strings_size); + memset((void*)((unsigned char*)*sub_strings + str_sizes->sub_strings_size), 0, reg->d_size.sub_strings_size - str_sizes->sub_strings_size); } str_sizes->sub_strings_size = reg->d_size.sub_strings_size; } @@ -60,13 +60,13 @@ const char *OSRegex_Execute_ex(const char *str, OSRegex *reg, regex_matching *re if (external_context && prts_str) { if (str_sizes->prts_str_alloc_size < reg->d_size.prts_str_alloc_size) { os_realloc(*prts_str, reg->d_size.prts_str_alloc_size, *prts_str); - memset((void *) *prts_str + str_sizes->prts_str_alloc_size, 0, + memset((void *)((unsigned char *)*prts_str + str_sizes->prts_str_alloc_size), 0, reg->d_size.prts_str_alloc_size - str_sizes->prts_str_alloc_size); if (!str_sizes->prts_str_size) { os_calloc(1, reg->d_size.prts_str_alloc_size, str_sizes->prts_str_size); } else { os_realloc(str_sizes->prts_str_size, reg->d_size.prts_str_alloc_size, str_sizes->prts_str_size); - memset((void *) str_sizes->prts_str_size + str_sizes->prts_str_alloc_size, 0, + memset((void *)((unsigned char *)str_sizes->prts_str_size + str_sizes->prts_str_alloc_size), 0, reg->d_size.prts_str_alloc_size - str_sizes->prts_str_alloc_size); } str_sizes->prts_str_alloc_size = reg->d_size.prts_str_alloc_size; @@ -80,7 +80,7 @@ const char *OSRegex_Execute_ex(const char *str, OSRegex *reg, regex_matching *re str_sizes->prts_str_size[i] = reg->d_size.prts_str_size[i]; } else if (str_sizes->prts_str_size[i] < reg->d_size.prts_str_size[i]) { os_realloc((*prts_str)[i], reg->d_size.prts_str_size[i], (*prts_str)[i]); - memset((void*)(*prts_str)[i] + str_sizes->prts_str_size[i], 0, reg->d_size.prts_str_size[i] - str_sizes->prts_str_size[i]); + memset((void*)((unsigned char *)(*prts_str)[i] + str_sizes->prts_str_size[i]), 0, reg->d_size.prts_str_size[i] - str_sizes->prts_str_size[i]); str_sizes->prts_str_size[i] = reg->d_size.prts_str_size[i]; } } diff --git a/src/common/os_xml/include/os_xml.h b/src/common/os_xml/include/os_xml.h index 3c7bb77bde..833739a57c 100644 --- a/src/common/os_xml/include/os_xml.h +++ b/src/common/os_xml/include/os_xml.h @@ -10,12 +10,31 @@ /* os_xml C Library */ -#ifndef OS_XML_H -#define OS_XML_H +#pragma once #include #include +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_NONNULL __attribute__((nonnull)) +#define ATTR_NONNULL_ONE __attribute__((nonnull(1))) +#define ATTR_NONNULL_TWO __attribute__((nonnull(2))) +#define ATTR_NONNULL_ONE_TWO __attribute__((nonnull(1, 2))) +#define ATTR_NONNULL_ONE_TWO_THREE_FIVE __attribute__((nonnull(1, 2, 3, 5))) +#define ATTR_PRINTF_TWO_THREE_NONNULL __attribute__((format(printf, 2, 3), nonnull)) +#define ATTR_UNUSED __attribute__((unused)) +#define UNREFERENCED_PARAMETER(P) +#else +#define ATTR_NONNULL +#define ATTR_NONNULL_ONE +#define ATTR_NONNULL_TWO +#define ATTR_NONNULL_ONE_TWO +#define ATTR_NONNULL_ONE_TWO_THREE_FIVE +#define ATTR_PRINTF_TWO_THREE_NONNULL +#define ATTR_UNUSED +#define UNREFERENCED_PARAMETER(P) (P) +#endif + /* XML Node structure */ typedef struct _xml_node { unsigned int key; @@ -59,7 +78,7 @@ typedef xml_node **XML_NODE; * @param lxml The struct to store the result. * @return int OS_SUCCESS on success, OS_INVALID otherwise. */ -int OS_ReadXML(const char *file, OS_XML *lxml) __attribute__((nonnull)); +int OS_ReadXML(const char *file, OS_XML *lxml) ATTR_NONNULL; /** * @brief Parses a XML file and stores the content in the OS_XML struct. @@ -70,7 +89,7 @@ int OS_ReadXML(const char *file, OS_XML *lxml) __attribute__((nonnull)); * truncate on TRUE or fail on FALSE. * @return int OS_SUCCESS on success, OS_INVALID otherwise. */ -int OS_ReadXML_Ex(const char *file, OS_XML *_lxml, bool flag_truncate) __attribute__((nonnull)); +int OS_ReadXML_Ex(const char *file, OS_XML *_lxml, bool flag_truncate) ATTR_NONNULL; /** * @brief Parses a XML string and stores the content in the OS_XML struct. @@ -80,7 +99,7 @@ int OS_ReadXML_Ex(const char *file, OS_XML *_lxml, bool flag_truncate) __attribu * @param lxml The struct to store the result. * @return int OS_SUCCESS on success, OS_INVALID otherwise. */ -int OS_ReadXMLString(const char *string, OS_XML *_lxml) __attribute__((nonnull)); +int OS_ReadXMLString(const char *string, OS_XML *_lxml) ATTR_NONNULL; /** * @brief Parses a XML string and stores the content in the OS_XML struct. @@ -91,7 +110,7 @@ int OS_ReadXMLString(const char *string, OS_XML *_lxml) __attribute__((nonnull)) * truncate on TRUE or fail on FALSE. * @return int OS_SUCCESS on success, OS_INVALID otherwise. */ -int OS_ReadXMLString_Ex(const char *string, OS_XML *_lxml, bool flag_truncate) __attribute__((nonnull)); +int OS_ReadXMLString_Ex(const char *string, OS_XML *_lxml, bool flag_truncate) ATTR_NONNULL; /** * @brief Parses a XML string or file. @@ -101,10 +120,10 @@ int OS_ReadXMLString_Ex(const char *string, OS_XML *_lxml, bool flag_truncate) _ * truncate on TRUE or fail on FALSE. * @return int OS_SUCCESS on success, OS_INVALID otherwise. */ -int ParseXML(OS_XML *_lxml, bool flag_truncate) __attribute__((nonnull)); +int ParseXML(OS_XML *_lxml, bool flag_truncate) ATTR_NONNULL; /* Clear the XML structure memory */ -void OS_ClearXML(OS_XML *_lxml) __attribute__((nonnull)); +void OS_ClearXML(OS_XML *_lxml) ATTR_NONNULL; /* Clear a node */ void OS_ClearNode(xml_node **node); @@ -113,35 +132,35 @@ void OS_ClearNode(xml_node **node); /* Functions to read the XML */ /* Return 1 if element_name is a root element */ -unsigned int OS_RootElementExist(const OS_XML *_lxml, const char *element_name) __attribute__((nonnull)); +unsigned int OS_RootElementExist(const OS_XML *_lxml, const char *element_name) ATTR_NONNULL; /* Return 1 if the element_name exists */ -unsigned int OS_ElementExist(const OS_XML *_lxml, const char **element_name) __attribute__((nonnull)); +unsigned int OS_ElementExist(const OS_XML *_lxml, const char **element_name) ATTR_NONNULL; /* Return the elements "children" of the element_name */ -char **OS_GetElements(const OS_XML *_lxml, const char **element_name) __attribute__((nonnull(1))); +char **OS_GetElements(const OS_XML *_lxml, const char **element_name) ATTR_NONNULL_ONE; /* Return the elements "children" of the element_name */ -xml_node **OS_GetElementsbyNode(const OS_XML *_lxml, const xml_node *node) __attribute__((nonnull(1))); +xml_node **OS_GetElementsbyNode(const OS_XML *_lxml, const xml_node *node) ATTR_NONNULL_ONE; /* Return the attributes of the element name */ -char **OS_GetAttributes(const OS_XML *_lxml, const char **element_name) __attribute__((nonnull(1))); +char **OS_GetAttributes(const OS_XML *_lxml, const char **element_name) ATTR_NONNULL_ONE; /* Return one value from element_name */ -char *OS_GetOneContentforElement(OS_XML *_lxml, const char **element_name) __attribute__((nonnull)); +char *OS_GetOneContentforElement(OS_XML *_lxml, const char **element_name) ATTR_NONNULL; /* Return an array with the content of all entries of element_name */ -char **OS_GetElementContent(OS_XML *_lxml, const char **element_name) __attribute__((nonnull)); +char **OS_GetElementContent(OS_XML *_lxml, const char **element_name) ATTR_NONNULL; /* Return an array with the contents of an element_nane */ -char **OS_GetContents(OS_XML *_lxml, const char **element_name) __attribute__((nonnull(1))); +char **OS_GetContents(OS_XML *_lxml, const char **element_name) ATTR_NONNULL_ONE; /* Return the value of a specific attribute of the element_name */ char *OS_GetAttributeContent(OS_XML *_lxml, const char **element_name, - const char *attribute_name) __attribute__((nonnull(1, 2))); + const char *attribute_name) ATTR_NONNULL_ONE_TWO; /* Apply the variables to the xml */ -int OS_ApplyVariables(OS_XML *_lxml) __attribute__((nonnull)); +int OS_ApplyVariables(OS_XML *_lxml) ATTR_NONNULL; /* Error from writer */ #define XMLW_ERROR 006 @@ -150,7 +169,7 @@ int OS_ApplyVariables(OS_XML *_lxml) __attribute__((nonnull)); /* Write an XML file, based on the input and values to change */ int OS_WriteXML(const char *infile, const char *outfile, const char **nodes, - const char *oldval, const char *newval) __attribute__((nonnull(1, 2, 3, 5))); + const char *oldval, const char *newval) ATTR_NONNULL_ONE_TWO_THREE_FIVE; /** * @brief Get value of an attribute of a node @@ -160,4 +179,3 @@ int OS_WriteXML(const char *infile, const char *outfile, const char **nodes, */ const char * w_get_attr_val_by_name(xml_node * node, const char * name); -#endif /* OS_XML_H */ diff --git a/src/common/os_xml/src/os_xml.c b/src/common/os_xml/src/os_xml.c index 04b372f6fc..ca2da363b9 100644 --- a/src/common/os_xml/src/os_xml.c +++ b/src/common/os_xml/src/os_xml.c @@ -20,15 +20,19 @@ #include "os_xml_internal.h" #include "file_op.h" +#ifdef WIN32 +static int strcasecmp(char *s1, char *s2){} +#endif + /* Prototypes */ -static int _oscomment(OS_XML *_lxml) __attribute__((nonnull)); -static int _writecontent(const char *str, __attribute__((unused)) size_t size, unsigned int parent, OS_XML *_lxml) __attribute__((nonnull)); +static int _oscomment(OS_XML *_lxml) ATTR_NONNULL; +static int _writecontent(const char *str, ATTR_UNUSED size_t size, unsigned int parent, OS_XML *_lxml) ATTR_NONNULL; static int _writememory(const char *str, XML_TYPE type, size_t size, - unsigned int parent, OS_XML *_lxml) __attribute__((nonnull)); -static int _xml_fgetc(FILE *fp, OS_XML *_lxml) __attribute__((nonnull)); -int _xml_sgetc(OS_XML *_lxml) __attribute__((nonnull)); -static int _getattributes(unsigned int parent, OS_XML *_lxml, bool flag_truncate) __attribute__((nonnull)); -static void xml_error(OS_XML *_lxml, const char *msg, ...) __attribute__((format(printf, 2, 3), nonnull)); + unsigned int parent, OS_XML *_lxml) ATTR_NONNULL; +static int _xml_fgetc(FILE *fp, OS_XML *_lxml) ATTR_NONNULL; +int _xml_sgetc(OS_XML *_lxml) ATTR_NONNULL; +static int _getattributes(unsigned int parent, OS_XML *_lxml, bool flag_truncate) ATTR_NONNULL; +static void xml_error(OS_XML *_lxml, const char *msg, ...) ATTR_PRINTF_TWO_THREE_NONNULL; /** * @brief Recursive method to read XML elements. @@ -39,7 +43,7 @@ static void xml_error(OS_XML *_lxml, const char *msg, ...) __attribute__((format * @param flag_truncate If TRUE, truncates the content of a tag when it's bigger than XML_MAXSIZE. Fails if set to FALSE. * @return int Returns 0, -1 or -2. */ -static int _ReadElem(unsigned int parent, OS_XML *_lxml, unsigned int recursion_level, bool flag_truncate) __attribute__((nonnull)); +static int _ReadElem(unsigned int parent, OS_XML *_lxml, unsigned int recursion_level, bool flag_truncate) ATTR_NONNULL; /* Local fgetc */ static int _xml_fgetc(FILE *fp, OS_XML *_lxml) @@ -544,8 +548,9 @@ static int _writememory(const char *str, XML_TYPE type, size_t size, return (-1); } -static int _writecontent(const char *str, __attribute__((unused)) size_t size, unsigned int parent, OS_XML *_lxml) +static int _writecontent(const char *str, ATTR_UNUSED size_t size, unsigned int parent, OS_XML *_lxml) { + UNREFERENCED_PARAMETER(size); _lxml->ct[parent] = strdup(str); if ( _lxml->ct[parent] == NULL) { diff --git a/src/common/os_xml/src/os_xml_access.c b/src/common/os_xml/src/os_xml_access.c index 8d92e7acfb..ca7a2bbd62 100644 --- a/src/common/os_xml/src/os_xml_access.c +++ b/src/common/os_xml/src/os_xml_access.c @@ -16,8 +16,8 @@ #include "os_xml_internal.h" /* Prototypes */ -static char **_GetElements(const OS_XML *_lxml, const char **element_name, XML_TYPE type) __attribute__((nonnull(1))); -static char **_GetElementContent(OS_XML *_lxml, const char **element_name, const char *attr) __attribute__((nonnull(1, 2))); +static char **_GetElements(const OS_XML *_lxml, const char **element_name, XML_TYPE type) ATTR_NONNULL_ONE; +static char **_GetElementContent(OS_XML *_lxml, const char **element_name, const char *attr) ATTR_NONNULL_ONE_TWO; /* Check if a element exists diff --git a/src/common/os_xml/src/os_xml_writer.c b/src/common/os_xml/src/os_xml_writer.c index 28de3f1354..d19d9818ef 100644 --- a/src/common/os_xml/src/os_xml_writer.c +++ b/src/common/os_xml/src/os_xml_writer.c @@ -16,10 +16,10 @@ #include "os_xml_internal.h" /* Prototypes */ -static int _oswcomment(FILE *fp_in, FILE *fp_out) __attribute__((nonnull)); +static int _oswcomment(FILE *fp_in, FILE *fp_out) ATTR_NONNULL; static int _WReadElem(FILE *fp_in, FILE *fp_out, unsigned int position, unsigned int parent, - const char **node, const char *value, unsigned int node_pos) __attribute__((nonnull)); -static int _xml_wfgetc(FILE *fp_in, FILE *fp_out) __attribute__((nonnull)); + const char **node, const char *value, unsigned int node_pos) ATTR_NONNULL; +static int _xml_wfgetc(FILE *fp_in, FILE *fp_out) ATTR_NONNULL; /* Local wfgetc */ diff --git a/src/common/privsep_op/include/privsep_op.h b/src/common/privsep_op/include/privsep_op.h index 57af0d7774..2999bf92a8 100644 --- a/src/common/privsep_op/include/privsep_op.h +++ b/src/common/privsep_op/include/privsep_op.h @@ -13,15 +13,10 @@ #ifndef PRIV_H #define PRIV_H +#include "cust_types.h" #include "shared.h" -#if defined(SUN_MAJOR_VERSION) && defined(SUN_MINOR_VERSION) && \ - (SUN_MAJOR_VERSION < 11) || \ - ((SUN_MAJOR_VERSION == 11) && (SUN_MINOR_VERSION < 4)) -#define w_ctime(x,y,z) ctime_r(x,y,z) -#else #define w_ctime(x,y,z) ctime_r(x,y) -#endif /** * @brief Find a user by name diff --git a/src/common/sqliteWrapper/include/sqliteWrapperTemp.h b/src/common/sqliteWrapper/include/sqliteWrapperTemp.h index 7dc4bf617b..b3148933f6 100644 --- a/src/common/sqliteWrapper/include/sqliteWrapperTemp.h +++ b/src/common/sqliteWrapper/include/sqliteWrapperTemp.h @@ -23,6 +23,12 @@ #include #include +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_RET_NONNULL __attribute__((__returns_nonnull__)) +#else +#define ATTR_RET_NONNULL +#endif + using DBSyncExceptionType = const std::pair; DBSyncExceptionType FACTORY_INSTANTATION { std::make_pair(1, "Unspecified type during factory instantiation") }; @@ -56,7 +62,7 @@ namespace DbSync class dbsync_error : public std::exception { public: - __attribute__((__returns_nonnull__)) + ATTR_RET_NONNULL const char* what() const noexcept override { return m_error.what(); @@ -90,7 +96,7 @@ namespace DbSync class max_rows_error : public std::exception { public: - __attribute__((__returns_nonnull__)) + ATTR_RET_NONNULL const char* what() const noexcept override { return m_error.what(); diff --git a/src/common/string_op/include/string_op.h b/src/common/string_op/include/string_op.h index 2f233da7e1..cf5b2a90b0 100644 --- a/src/common/string_op/include/string_op.h +++ b/src/common/string_op/include/string_op.h @@ -22,8 +22,27 @@ #define WC_ERR_INVALID_CHARS 0x80 #endif +#ifndef ATTR_NONNULL_FIRST +#define ATTR_NONNULL_FIRST +#endif + +#ifndef ATTR_RET_NONNULL +#define ATTR_RET_NONNULL +#endif + /* converts a Windows format string to char* */ char *convert_windows_string(LPCWSTR string); + +#else + +#ifndef ATTR_NONNULL_FIRST +#define ATTR_NONNULL_FIRST __attribute__((nonnull(1))) +#endif + +#ifndef ATTR_RET_NONNULL +#define ATTR_RET_NONNULL __attribute__((__returns_nonnull__)) +#endif + #endif // Time values for conversion @@ -70,10 +89,10 @@ char *convert_windows_string(LPCWSTR string); void os_trimcrlf(char *str); /* Similiar to Perl's substr() function */ -int os_substr(char *dest, const char *src, size_t position, ssize_t length) __attribute__((nonnull(1))); +int os_substr(char *dest, const char *src, size_t position, int64_t length) ATTR_NONNULL_FIRST; /* Remove a character from a string */ -char *os_strip_char(const char *source, char remove) __attribute__((nonnull)); +char *os_strip_char(const char *source, char remove) ATTR_RET_NONNULL; /* Escape a list of characters with a backslash */ char *os_shell_escape(const char *src); @@ -122,7 +141,7 @@ char * wstr_chr_escape(const char * str, char character, char escape); * @param match The value to escape. * @return The size of the dststr if success, or OS_INVALID if fail. */ -ssize_t wstr_escape(char *dststr, size_t dst_size, const char *str, char escape, char match); +int64_t wstr_escape(char *dststr, size_t dst_size, const char *str, char escape, char match); /** * @brief Unescape a specific character from a character string. @@ -133,7 +152,7 @@ ssize_t wstr_escape(char *dststr, size_t dst_size, const char *str, char escape, * @param escape The character used to unescape. * @return The size of the dststr if success, or OS_INVALID if fail. */ -ssize_t wstr_unescape(char *dststr, size_t dst_size, const char *str, char escape); +int64_t wstr_unescape(char *dststr, size_t dst_size, const char *str, char escape); // Free string array void free_strarray(char ** array); @@ -225,7 +244,7 @@ long w_parse_time(const char * string); * @return Size represented in bytes. * @retval -1 Cannot parse string, or value is negative. */ -ssize_t w_parse_size(const char * string); +int64_t w_parse_size(const char * string); /** * @brief Convert seconds into the greater valid time unit (s|m|h|d|w). diff --git a/src/common/string_op/src/string_op.c b/src/common/string_op/src/string_op.c index 6397f8d8a8..a031d8d412 100644 --- a/src/common/string_op/src/string_op.c +++ b/src/common/string_op/src/string_op.c @@ -17,6 +17,7 @@ #ifdef EVENTCHANNEL_SUPPORT #define _WIN32_WINNT 0x0600 #endif +char *strtok_r(char *str, const char *delim, char **saveptr){return NULL; } #endif /* Trim CR and/or LF from the last positions of a string */ @@ -79,7 +80,7 @@ char *os_strip_char(const char *source, char remove) } /* Do a substring */ -int os_substr(char *dest, const char *src, size_t position, ssize_t length) +int os_substr(char *dest, const char *src, size_t position, int64_t length) { dest[0] = '\0'; @@ -334,7 +335,7 @@ char * wstr_chr_escape(const char * str, char character, char escape) { // Escape a specific character from a character string -ssize_t wstr_escape(char *dststr, size_t dst_size, const char *str, char escape, char match) { +int64_t wstr_escape(char *dststr, size_t dst_size, const char *str, char escape, char match) { if (str == NULL || dststr == NULL) { return OS_INVALID; @@ -376,7 +377,7 @@ ssize_t wstr_escape(char *dststr, size_t dst_size, const char *str, char escape, // Unescape a specific character from a character string -ssize_t wstr_unescape(char *dststr, size_t dst_size, const char *str, char escape) { +int64_t wstr_unescape(char *dststr, size_t dst_size, const char *str, char escape) { if (str == NULL || dststr == NULL) { return OS_INVALID; @@ -765,9 +766,9 @@ long w_parse_time(const char * string) { // Parse positive size string into bytes -ssize_t w_parse_size(const char * string) { +int64_t w_parse_size(const char * string) { char c; - ssize_t size; + int64_t size; switch (sscanf(string, "%zd%c", &size, &c)) { case 1: diff --git a/src/common/time_op/include/time_op.h b/src/common/time_op/include/time_op.h index 35fa3bcc28..c69af60cea 100644 --- a/src/common/time_op/include/time_op.h +++ b/src/common/time_op/include/time_op.h @@ -13,8 +13,7 @@ * @date October 4, 2017 */ -#ifndef TIME_OP_H -#define TIME_OP_H +#pragma once #include @@ -87,5 +86,3 @@ long long int get_windows_file_time_epoch(FILETIME ft); * @return Boolean indicating whether the year is leap. */ bool is_leap_year(int year); - -#endif // TIME_OP_H diff --git a/src/common/time_op/src/time_op.c b/src/common/time_op/src/time_op.c index e8e9e00ed4..ed9ae6c5c9 100644 --- a/src/common/time_op/src/time_op.c +++ b/src/common/time_op/src/time_op.c @@ -43,7 +43,7 @@ void gettime(struct timespec *ts) { #include #define EPOCH_DIFFERENCE 11644473600LL - +struct tm *localtime_r(const time_t *timer, struct tm *result){} // Get the epoch time long long int get_windows_time_epoch() { diff --git a/src/common/utils/include/cust_types.h b/src/common/utils/include/cust_types.h new file mode 100644 index 0000000000..7bde146f97 --- /dev/null +++ b/src/common/utils/include/cust_types.h @@ -0,0 +1,14 @@ +#pragma once + +#ifdef WIN32 + +typedef int uid_t; +typedef int gid_t; +typedef uint32_t u_int32_t; +typedef uint16_t u_int16_t; +typedef uint8_t u_int8_t; +typedef int pid_t; + +typedef int mode_t; +#define PATH_MAX 1024*1024 +#endif diff --git a/src/common/utils/include/makeUnique.h b/src/common/utils/include/makeUnique.h deleted file mode 100644 index c585f27778..0000000000 --- a/src/common/utils/include/makeUnique.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Wazuh shared modules utils - * Copyright (C) 2015, Wazuh Inc. - * July 14, 2020. - * - * This program is free software; you can redistribute it - * and/or modify it under the terms of the GNU General Public - * License (version 2) as published by the FSF - Free Software - * Foundation. - */ - -/* Taken from isocpp.org/files/papers/N3656.txt - * Stephan T. Lavavej - */ -#ifndef _MAKE_UNIQUE_H -#define _MAKE_UNIQUE_H - -#if __cplusplus < 201402L -#include -#include -#include -#include - -namespace std -{ - template struct _Unique_if - { - typedef unique_ptr _Single_object; - }; - - template struct _Unique_if - { - typedef unique_ptr _Unknown_bound; - }; - - template struct _Unique_if - { - typedef void _Known_bound; - }; - - template - typename _Unique_if::_Single_object - make_unique(Args&& ... args) - { - return unique_ptr(new T(std::forward(args)...)); - } - - template - typename _Unique_if::_Unknown_bound - make_unique(size_t n) - { - typedef typename remove_extent::type U; - return unique_ptr(new U[n]()); - } - - template - typename _Unique_if::_Known_bound - make_unique(Args&& ...) = delete; -} -#endif -#endif //_MAKE_UNIQUE_H \ No newline at end of file diff --git a/src/common/utils/include/shared.h b/src/common/utils/include/shared.h index 2ee7f2a699..02dad514e3 100644 --- a/src/common/utils/include/shared.h +++ b/src/common/utils/include/shared.h @@ -13,6 +13,8 @@ * Reference: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/cpp.pdf */ +#pragma once + #if defined(__GNUC__) && (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 1) && (__GNUC_PATCHLEVEL__ >= 2)) || \ ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)) || \ (__GNUC__ >= 5)) @@ -24,9 +26,6 @@ #define FORTIFY_SOURCE #endif -#ifndef SHARED_H -#define SHARED_H - #ifndef LARGEFILE64_SOURCE #define LARGEFILE64_SOURCE #endif /* LARGEFILE64_SOURCE */ @@ -38,8 +37,10 @@ /* Global headers */ #include #include +#ifndef WIN32 #include #include +#endif #include #include #include @@ -53,34 +54,26 @@ #include #endif -/* HPUX does not have select.h */ -#ifndef HPUX #include -#endif - #include -#endif /* WIN32 */ +#endif /* NOT WIN32 */ #include -#include #include #include #include #include +#ifndef WIN32 +#include #include +#endif #include #include #include -#include - -/* The mingw32 builder used by travis.ci can't find glob.h - * Yet glob must work on actual win32. - */ -#ifndef __MINGW32__ -#include -#endif #ifndef WIN32 +#include +#include #include #include #include @@ -88,6 +81,7 @@ #include #include #else +#include #include #include #include @@ -99,16 +93,13 @@ #ifdef __cplusplus #include #define _Atomic(T) std::atomic -#else -#ifdef hpux -// TODO: remove this line after upgrading GCC on HP-UX -#define _Atomic(T) T -#endif #endif #include #include +#ifndef WIN32 #include +#endif #include "defs.h" #include "os_err.h" @@ -117,8 +108,10 @@ #include "file_op.h" #include "regex_op.h" #include "mem_op.h" +#ifndef WIN32 #include "privsep_op.h" #include "pthreads_op.h" +#endif #include "os_xml.h" #include "error_messages.h" #include "os_regex.h" @@ -131,7 +124,6 @@ #include "bzip2_op.h" #include "validate_op.h" - #ifndef LARGEFILE64_SOURCE #define LARGEFILE64_SOURCE #endif /* LARGEFILE64_SOURCE */ @@ -142,29 +134,6 @@ /* Global portability code */ -#ifdef SOLARIS -#include -typedef uint32_t u_int32_t; -typedef uint16_t u_int16_t; -typedef uint8_t u_int8_t; - -#ifndef va_copy -#define va_copy __va_copy -#endif - -#endif /* SOLARIS */ - -#if defined(HPUX) || defined(DOpenBSD) -#include -typedef uint64_t u_int64_t; -typedef int int32_t; -typedef uint32_t u_int32_t; -typedef uint16_t u_int16_t; -typedef uint8_t u_int8_t; - -#define MSG_DONTWAIT 0 -#endif - #ifdef Darwin typedef int sock2len_t; #endif @@ -174,17 +143,12 @@ typedef int sock2len_t; #endif #ifdef WIN32 -typedef int uid_t; -typedef int gid_t; typedef int socklen_t; #define sleep(x) Sleep((x) * 1000) #define srandom(x) srand(x) #define lstat(x,y) stat(x,y) #define CloseSocket(x) closesocket(x) void WinSetError(); -typedef uint32_t u_int32_t; -typedef uint16_t u_int16_t; -typedef uint8_t u_int8_t; #define MSG_DONTWAIT 0 @@ -237,7 +201,7 @@ extern const char *__local_name; #define sqlite_strdup(x,y) ({ if (x) { os_strdup(x, y); } else (void)0; }) -#define w_strlen(x) ({ size_t ret = 0; if (x) ret = strlen(x); ret;}) +#define w_strlen(x) ((x)? strlen(x) : 0) // Calculate the number of elements within an array. // Only static arrays allowed. @@ -247,4 +211,3 @@ extern const char *__local_name; #define FOREVER() 1 #endif -#endif /* SHARED_H */ diff --git a/src/common/validate_op/include/validate_op.h b/src/common/validate_op/include/validate_op.h index 22e8f0801d..688470bc3a 100644 --- a/src/common/validate_op/include/validate_op.h +++ b/src/common/validate_op/include/validate_op.h @@ -11,9 +11,15 @@ #ifndef VALIDATE_H #define VALIDATE_H +#if defined(__GNUC__) || defined(__clang__) +#define ATTR_NONNULL __attribute__((nonnull)) +#define UNREFERENCED_PARAMETER(P) +#else +#define ATTR_NONNULL +#endif /* Run-time definitions */ -int getDefine_Int(const char *high_name, const char *low_name, int min, int max) __attribute__((nonnull)); +int getDefine_Int(const char *high_name, const char *low_name, int min, int max) ATTR_NONNULL; /** @@ -23,7 +29,7 @@ int getDefine_Int(const char *high_name, const char *low_name, int min, int max) * @param that_ip Struct os_ip to check. * @return Returns 1 on success or 0 on failure. */ -int OS_IPFound(const char *ip_address, const os_ip *that_ip) __attribute__((nonnull)); +int OS_IPFound(const char *ip_address, const os_ip *that_ip) ATTR_NONNULL; /** @@ -33,7 +39,7 @@ int OS_IPFound(const char *ip_address, const os_ip *that_ip) __attribute__((nonn * @param list_of_ips List of os_ip struct to check. * @return Returns 1 on success or 0 on failure. */ -int OS_IPFoundList(const char *ip_address, os_ip **list_of_ips);// __attribute__((nonnull)); +int OS_IPFoundList(const char *ip_address, os_ip **list_of_ips);// ATTR_NONNULL; /** @@ -88,7 +94,7 @@ char *OS_IsValidTime(const char *time_str); * @param time_str Time to be validated. * @return Returns 0 if doesn't match or a valid string in success. */ -char *OS_IsValidUniqueTime(const char *time_str) __attribute__((nonnull)); +char *OS_IsValidUniqueTime(const char *time_str) ATTR_NONNULL; /** @@ -98,7 +104,7 @@ char *OS_IsValidUniqueTime(const char *time_str) __attribute__((nonnull)); * @param ossec_time Time interval. * @return Returns 1 on success or 0 on failure. */ -int OS_IsonTime(const char *time_str, const char *ossec_time) __attribute__((nonnull)); +int OS_IsonTime(const char *time_str, const char *ossec_time) ATTR_NONNULL; /** @@ -108,7 +114,7 @@ int OS_IsonTime(const char *time_str, const char *ossec_time) __attribute__((non * @param ossec_time Time interval. * @return Returns 1 on success or 0 on failure. */ -int OS_IsAfterTime(const char *time_str, const char *ossec_time) __attribute__((nonnull)); +int OS_IsAfterTime(const char *time_str, const char *ossec_time) ATTR_NONNULL; /** @@ -130,7 +136,7 @@ char *OS_IsValidDay(const char *day_str); * @param ossec_day Interval. * @return Returns 1 on success or 0 on failure. */ -int OS_IsonDay(int week_day, const char *ossec_day) __attribute__((nonnull)); +int OS_IsonDay(int week_day, const char *ossec_day) ATTR_NONNULL; /** diff --git a/src/common/version_op/src/version_op.c b/src/common/version_op/src/version_op.c index 8001329ce6..8ac3e16096 100644 --- a/src/common/version_op/src/version_op.c +++ b/src/common/version_op/src/version_op.c @@ -29,18 +29,18 @@ os_info *get_win_version() DWORD dwRet; HKEY RegistryKey; char * subkey; - const DWORD vsize = 1024; - TCHAR value[vsize]; - DWORD dwCount = vsize; + #define VSIZE 1024 + TCHAR value[VSIZE]; + DWORD dwCount = VSIZE; char version[64] = ""; - const DWORD size = 30; + #define WINVER_SIZE 30 unsigned long type = REG_DWORD; size_t ver_length = 60; size_t v_length = 20; os_calloc(1,sizeof(os_info),info); - os_calloc(vsize, sizeof(char), subkey); + os_calloc(VSIZE, sizeof(char), subkey); typedef void (WINAPI * PGNSI)(LPSYSTEM_INFO); @@ -70,7 +70,7 @@ os_info *get_win_version() // Read Windows Version from registry - snprintf(subkey, vsize - 1, "%s", "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"); + snprintf(subkey, VSIZE - 1, "%s", "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"); if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, KEY_READ, &RegistryKey) != ERROR_SUCCESS) { merror(SK_REG_OPEN, subkey); @@ -95,11 +95,11 @@ os_info *get_win_version() // Read Windows Version number from registry char vn_temp[64]; memset(vn_temp, '\0', 64); - TCHAR winver[size]; - TCHAR wincomp[size]; + TCHAR winver[WINVER_SIZE]; + TCHAR wincomp[WINVER_SIZE]; DWORD winMajor = 0; DWORD winMinor = 0; - dwCount = size; + dwCount = WINVER_SIZE; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, KEY_READ, &RegistryKey) != ERROR_SUCCESS) { merror(SK_REG_OPEN, subkey); @@ -108,7 +108,7 @@ os_info *get_win_version() // Windows 10 dwRet = RegQueryValueEx(RegistryKey, TEXT("CurrentMajorVersionNumber"), NULL, &type, (LPBYTE)&winMajor, &dwCount); if (dwRet == ERROR_SUCCESS) { - dwCount = size; + dwCount = WINVER_SIZE; dwRet = RegQueryValueEx(RegistryKey, TEXT("CurrentMinorVersionNumber"), NULL, &type, (LPBYTE)&winMinor, &dwCount); if (dwRet != ERROR_SUCCESS) { merror("Error reading 'CurrentMinorVersionNumber' from Windows registry. (Error %u)",(unsigned int)dwRet); @@ -118,7 +118,7 @@ os_info *get_win_version() info->os_major = strdup(vn_temp); snprintf(vn_temp, 63, "%d", (unsigned int)winMinor); info->os_minor = strdup(vn_temp); - dwCount = size; + dwCount = WINVER_SIZE; dwRet = RegQueryValueEx(RegistryKey, TEXT("CurrentBuildNumber"), NULL, NULL, (LPBYTE)wincomp, &dwCount); if (dwRet != ERROR_SUCCESS) { merror("Error reading 'CurrentBuildNumber' from Windows registry. (Error %u)",(unsigned int)dwRet); @@ -129,7 +129,7 @@ os_info *get_win_version() } } - dwCount = vsize; + dwCount = VSIZE; dwRet = RegQueryValueEx(RegistryKey, TEXT("ReleaseId"), NULL, NULL, (LPBYTE)value, &dwCount); if (dwRet != ERROR_SUCCESS) { mdebug1("Could not read the 'ReleaseId' key from Windows registry. (Error %u)",(unsigned int)dwRet); @@ -155,7 +155,7 @@ os_info *get_win_version() free(parts[i]); } free(parts); - dwCount = size; + dwCount = WINVER_SIZE; dwRet = RegQueryValueEx(RegistryKey, TEXT("CurrentBuildNumber"), NULL, NULL, (LPBYTE)wincomp, &dwCount); if (dwRet != ERROR_SUCCESS) { merror("Error reading 'CurrentBuildNumber' from Windows registry. (Error %u)",(unsigned int)dwRet); @@ -240,7 +240,7 @@ os_info *get_win_version() if(!info->os_release) { DWORD service_pack = 0; dwCount = sizeof(DWORD); - snprintf(subkey, vsize - 1, "%s", "SYSTEM\\CurrentControlSet\\Control\\Windows"); + snprintf(subkey, VSIZE - 1, "%s", "SYSTEM\\CurrentControlSet\\Control\\Windows"); if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, KEY_READ, &RegistryKey) != ERROR_SUCCESS) { merror(SK_REG_OPEN, subkey); @@ -280,7 +280,7 @@ os_info *get_win_version() // Read Architecture - snprintf(subkey, vsize - 1, "%s", "System\\CurrentControlSet\\Control\\Session Manager\\Environment"); + snprintf(subkey, VSIZE - 1, "%s", "System\\CurrentControlSet\\Control\\Session Manager\\Environment"); if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, KEY_READ, &RegistryKey) != ERROR_SUCCESS) { merror(SK_REG_OPEN, subkey); @@ -307,12 +307,12 @@ os_info *get_win_version() // Read Hostname - snprintf(subkey, vsize - 1, "%s", "System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName"); + snprintf(subkey, VSIZE - 1, "%s", "System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName"); char nodename[1024] = ""; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, KEY_READ, &RegistryKey) != ERROR_SUCCESS) { merror(SK_REG_OPEN, subkey); } else { - dwCount = size; + dwCount = WINVER_SIZE; dwRet = RegQueryValueEx(RegistryKey, TEXT("ComputerName"), NULL, NULL, (LPBYTE)&nodename, &dwCount); if (dwRet != ERROR_SUCCESS) { diff --git a/src/common/windowsHelper/include/windowsHelper.h b/src/common/windowsHelper/include/windowsHelper.h index 8a709667f3..78875d1e88 100644 --- a/src/common/windowsHelper/include/windowsHelper.h +++ b/src/common/windowsHelper/include/windowsHelper.h @@ -11,8 +11,7 @@ #ifdef WIN32 -#ifndef _NETWORK_WINDOWS_HELPER_H -#define _NETWORK_WINDOWS_HELPER_H +#pragma once #include #include @@ -24,16 +23,17 @@ #include #include #include +#include #include #include "mem_op.h" #include "stringHelper.h" #include "encodingWindowsHelper.h" #include "timeHelper.h" +#include -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-function" -#pragma GCC diagnostic ignored "-Wreturn-type" -#pragma GCC diagnostic ignored "-Wcast-function-type" +typedef unsigned int UINT; +typedef ULONG (WINAPI* ConvertLengthToIpv4Mask_t)(ULONG, PULONG); +typedef ULONG (WINAPI* GetIfEntry2_t)(PMIB_IF_ROW2); constexpr auto WORKING_ADAPTERS_INFO_BUFFER_SIZE { @@ -128,7 +128,6 @@ namespace Utils return ret; } - typedef NETIOAPI_API (WINAPI* ConvertLengthToIpv4Mask_t)(ULONG, PULONG); static ConvertLengthToIpv4Mask_t getConvertLengthToIpv4MaskFunctionAddress() { ConvertLengthToIpv4Mask_t ret{nullptr}; @@ -142,7 +141,6 @@ namespace Utils return ret; } - typedef NETIOAPI_API (WINAPI* GetIfEntry2_t)(PMIB_IF_ROW2); static GetIfEntry2_t getIfEntry2FunctionAddress() { GetIfEntry2_t ret{nullptr}; @@ -682,9 +680,3 @@ namespace Utils } }; } - -#pragma GCC diagnostic pop - -#endif // _NETWORK_WINDOWS_HELPER_H - -#endif //WIN32 diff --git a/src/modules/inventory/src/inventory.cpp b/src/modules/inventory/src/inventory.cpp index 01939c1ebd..393eaad4de 100644 --- a/src/modules/inventory/src/inventory.cpp +++ b/src/modules/inventory/src/inventory.cpp @@ -24,6 +24,12 @@ using namespace std; #define INV_LOGTAG "modules:inventory" // Tag for log messages +#ifdef WIN32 +void pthread_exit(void *p){ + +} +#endif + void Inventory::start() { if (!m_enabled) { diff --git a/src/modules/inventory/testtool/CMakeLists.txt b/src/modules/inventory/testtool/CMakeLists.txt index f0d28c8dd2..0f22ebcf71 100644 --- a/src/modules/inventory/testtool/CMakeLists.txt +++ b/src/modules/inventory/testtool/CMakeLists.txt @@ -13,7 +13,7 @@ if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set(CMAKE_CXX_FLAGS_DEBUG "-fsanitize=address,leak,undefined") endif(FSANITIZE) else() - set(CMAKE_CXX_FLAGS "/Zi /W4 /permissive- /std:c++14 /MT") + set(CMAKE_CXX_FLAGS "/Zi /W4 /permissive- /std:c++14 /MT /EHsc") endif() file(GLOB INVENTORY_TESTTOOL_SRC @@ -31,11 +31,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows") inventory psapi iphlpapi - crypto - ssl ws2_32 crypt32 - -static-libstdc++ ) elseif (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") target_link_libraries(inventory_test_tool