Sanitize log argument (#27)

This commit is contained in:
Ivan Roberto de Oliveira 2016-12-14 12:52:06 -02:00 committed by Luke Usher
parent 0e230ebd47
commit 0afc92bf96
2 changed files with 15 additions and 6 deletions

View File

@ -67,6 +67,19 @@ extern thread_local const DWORD _CurrentThreadId;
// TODO : Use Boost.Format http://www.boost.org/doc/libs/1_53_0/libs/format/index.html
extern thread_local std::string _logPrefix;
// Data sanitization functions
// Default sanitization functions simply returns the given argument
template<class T>
inline T _log_sanitize(T arg) { return arg; }
// Sanitize C-style strings by converting NULL to "<nullptr>" to prevent null dereference
inline const char * _log_sanitize(const char *arg) { return (NULL == arg) ? "<nullptr>" : arg; }
inline const wchar_t * _log_sanitize(const wchar_t *arg) { return (NULL == arg) ? L"<nullptr>" : arg; }
// Convert BOOLs to strings properly
inline const char * _log_sanitize(BOOL value) { return value ? "TRUE" : "FALSE"; }
#ifdef _DEBUG_TRACE
#define LOG_FUNC_BEGIN \
do { if(g_bPrintfOn) { \
@ -80,11 +93,7 @@ extern thread_local std::string _logPrefix;
// LOG_FUNC_ARG_OUT writes output via all available ostream << operator overloads, adding detail where possible
#define LOG_FUNC_ARG(arg) \
msg << "\n " << std::setw(26) << std::left << std::setfill(' ') << #arg" : " << arg;
// LOG_FUNC_ARG_STR writes a pointer-based string argument, rendering nulls as <nullptr>
#define LOG_FUNC_ARG_STR(arg) \
msg << "\n " << std::setw(26) << std::left << std::setfill(' ') << #arg" : " << (arg == NULL ? "<nullptr>" : arg);
msg << "\n " << std::setw(26) << std::left << std::setfill(' ') << #arg" : " << _log_sanitize(arg);
// LOG_FUNC_ARG_OUT prevents expansion of types, by only rendering as a pointer
#define LOG_FUNC_ARG_OUT(arg) \

View File

@ -815,7 +815,7 @@ DWORD WINAPI XTL::EmuXLaunchNewImage
)
{
LOG_FUNC_BEGIN
LOG_FUNC_ARG_STR(lpTitlePath)
LOG_FUNC_ARG(lpTitlePath)
LOG_FUNC_ARG(pLaunchData)
LOG_FUNC_END;