From c60d46d3f97dcf185f1716388eaa3907c2e545e5 Mon Sep 17 00:00:00 2001 From: Jens Lorenz Date: Mon, 5 Feb 2018 17:15:41 +0100 Subject: AMUtil: Fixes infinite loop in dlt wrapper for out of range values of enumerations. Signed-off-by: Kapildev Patel --- AudioManagerUtilities/include/CAmDltWrapper.h | 367 +++++++++++++------------- 1 file changed, 183 insertions(+), 184 deletions(-) diff --git a/AudioManagerUtilities/include/CAmDltWrapper.h b/AudioManagerUtilities/include/CAmDltWrapper.h index 8c5678e..25a62c7 100644 --- a/AudioManagerUtilities/include/CAmDltWrapper.h +++ b/AudioManagerUtilities/include/CAmDltWrapper.h @@ -29,68 +29,68 @@ #include "audiomanagertypes.h" #ifdef WITH_DLT - #include + #include #else - #include - #include - - #define DLT_USER_BUF_MAX_SIZE 2048 - - /** - * This structure is used for every context used in an application. - */ - typedef struct - { - char contextID[4]; /**< context id */ - int32_t log_level_pos; /**< offset in user-application context field */ - int32_t log_level_user; /** any message above this log level is not logged */ - } DltContext; - - /** - * Definition of DLT trace status - */ - typedef enum - { - DLT_TRACE_STATUS_DEFAULT = -1, /**< Default trace status */ - DLT_TRACE_STATUS_OFF = 0x00, /**< Trace status: Off */ - DLT_TRACE_STATUS_ON = 0x01 /**< Trace status: On */ - } DltTraceStatusType; - - /** - * This structure is used for context data used in an application. - */ - typedef struct - { - DltContext *handle; /**< pointer to DltContext */ - std::stringstream buffer; /**< buffer for building log message*/ - int32_t log_level; /**< log level */ - int32_t trace_status; /**< trace status */ - int32_t args_num; /**< number of arguments for extended header*/ - uint8_t mcnt; /**< message counter */ - char* context_description; /**< description of context */ - } DltContextData; - - /** - * Definitions of DLT log level - */ - typedef enum - { - DLT_LOG_DEFAULT = -1, /**< Default log level */ - DLT_LOG_OFF = 0x00, /**< Log level off */ - DLT_LOG_FATAL = 0x01, /**< fatal system error */ - DLT_LOG_ERROR = 0x02, /**< error with impact to correct functionality */ - DLT_LOG_WARN = 0x03, /**< warning, correct behaviour could not be ensured */ - DLT_LOG_INFO = 0x04, /**< informational */ - DLT_LOG_DEBUG = 0x05, /**< debug */ - DLT_LOG_VERBOSE = 0x06 /**< highest grade of information */ - } DltLogLevelType; - - #define DLT_DEFAULT_LOG_LEVEL DLT_LOG_INFO - #define DLT_DECLARE_CONTEXT(CONTEXT) \ - DltContext CONTEXT; - - #define DLT_IMPORT_CONTEXT(CONTEXT) \ - extern DltContext CONTEXT; + #include + #include + + #define DLT_USER_BUF_MAX_SIZE 2048 + + /** + * This structure is used for every context used in an application. + */ + typedef struct + { + char contextID[4]; /**< context id */ + int32_t log_level_pos; /**< offset in user-application context field */ + int32_t log_level_user; /** any message above this log level is not logged */ + } DltContext; + + /** + * Definition of DLT trace status + */ + typedef enum + { + DLT_TRACE_STATUS_DEFAULT = -1, /**< Default trace status */ + DLT_TRACE_STATUS_OFF = 0x00, /**< Trace status: Off */ + DLT_TRACE_STATUS_ON = 0x01 /**< Trace status: On */ + } DltTraceStatusType; + + /** + * This structure is used for context data used in an application. + */ + typedef struct + { + DltContext *handle; /**< pointer to DltContext */ + std::stringstream buffer; /**< buffer for building log message*/ + int32_t log_level; /**< log level */ + int32_t trace_status; /**< trace status */ + int32_t args_num; /**< number of arguments for extended header*/ + uint8_t mcnt; /**< message counter */ + char* context_description; /**< description of context */ + } DltContextData; + + /** + * Definitions of DLT log level + */ + typedef enum + { + DLT_LOG_DEFAULT = -1, /**< Default log level */ + DLT_LOG_OFF = 0x00, /**< Log level off */ + DLT_LOG_FATAL = 0x01, /**< fatal system error */ + DLT_LOG_ERROR = 0x02, /**< error with impact to correct functionality */ + DLT_LOG_WARN = 0x03, /**< warning, correct behaviour could not be ensured */ + DLT_LOG_INFO = 0x04, /**< informational */ + DLT_LOG_DEBUG = 0x05, /**< debug */ + DLT_LOG_VERBOSE = 0x06 /**< highest grade of information */ + } DltLogLevelType; + + #define DLT_DEFAULT_LOG_LEVEL DLT_LOG_INFO + #define DLT_DECLARE_CONTEXT(CONTEXT) \ + DltContext CONTEXT; + + #define DLT_IMPORT_CONTEXT(CONTEXT) \ + extern DltContext CONTEXT; #endif // WITH_DLT @@ -107,56 +107,55 @@ class CAmDltWrapper { public: - /** - * This structure is used for context data used in an application. - */ - typedef struct - { - DltContext *handle; /**< pointer to DltContext */ - std::stringstream buffer; /**< buffer for building log message*/ - int32_t log_level; /**< log level */ - int32_t trace_status; /**< trace status */ - int32_t args_num; /**< number of arguments for extended header*/ - uint8_t mcnt; /**< message counter */ - char* context_description; /**< description of context */ - } NoDltContextData; - - /* - * The eunum gives the logtype - */ - enum logDestination - { - DAEMON=0, //!< logging with the DLT daemon - COMMAND_LINE=1, //!< logging with commandline - FILE_OUT =2 //!< logging into a file - }; - - /** - * Instanciate the Dlt Wrapper - * @param appid The AppID - * @param description A description of the Application - * @param debugEnabled if set to true, debug outputs will be generated, default = true - * @param logDest the destination, the log should be written - * @param Filename the filename with absolute path where the log shall be written. only needed if logDest==FILE_OUT - * @param onlyError if set to true, only errors will be logged. just valid for commandline and file logs, default value = false - */ - static CAmDltWrapper* instanctiateOnce(const char *appid, const char * description, const bool debugEnabled = true, const logDestination logDest = logDestination::DAEMON, const std::string Filename="",bool onlyError=false); + /** + * This structure is used for context data used in an application. + */ + typedef struct + { + DltContext *handle; /**< pointer to DltContext */ + std::stringstream buffer; /**< buffer for building log message*/ + int32_t log_level; /**< log level */ + int32_t trace_status; /**< trace status */ + int32_t args_num; /**< number of arguments for extended header*/ + uint8_t mcnt; /**< message counter */ + char* context_description; /**< description of context */ + } NoDltContextData; + + /* + * The eunum gives the logtype + */ + enum logDestination + { + DAEMON=0, //!< logging with the DLT daemon + COMMAND_LINE=1, //!< logging with commandline + FILE_OUT =2 //!< logging into a file + }; + + /** + * Instanciate the Dlt Wrapper + * @param appid The AppID + * @param description A description of the Application + * @param debugEnabled if set to true, debug outputs will be generated, default = true + * @param logDest the destination, the log should be written + * @param Filename the filename with absolute path where the log shall be written. only needed if logDest==FILE_OUT + * @param onlyError if set to true, only errors will be logged. just valid for commandline and file logs, default value = false + */ + static CAmDltWrapper* instanctiateOnce(const char *appid, const char * description, const bool debugEnabled = true, const logDestination logDest = logDestination::DAEMON, const std::string Filename="",bool onlyError=false); /** * get the Wrapper Instance */ static CAmDltWrapper* instance(); - - /** - * register a context - */ + + /** + * register a context + */ void registerContext(DltContext& handle, const char *contextid, const char * description); void registerContext(DltContext& handle, const char *contextid, const char * description, const DltLogLevelType level, const DltTraceStatusType status); void unregisterContext(DltContext& handle); bool getEnabled(); ~CAmDltWrapper(); - - + bool init(DltLogLevelType loglevel, DltContext* context = NULL); bool checkLogLevel(DltLogLevelType logLevel) { @@ -186,147 +185,147 @@ public: void append(const std::vector & data); template void appendNoDLT(T value) - { - mNoDltContextData.buffer << value <<" "; - } + { + mNoDltContextData.buffer << value <<" "; + } // specialization for const char* template void append(const char* value) { - #ifdef WITH_DLT - if (mlogDestination == logDestination::DAEMON) - { - dlt_user_log_write_string(&mDltContextData, value); - } - else - { - mNoDltContextData.buffer << std::string(value); - } - #else - mNoDltContextData.buffer << std::string(value); - #endif //WITH_DLT - - } - + #ifdef WITH_DLT + if (mlogDestination == logDestination::DAEMON) + { + dlt_user_log_write_string(&mDltContextData, value); + } + else + { + mNoDltContextData.buffer << std::string(value); + } + #else + mNoDltContextData.buffer << std::string(value); + #endif //WITH_DLT + + } + private: - static const std::vector mStr_error; - static const std::vector mStr_sourceState; - static const std::vector mStr_MuteState; - static const std::vector mStr_DomainState; - static const std::vector mStr_ConnectionState; - static const std::vector mStr_Availability; - static const std::vector mStr_Interrupt; - static const std::vector mStr_Handle; - static const std::vector mStr_NotificationStatus; + static const std::vector mStr_error; + static const std::vector mStr_sourceState; + static const std::vector mStr_MuteState; + static const std::vector mStr_DomainState; + static const std::vector mStr_ConnectionState; + static const std::vector mStr_Availability; + static const std::vector mStr_Interrupt; + static const std::vector mStr_Handle; + static const std::vector mStr_NotificationStatus; public: // specialization for const am_Error_e template void append(const am_Error_e value) { - if ((int)value >=mStr_error.size()) + if (static_cast(value) >= mStr_error.size()) { - append("value for am_Error_e out of bounds!"); - append(value); - return; + append("value for am_Error_e out of bounds!"); + append(static_cast(value)); + return; } - append(mStr_error[value]); + append(mStr_error[value]); } // specialization for const am_Error_e template void append(const am_SourceState_e value) { - if ((int)value >=(mStr_sourceState.size())) + if (static_cast(value) >= mStr_sourceState.size()) { - append("value for am_SourceState_e out of bounds!"); - append(value); - return; + append("value for am_SourceState_e out of bounds!"); + append(static_cast(value)); + return; } - append(mStr_sourceState[value]); + append(mStr_sourceState[value]); } template void append(const am_MuteState_e value) { - if ((int)value >=mStr_MuteState.size()) + if (static_cast(value) >= mStr_MuteState.size()) { - append("value for am_MuteState_e out of bounds!"); - append(value); - return; + append("value for am_MuteState_e out of bounds!"); + append(static_cast(value)); + return; } - append(mStr_MuteState[value]); + append(mStr_MuteState[value]); } template void append(const am_DomainState_e value) { - if ((int)value >= mStr_DomainState.size()) + if (static_cast(value) >= mStr_DomainState.size()) { - append("value for am_DomainState_e out of bounds!"); - append(value); - return; + append("value for am_DomainState_e out of bounds!"); + append(static_cast(value)); + return; } - append(mStr_DomainState[value]); + append(mStr_DomainState[value]); } template void append(const am_ConnectionState_e value) { - if ((int)value >=mStr_ConnectionState.size()) + if (static_cast(value) >= mStr_ConnectionState.size()) { - append("value for am_ConnectionState_e out of bounds!"); - append(value); - return; + append("value for am_ConnectionState_e out of bounds!"); + append(static_cast(value)); + return; } - append(mStr_ConnectionState[value]); + append(mStr_ConnectionState[value]); } template void append(const am_Availability_e value) { - if ((int)value >= mStr_Availability.size()) + if (static_cast(value) >= mStr_Availability.size()) { - append("value for am_Availability_e out of bounds!"); - append(value); - return; + append("value for am_Availability_e out of bounds!"); + append(static_cast(value)); + return; } - append(mStr_Availability[value]); + append(mStr_Availability[value]); } template void append(const am_InterruptState_e value) { - if ((int)value >=mStr_Interrupt.size()) + if (static_cast(value) >= mStr_Interrupt.size()) { - append("value for am_InterruptState_e out of bounds!"); - append(value); - return; + append("value for am_InterruptState_e out of bounds!"); + append(static_cast(value)); + return; } - append(mStr_Interrupt[value]); + append(mStr_Interrupt[value]); } template void append(const am_Handle_e value) { - if ((int)value >=mStr_Handle.size()) + if (static_cast(value) >= mStr_Handle.size()) { - append("value for am_Handle_e out of bounds!"); - append(value); - return; + append("value for am_Handle_e out of bounds!"); + append(static_cast(value)); + return; } - append(mStr_Handle[value]); + append(mStr_Handle[value]); } template void append(const am_Handle_s value) { - append (value.handleType); - append (value.handle); + append (value.handleType); + append (value.handle); } template void append(const am_NotificationStatus_e value) { - if ((int)value >=mStr_NotificationStatus.size()) + if (static_cast(value) >= mStr_NotificationStatus.size()) { - append("value for am_NotificationStatus_e out of bounds!"); - append(value); - return; + append("value for am_NotificationStatus_e out of bounds!"); + append(static_cast(value)); + return; } - append(mStr_NotificationStatus[value]); + append(mStr_NotificationStatus[value]); } // Template to print unknown pointer types with their address @@ -354,9 +353,9 @@ public: } private: - /** - * private contructor - */ + /** + * private contructor + */ CAmDltWrapper(const char *appid, const char * description, const bool debugEnabled = true, const logDestination logDest = logDestination::DAEMON, const std::string Filename="",bool onlyError=false); //is private because of singleton pattern bool initNoDlt(DltLogLevelType loglevel, DltContext* context); std::string now(); @@ -364,7 +363,7 @@ private: DltContextData mDltContextData; //!< contextdata NoDltContextData mNoDltContextData; //! mMapContext; //!< a Map for all registered context - bool mDebugEnabled; //!< debug Enabled or not + bool mDebugEnabled; //!< debug Enabled or not logDestination mlogDestination; //!< The log destination std::ofstream mFilename; //!< Filename for logging bool mOnlyError; //!< Only if Log Level is above Error @@ -385,14 +384,14 @@ template void log(DltContext* const context, DltLogLevelType loglevel, T value, TArgs... args) { CAmDltWrapper* inst(CAmDltWrapper::instance()); - if (!inst->getEnabled()) + if (!inst->getEnabled()) { - return; - } + return; + } if (!inst->init(loglevel, context)) { - return; - } + return; + } inst->append(value); inst->append(args...); inst->send(); -- cgit v1.2.1