Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/common/iflytek_ai_assistant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,17 @@ static const QString kFlytekService = "com.iflytek.aiassistant";

IflytekAiAssistant::IflytekAiAssistant(QObject *parent)
: QObject(parent)
, m_copilot{new QDBusInterface(kCopilotService, kCopilotPath, kCopilotInterface)}
{
qCDebug(appLog) << "IflytekAiAssistant created";
m_copilot->setTimeout(2000);
}

QSharedPointer<QDBusInterface> IflytekAiAssistant::ensureCopilot()
{
if (!m_copilot) {
m_copilot = QSharedPointer<QDBusInterface>::create(kCopilotService, kCopilotPath, kCopilotInterface);
Comment on lines +149 to +150

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Concurrent first use races

When two threads invoke checkValid() before m_copilot has been initialized, both calls can read and write m_copilot concurrently; QSharedPointer does not synchronize concurrent access to the same pointer object, so the assignments race and callers can use an invalid interface or crash.

Protect lazy initialization and subsequent access to m_copilot with synchronization, or guarantee all calls occur on the assistant's owning thread.

Prompt for AI agents
In `src/common/iflytek_ai_assistant.cpp` at lines 149-150:

**Concurrent first use races**

When two threads invoke `checkValid()` before `m_copilot` has been initialized, both calls can read and write `m_copilot` concurrently; `QSharedPointer` does not synchronize concurrent access to the same pointer object, so the assignments race and callers can use an invalid interface or crash.

Protect lazy initialization and subsequent access to `m_copilot` with synchronization, or guarantee all calls occur on the assistant's owning thread.

m_copilot->setTimeout(2000);
}
return m_copilot;
}

bool IflytekAiAssistant::hasAudioOutputDevice() const
Expand All @@ -169,6 +176,7 @@ IflytekAiAssistant *IflytekAiAssistant::instance()
IflytekAiAssistant::CallStatus IflytekAiAssistant::checkValid()
{
qCDebug(appLog) << "checkValid status:" << m_status;
ensureCopilot();
switch (m_status) {
case Invalid:
case NotInstalled:
Expand Down
2 changes: 2 additions & 0 deletions src/common/iflytek_ai_assistant.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class IflytekAiAssistant : public QObject

CallStatus stopTtsDirectlyInternal() const;

QSharedPointer<QDBusInterface> ensureCopilot();

static CallStatus copilotInstalled(const QSharedPointer<QDBusInterface> &copilot);
static CallStatus isCopilotEnabled(const QSharedPointer<QDBusInterface> &copilot);
static CallStatus launchCopilotChat(const QSharedPointer<QDBusInterface> &copilot);
Expand Down
Loading