Repository: nomic-ai/gpt4all
File: gpt4all-chat/src/chatllm.cpp (C++)

Code before:
    }
    stream << quint64(m_ctx.tokens.size());
    stream.writeRawData(reinterpret_cast<const char*>(m_ctx.tokens.data()), m_ctx.tokens.size() * sizeof(int));
    saveState();
    QByteArray compressed = qCompress(m_state);
    stream << compressed;
#if defined(DEBUG)
    qDebug() << "serialize" << m_llmThread.objectName() << m_state.size();
#endif
    return stream.status() == QDataStream::Ok;
}

bool ChatLLM::deserialize(QDataStream &stream, int version, bool deserializeKV, bool discardKV)
{
    if (version >= 2) {
        int llModelType;
        stream >> llModelType;
        m_llModelType = (version >= 6 ? parseLLModelTypeV1 : parseLLModelTypeV0)(llModelType);
        if (m_llModelType == LLModelTypeV1::NONE) {
            qWarning().nospace() << "error loading chat id " << m_chat->id() << ": unrecognized model type: "
                                 << llModelType;
            return false;
        }

        /* note: prior to chat version 10, API models and chats with models removed in v2.5.0 only wrote this because of
         * UB in Release builds */
        int internalStateVersion; // for future use
        stream >> internalStateVersion;
    }
    QString response;
    stream >> response;
    m_response = response.toStdString();
    m_trimmedResponse = trim_whitespace(m_response);
    QString nameResponse;
    stream >> nameResponse;
    m_nameResponse = nameResponse.toStdString();
    stream >> m_promptResponseTokens;

    // If we do not deserialize the KV or it is discarded, then we need to restore the state from the
    // text only. This will be a costly operation, but the chat has to be restored from the text archive
    // alone.
    if (!deserializeKV || discardKV) {
        m_restoreStateFromText = true;
        m_pristineLoadedState = true;
    }

    if (!deserializeKV) {
#if defined(DEBUG)
        qDebug() << "deserialize" << m_llmThread.objectName();
#endif
        return stream.status() == QDataStream::Ok;

Review comment (bug):
```suggestion
         * undefined behavior in Release builds */
```

Code after:
    }
    stream << quint64(m_ctx.tokens.size());
    stream.writeRawData(reinterpret_cast<const char*>(m_ctx.tokens.data()), m_ctx.tokens.size() * sizeof(int));
    saveState();
    QByteArray compressed = qCompress(m_state);
    stream << compressed;
#if defined(DEBUG)
    qDebug() << "serialize" << m_llmThread.objectName() << m_state.size();
#endif
    return stream.status() == QDataStream::Ok;
}

bool ChatLLM::deserialize(QDataStream &stream, int version, bool deserializeKV, bool discardKV)
{
    if (version >= 2) {
        int llModelType;
        stream >> llModelType;
        m_llModelType = (version >= 6 ? parseLLModelTypeV1 : parseLLModelTypeV0)(llModelType);
        if (m_llModelType == LLModelTypeV1::NONE) {
            qWarning().nospace() << "error loading chat id " << m_chat->id() << ": unrecognized model type: "
                                 << llModelType;
            return false;
        }

        /* note: prior to chat version 10, API models and chats with models removed in v2.5.0 only wrote this because of
         * undefined behavior in Release builds */
        int internalStateVersion; // for future use
        stream >> internalStateVersion;
    }
    QString response;
    stream >> response;
    m_response = response.toStdString();
    m_trimmedResponse = trim_whitespace(m_response);
    QString nameResponse;
    stream >> nameResponse;
    m_nameResponse = nameResponse.toStdString();
    stream >> m_promptResponseTokens;

    // If we do not deserialize the KV or it is discarded, then we need to restore the state from the
    // text only. This will be a costly operation, but the chat has to be restored from the text archive
    // alone.
    if (!deserializeKV || discardKV) {
        m_restoreStateFromText = true;
        m_pristineLoadedState = true;
    }

    if (!deserializeKV) {
#if defined(DEBUG)
        qDebug() << "deserialize" << m_llmThread.objectName();
#endif
        return stream.status() == QDataStream::Ok;