[SOLVED] QCA encode/decode crash



  • Hi everyone!

    In my project i'm using QCA library to encode/decode datas by AES256. I have class which open file and do encode datas. This class look like:

    #ifndef MDLFILEREADER_H
    #define MDLFILEREADER_H
    
    #include <QtCore>
    
    class MdlFileReader : public QObject
    {
            Q_OBJECT
    
        private:
            QString m_logFilePath;
            QString m_aesKey;
            QString m_aesIv;
    
            QString decode(QString encodeString, QString key, QString iv);
            QByteArray uncompressDatas(QByteArray datas);
    
        public:
            explicit MdlFileReader(QString path, QString key, QString iv, QObject *parent = 0);
    
        signals:
            void fileReaderDidFinish(QVariant jsonObj);
            void fileReaderDidUncompressContent(QString uncompressedString);
    
        public slots:
            void startParseFile();
    };
    
    #endif // MDLFILEREADER_H
    

    Implementation of class:

    #include "mdlfilereader.h"
    #include <zlib.h>
    #include <QtCrypto>
    
    #define         GZIP_WINDOWS_BIT        15 + 16
    #define         GZIP_CHUNK_SIZE         32 * 1024
    
    QString MdlFileReader::decode(QString encodeString, QString key, QString iv)
    {
        if(!encodeString.isEmpty())
        {
            QCA::Initializer init = QCA::Initializer();
            QCA::SymmetricKey keyData = QCA::SymmetricKey(key.toUtf8());
            QCA::InitializationVector ivData = QCA::InitializationVector(iv.toUtf8());
            QCA::Cipher cipherRes = QCA::Cipher(QString("aes256"), QCA::Cipher::CBC,
                                             QCA::Cipher::DefaultPadding, QCA::Decode,
                                             keyData, ivData);
    
            QCA::SecureArray decryptedData = cipherRes.process(QCA::hexToArray(encodeString));
    
            if (!cipherRes.ok())
            {
                qDebug() << "Decryption failed !  ";
                return "";
            }
    
            return QString(decryptedData.data());
        }
    
        return "";
    }
    
    QByteArray MdlFileReader::uncompressDatas(QByteArray datas)
    {
        QByteArray res;
        res.clear();
    
        if(!datas.isEmpty())
        {
            z_stream strm;
            strm.zalloc = Z_NULL;
            strm.zfree = Z_NULL;
            strm.opaque = Z_NULL;
            strm.avail_in = 0;
            strm.next_in = Z_NULL;
    
            int ret = inflateInit2(&strm, GZIP_WINDOWS_BIT);
            if (ret != Z_OK)
            {
                return QByteArray();
            }
    
            char *input_data = datas.data();
            int input_data_left = datas.length();
    
            do{
    
                int chunk_size = qMin(GZIP_CHUNK_SIZE, input_data_left);
                if(chunk_size <= 0)
                {
                    break;
                }
    
                strm.next_in = (unsigned char*)input_data;
                strm.avail_in = chunk_size;
                input_data += chunk_size;
                input_data_left -= chunk_size;
    
                do {
    
                    char out[GZIP_CHUNK_SIZE];
                    strm.next_out = (unsigned char*)out;
                    strm.avail_out = GZIP_CHUNK_SIZE;
                    ret = inflate(&strm, Z_NO_FLUSH);
    
                    switch (ret)
                    {
                        case Z_NEED_DICT:
                            ret = Z_DATA_ERROR;
                        case Z_DATA_ERROR:
                        case Z_MEM_ERROR:
                        case Z_STREAM_ERROR:
                            inflateEnd(&strm);
                            return QByteArray();
                    }
    
                    int have = (GZIP_CHUNK_SIZE - strm.avail_out);
                    if(have > 0)
                    {
                        res.append((char*)out, have);
                    }
    
                }while (strm.avail_out == 0);
    
            }while (ret != Z_STREAM_END);
    
            inflateEnd(&strm);
    
            return res;
        }
    
        return res;
    }
    
    MdlFileReader::MdlFileReader(QString path, QString key, QString iv, QObject *parent) : QObject(parent)
    {
        m_logFilePath = path;
        m_aesKey = key;
        m_aesIv = iv;
    }
    
    void MdlFileReader::startParseFile()
    {
        if(!m_logFilePath.isEmpty())
        {
            m_logFilePath = m_logFilePath.replace("file://", "");
            QFile file(m_logFilePath);
            if(file.exists())
            {
                if(file.open(QIODevice::ReadOnly | QIODevice::Text))
                {
                    QTextStream stream(&file);
                    QString content = stream.readAll();
    
                    if(!content.isNull())
                    {
                        QByteArray compressedDatas = QByteArray::fromBase64(content.toUtf8());
                        QString fileContent = QString(uncompressDatas(compressedDatas));        //Trying to decompress source datas
    
                        emit fileReaderDidUncompressContent(fileContent);
    
                        QString decodedString;
                        if(!fileContent.isEmpty()) //If source datas was compressed.
                        {
                            decodedString = decode(fileContent, m_aesKey, m_aesIv);
                        }
                        else        //If source datas doen't compressed we trying to decode withot decompress.
                        {
                            decodedString = decode(compressedDatas, m_aesKey, m_aesIv);
                        }
    
                        //Start convert to Objects
                        if(!decodedString.isNull())
                        {
                            QString crashString("Crash=Y");
                            bool wasCrash = false;
                            if(decodedString.startsWith(crashString, Qt::CaseInsensitive))
                            {
                                wasCrash = true;
                            }
    
                            decodedString = QString("[") + decodedString.mid(crashString.length(),
                                                                             decodedString.length()-crashString.length()-1) + QString("]");
    
                            QJsonDocument doc = QJsonDocument::fromJson(decodedString.toUtf8());
                            if(!doc.isEmpty())
                            {
                                QVariant data = doc.toVariant();
                                QList<QVariant> list = data.toList();
    
                                if(!list.isEmpty())
                                {
                                    QVariantMap jsonObj, crashImage, startSession;
                                    QVariantList methods, viewsList, classes, messageList, systemsList, frameworks, images, loggerMessagesList, loggerExceptionsList, sessions;
    
                                    foreach(QVariant item, list)
                                    {
                                        QVariantMap obj = item.toMap();
                                        QString componentType = obj.value(QString("componentType")).toString();
                                        QString action = obj.value(QString("action")).toString();
    
                                        if(componentType == QString("DeviceInfo"))
                                        {
                                            jsonObj["deviceInfo"] = obj;
                                        }
                                        else if(componentType == QString("ApplicationInfo"))
                                        {
                                            jsonObj["appInfo"] = obj;
                                        }
                                        else if(componentType == QString("LoggerInfo"))
                                        {
                                            jsonObj["loggerInfo"] = obj;
                                        }
                                        else if(componentType == QString("Crash"))
                                        {
                                            jsonObj["crashLog"] = obj;
                                        }
                                        else if(componentType == QString("StatisticObject"))
                                        {
                                            jsonObj["statistic"] = obj;
                                        }
                                        else if(componentType == QString("Method"))
                                        {
                                            methods.append(obj);
                                        }
                                        else if(componentType == QString("LibImages"))
                                        {
                                            frameworks = obj["list"].toList();
                                        }
                                        else if(componentType == QString("Image"))
                                        {
                                            images.append(obj);
    
                                            bool isCrash = obj["forCrash"].toBool();
                                            if(isCrash)
                                            {
                                                crashImage = obj;
                                            }
                                        }
                                        else if(componentType == QString("ViewStructure"))
                                        {
                                            viewsList.append(obj);
                                        }
                                        else if(componentType == QString("LoggerMessage"))
                                        {
                                            if(action == QString("Exception"))
                                            {
                                                loggerExceptionsList.append(obj);
                                            }
                                            else
                                            {
                                                loggerMessagesList.append(obj);
                                            }
                                        }
                                        else if(componentType == QString("Message"))
                                        {
                                            messageList.append(obj);
                                        }
                                        else if(componentType == QString("SystemMessage"))
                                        {
                                            systemsList.append(obj);
                                        }
                                        else if(componentType == QString("Class"))
                                        {
                                            classes.append(obj);
                                        }
                                        else if(componentType == QString("Session"))
                                        {
                                            if(action == QString("SessionStart"))
                                            {
                                                startSession = obj;
                                            }
                                            else if(action == QString("SessionFinished"))
                                            {
                                                QVariantMap map = startSession;
                                                map["start"] = startSession["timestamp"];
                                                map["end"] = obj["timestamp"];
                                                map["length"] = (obj["timestamp"].toDouble() - startSession["timestamp"].toDouble());
                                                sessions.append(map);
                                            }
                                        }
                                    }
    
    
                                    QString path = m_logFilePath;
                                    QString logFile = m_logFilePath.split("/").last();
                                    QString logFileFolder = path.replace(logFile, "");
    
                                    jsonObj["logFilePath"] = m_logFilePath;
                                    jsonObj["logFileFolder"] = logFileFolder;
                                    jsonObj["logContent"] = decodedString;
                                    jsonObj["wasCrash"] = wasCrash;
    
                                    jsonObj["classes"] = classes;
                                    jsonObj["methods"] = methods;
                                    jsonObj["viewsStructures"] = viewsList;
                                    jsonObj["messages"] = messageList;
                                    jsonObj["systemMessage"] = systemsList;
                                    jsonObj["frameworksList"] = frameworks;
                                    jsonObj["images"] = images;
                                    jsonObj["sessions"] = sessions;
    
                                    QVariantMap crashLog = jsonObj["crashLog"].toMap();
                                    crashLog["image"] = crashImage;
                                    jsonObj["crashLog"] = crashLog;
    
                                    QVariantMap loggerInfo = jsonObj["loggerInfo"].toMap();
                                    loggerInfo["messages"] = loggerMessagesList;
                                    loggerInfo["exceptions"] = loggerExceptionsList;
                                    jsonObj["loggerInfo"] = loggerInfo;
    
                                    emit fileReaderDidFinish(jsonObj);
                                }
                            }
                        }
                    }
                }
            }
        }
    
        emit fileReaderDidFinish(QVariant());
    }
    

    I use it like:

    MdlFileReader* fileReader = new MdlFileReader(file, m_settingsManager->getAesKey(), m_settingsManager->getAesInventoryKey());
    fileReader->setParent(this);
    connect(fileReader, &MdlFileReader::fileReaderDidFinish, this, [this](QVariant jsonObj) {
    
        if(!jsonObj.isNull())
        {
            m_parser->updateFromJSONObject(jsonObj);
        }
    });
    fileReader->startParseFile();
    

    I receive crash:

    Date/Time:             2015-03-17 12:33:14.967 +0200
    OS Version:            Mac OS X 10.10.2 (14C1510)
    Report Version:        11
    Anonymous UUID:        E3D7C826-5A6B-C198-D26A-2A630EECD882
    
    Time Awake Since Boot: 4300 seconds
    
    Crashed Thread:        0  Dispatch queue: com.apple.main-thread
    
    Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
    Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000
    
    VM Regions Near 0:
    --> 
        __TEXT                 00000001036f0000-00000001036f5000 [   20K] r-x/rwx SM=COW  /Volumes/VOLUME/*/MdlViewer.app/Contents/MacOS/MdlViewer
    
    Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
    0   libqca.2.dylib                	0x000000011305f57b QCA::ai_new(QCA::alloc_info*, int, bool) + 363
    1   libqca.2.dylib                	0x000000011305c8f9 QCA::MemoryRegion::resize(int) + 137
    2   libqca-ossl.dylib             	0x000000011319694c opensslQCAPlugin::opensslCipherContext::update(QCA::SecureArray const&, QCA::SecureArray*) + 76
    3                                 	0x000000010cb27810 QCA::Cipher::update(QCA::MemoryRegion const&) + 192 (qca_basic.cpp:353)
    4                                 	0x000000010cb278e6 non-virtual thunk to QCA::Cipher::update(QCA::MemoryRegion const&) + 54 (qca_basic.cpp:355)
    5                                 	0x000000010caaa2f6 QCA::Filter::process(QCA::MemoryRegion const&) + 118 (qca_core.cpp:1258)
    6   libMdl.dylib                  	0x000000010c83d81c MdlFileReader::decode(QString, QString, QString) + 460
    7   libMdl.dylib                  	0x000000010c83e325 MdlFileReader::startParseFile() + 709
    8   libMdl.dylib                  	0x000000010c828e37 MdlLogViewerManager::parseLogFile(QString) + 423
    9   libMdl.dylib                  	0x000000010c8436b6 MdlManager::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) + 1766
    10  libMdl.dylib                  	0x000000010c843d3a MdlManager::qt_metacall(QMetaObject::Call, int, void**) + 106
    11  org.qt-project.QtQml          	0x0000000104864ea5 CallMethod(QObject*, int, int, int, int*, QV8Engine*, QV4::CallData*) + 5541
    12  org.qt-project.QtQml          	0x00000001048634bc CallPrecise(QObject*, QQmlPropertyData const&, QV8Engine*, QV4::CallData*) + 956
    13  org.qt-project.QtQml          	0x0000000104862e9c QV4::QObjectMethod::callInternal(QV4::CallData*) + 3180
    14  org.qt-project.QtQml          	0x000000010486eaaf QV4::Runtime::callProperty(QV4::ExecutionContext*, QV4::String*, QV4::CallData*) + 671
    15  ???                           	0x000000010f6298d6 0 + 4553087190
    16  org.qt-project.QtQml          	0x000000010481e190 QV4::SimpleScriptFunction::call(QV4::Managed*, QV4::CallData*) + 528
    17  org.qt-project.QtQml          	0x000000010490445e QQmlJavaScriptExpression::evaluate(QQmlContextData*, QV4::ValueRef, QV4::CallData*, bool*) + 622
    18  org.qt-project.QtQml          	0x00000001048ab142 QQmlBoundSignalExpression::evaluate(void**) + 2258
    19  org.qt-project.QtQml          	0x00000001048abb68 QQmlBoundSignal_callback(QQmlNotifierEndpoint*, void**) + 440
    20  org.qt-project.QtQml          	0x00000001048e645c QQmlNotifier::emitNotify(QQmlNotifierEndpoint*, void**) + 92
    21  org.qt-project.QtQml          	0x0000000104887711 QQmlData::signalEmitted(QAbstractDeclarativeData*, QObject*, int, void**) + 753
    22  org.qt-project.QtCore         	0x000000010436acb7 QMetaObject::activate(QObject*, int, int, void**) + 183
    23  libqtquickcontrolsplugin.dylib	0x000000010c608285 QQuickMenuItem::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) + 517
    24  org.qt-project.QtCore         	0x000000010436b7ab QMetaObject::activate(QObject*, int, int, void**) + 2987
    25  libqtquickcontrolsplugin.dylib	0x000000010c605cfd QQuickAction::triggered(QObject*) + 61
    26  libqtquickcontrolsplugin.dylib	0x000000010c6082a1 QQuickMenuItem::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) + 545
    27  org.qt-project.QtCore         	0x0000000104364393 QObject::event(QEvent*) + 755
    28  org.qt-project.QtWidgets      	0x0000000104d2e32b QApplicationPrivate::notify_helper(QObject*, QEvent*) + 251
    29  org.qt-project.QtWidgets      	0x0000000104d31648 QApplication::notify(QObject*, QEvent*) + 8136
    30  org.qt-project.QtCore         	0x0000000104339a5b QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) + 971
    31  libqcocoa.dylib               	0x0000000107d9ab8e QCocoaEventDispatcherPrivate::processPostedEvents() + 190
    32  libqcocoa.dylib               	0x0000000107d9b411 QCocoaEventDispatcherPrivate::postedEventsSourceCallback(void*) + 33
    33  com.apple.CoreFoundation      	0x00007fff87676681 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    34  com.apple.CoreFoundation      	0x00007fff8766880d __CFRunLoopDoSources0 + 269
    35  com.apple.CoreFoundation      	0x00007fff87667e3f __CFRunLoopRun + 927
    36  com.apple.CoreFoundation      	0x00007fff87667858 CFRunLoopRunSpecific + 296
    37  com.apple.HIToolbox           	0x00007fff871e6aef RunCurrentEventLoopInMode + 235
    38  com.apple.HIToolbox           	0x00007fff871e676e ReceiveNextEventCommon + 179
    39  com.apple.HIToolbox           	0x00007fff871e66ab _BlockUntilNextEventMatchingListInModeWithFilter + 71
    40  com.apple.AppKit              	0x00007fff93c20f81 _DPSNextEvent + 964
    41  com.apple.AppKit              	0x00007fff93c20730 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 194
    42  com.apple.AppKit              	0x00007fff93c14593 -[NSApplication run] + 594
    43  libqcocoa.dylib               	0x0000000107d9a2fd QCocoaEventDispatcher::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) + 2189
    44  org.qt-project.QtCore         	0x000000010433637d QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) + 381
    45  org.qt-project.QtCore         	0x000000010433935a QCoreApplication::exec() + 346
    46  com.shav.MdlViewer            	0x00000001036f4417 main + 823
    47  com.shav.MdlViewer            	0x00000001036f40d4 start + 52
    

    What I do wrong? Thanks for the any help!



  • Ok, I found the problem. I need to rebuild QCA library.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.