<commit_before>#include <cppassert/details/StackTrace.hpp>
#include <stdexcept>
#include <memory>
#include <mutex>
#include <cstdlib>
#include <Windows.h>
#include <DbgHelp.h>
#include <strsafe.h>

namespace cppassert
{
    namespace internal
    {

        /**
         * Displays error message related to last occured error
         * 
         */
        void DisplayLastError(LPTSTR lpszFunction)
        {
            // Retrieve the system error message for the last-error code

            LPVOID lpMsgBuf;
            LPVOID lpDisplayBuf;
            DWORD dw = GetLastError();

            FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                dw,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                (LPTSTR)&lpMsgBuf,
                0, NULL);

            // Display the error message and exit the process

            lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
                (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));

            StringCchPrintf((LPTSTR)lpDisplayBuf,
                LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                TEXT("%s failed with error %d: %s"),
                lpszFunction, dw, lpMsgBuf);
            std::fprintf(stderr, "%s", (LPCTSTR)lpDisplayBuf);
            LocalFree(lpMsgBuf);
            LocalFree(lpDisplayBuf);
        }

        /**
        * Provides windows implementation of stack trace collection.
        */
        class StackTraceImpl
        {
            StackTraceImpl(const StackTraceImpl &) = delete;
            StackTraceImpl &operator=(const StackTraceImpl &) = delete;
            struct BacktraceSymbol : public StackTrace::StackFrame
            {
                BacktraceSymbol() = default;

                BacktraceSymbol(const void *frameAddr, const char *name)
                {
                    address_ = frameAddr;
                    setSymbol(name);
                }

                void setAddress(const void *frameAddress)
                {
                    address_ = frameAddress;
                }

                void setSymbol(const char *name)
                {
                    if (name && name[0])
                    {
                        strcpy_s(symbolName_, MaxSymbolNameSize, name);
                        symbolName_[MaxSymbolNameSize - 1] = '\0';
                    }
                    else
                    {
                        symbolName_[0] = '\0';
                    }
                    symbol_ = symbolName_;
                }

                enum { MaxSymbolNameSize = MAX_SYM_NAME + 1 };
                char symbolName_[MaxSymbolNameSize];
            };
        public:
            StackTraceImpl()
            {
            }

            ~StackTraceImpl()
            {
            }

            void collect()
            {
                HANDLE              process;
                ULONG               frame;
                PSYMBOL_INFO        symbol;
                DWORD64             displacement;
                PVOID               frames[cFramesSize];
                enum {              MaxNameLen = MAX_SYM_NAME + 1 };
                enum {              BufferSize = sizeof(SYMBOL_INFO)
                                                + MaxNameLen * sizeof(TCHAR) };

                char buffer[BufferSize] = { '\0' };

                symbol = static_cast<PSYMBOL_INFO>(static_cast<void *>(buffer));

                std::memset(symbol, 0, BufferSize);
                symbol->MaxNameLen = MaxNameLen-1;
                symbol->SizeOfStruct = sizeof(SYMBOL_INFO);

                process = GetCurrentProcess();
                displacement = 0;
                std::unique_lock<std::mutex> lock(mutex_);

                SymInitialize(process, NULL, TRUE);
                capturedFrames_ = CaptureStackBackTrace(cFramesToSkip
                                            , cFramesSize
                                            , frames
                                            , NULL);

                if (capturedFrames_ > cFramesSize)
                {
                    capturedFrames_ = cFramesSize;
                }

                for (frame = 0; frame<capturedFrames_; frame++)
                {
                    DWORD64 frameAddr = (DWORD64)(frames[frame]);
                    frames_[frame].setAddress(frames[frame]);
                    if (SymFromAddr(process, frameAddr, &displacement, symbol)
                                    == TRUE)
                    {
                        frames_[frame].setSymbol(symbol->Name);
                    }
                    else
                    {
                        DisplayLastError("SymFromAddr");
                    }
                }
            }

            /**
            * Returns number of captured frames
            * @return 0
            */
            std::size_t size() const
            {
                return capturedFrames_;<commit_msg>Added check for SymInitialize call<commit_after>#include <cppassert/details/StackTrace.hpp>
#include <stdexcept>
#include <memory>
#include <mutex>
#include <cstdlib>
#include <Windows.h>
#include <DbgHelp.h>
#include <strsafe.h>

namespace cppassert
{
    namespace internal
    {

        /**
         * Displays error message related to last occured error
         * 
         */
        void DisplayLastError(LPTSTR lpszFunction)
        {
            // Retrieve the system error message for the last-error code

            LPVOID lpMsgBuf;
            LPVOID lpDisplayBuf;
            DWORD dw = GetLastError();

            FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                dw,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                (LPTSTR)&lpMsgBuf,
                0, NULL);

            // Display the error message and exit the process

            lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
                (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));

            StringCchPrintf((LPTSTR)lpDisplayBuf,
                LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                TEXT("%s failed with error %d: %s"),
                lpszFunction, dw, lpMsgBuf);
            std::fprintf(stderr, "%s", (LPCTSTR)lpDisplayBuf);
            LocalFree(lpMsgBuf);
            LocalFree(lpDisplayBuf);
        }

        /**
        * Provides windows implementation of stack trace collection.
        */
        class StackTraceImpl
        {
            StackTraceImpl(const StackTraceImpl &) = delete;
            StackTraceImpl &operator=(const StackTraceImpl &) = delete;
            struct BacktraceSymbol : public StackTrace::StackFrame
            {
                BacktraceSymbol() = default;

                BacktraceSymbol(const void *frameAddr, const char *name)
                {
                    address_ = frameAddr;
                    setSymbol(name);
                }

                void setAddress(const void *frameAddress)
                {
                    address_ = frameAddress;
                }

                void setSymbol(const char *name)
                {
                    if (name && name[0])
                    {
                        strcpy_s(symbolName_, MaxSymbolNameSize, name);
                        symbolName_[MaxSymbolNameSize - 1] = '\0';
                    }
                    else
                    {
                        symbolName_[0] = '\0';
                    }
                    symbol_ = symbolName_;
                }

                enum { MaxSymbolNameSize = MAX_SYM_NAME + 1 };
                char symbolName_[MaxSymbolNameSize];
            };
        public:
            StackTraceImpl()
            {
            }

            ~StackTraceImpl()
            {
            }

            void collect()
            {
                HANDLE              process;
                ULONG               frame;
                PSYMBOL_INFO        symbol;
                DWORD64             displacement;
                PVOID               frames[cFramesSize];
                enum {              MaxNameLen = MAX_SYM_NAME + 1 };
                enum {              BufferSize = sizeof(SYMBOL_INFO)
                                                + MaxNameLen * sizeof(TCHAR) };

                char buffer[BufferSize] = { '\0' };

                symbol = static_cast<PSYMBOL_INFO>(static_cast<void *>(buffer));

                std::memset(symbol, 0, BufferSize);
                symbol->MaxNameLen = MaxNameLen-1;
                symbol->SizeOfStruct = sizeof(SYMBOL_INFO);

                process = GetCurrentProcess();
                displacement = 0;
                std::unique_lock<std::mutex> lock(mutex_);

                if (SymInitialize(process, NULL, TRUE) != TRUE)
                {
                    DisplayLastError("SymInitialize");
                    return;
                }

                capturedFrames_ = CaptureStackBackTrace(cFramesToSkip
                                            , cFramesSize
                                            , frames
                                            , NULL);

                if (capturedFrames_ > cFramesSize)
                {
                    capturedFrames_ = cFramesSize;
                }

                for (frame = 0; frame<capturedFrames_; frame++)
                {
                    DWORD64 frameAddr = (DWORD64)(frames[frame]);
                    frames_[frame].setAddress(frames[frame]);
                    if (SymFromAddr(process, frameAddr, &displacement, symbol)
                                    == TRUE)
                    {
                        frames_[frame].setSymbol(symbol->Name);
                    }
                    else
                    {
                        DisplayLastError("SymFromAddr");
                    }
                }
            }

            /**
            * Returns number of captured frames
            * @return number of captured frames
            */
            std::size_t size() const
            {
                return capturedFrames_;
{'fraction_non_alphanumeric': 0.07905100374603785, 'fraction_numerical': 0.004130246854288733, 'mean_word_length': 1.0958132045088567, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1769710', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2348, 'n_words': 482}