Fix test errors by guarding against null dereference of wxTheApp in OrcaCloudServiceAgent::compute_fallback_path() and skipping file persistence when no fallback path is available.

This commit is contained in:
SoftFever
2026-06-15 14:29:30 +08:00
parent b1e510fc99
commit 14d2dfdd4c

View File

@@ -31,6 +31,7 @@
#include <wx/filefn.h> #include <wx/filefn.h>
#include <wx/secretstore.h> #include <wx/secretstore.h>
#include <wx/stdpaths.h> #include <wx/stdpaths.h>
#include <wx/app.h>
#include <wx/utils.h> #include <wx/utils.h>
#if defined(_WIN32) #if defined(_WIN32)
@@ -1416,6 +1417,10 @@ void OrcaCloudServiceAgent::persist_refresh_token(const std::string& token)
} }
compute_fallback_path(); compute_fallback_path();
if (refresh_fallback_path.empty()) {
BOOST_LOG_TRIVIAL(warning) << "OrcaCloudServiceAgent: no refresh-token storage path available; skipping file persistence";
return;
}
wxFileName path(wxString::FromUTF8(refresh_fallback_path.c_str())); wxFileName path(wxString::FromUTF8(refresh_fallback_path.c_str()));
path.Normalize(); path.Normalize();
if (!wxFileName::DirExists(path.GetPath())) { if (!wxFileName::DirExists(path.GetPath())) {
@@ -2219,7 +2224,15 @@ bool OrcaCloudServiceAgent::http_post_auth(const std::string& path, const std::s
void OrcaCloudServiceAgent::compute_fallback_path() void OrcaCloudServiceAgent::compute_fallback_path()
{ {
if (!refresh_fallback_path.empty()) return; if (!refresh_fallback_path.empty())
return;
// wxStandardPaths::GetUserDataDir() resolves the app data directory via
// wxAppConsoleBase::GetAppName(), which dereferences wxTheApp. In headless
// contexts (CLI, unit tests) there is no wxApp, so guard the call to avoid a
// null dereference. The path can still be provided explicitly through
// set_config_dir(); when it is left empty, file persistence is skipped.
if (wxTheApp == nullptr)
return;
wxFileName fallback(wxStandardPaths::Get().GetUserDataDir(), "orca_refresh_token.sec"); wxFileName fallback(wxStandardPaths::Get().GetUserDataDir(), "orca_refresh_token.sec");
fallback.Normalize(); fallback.Normalize();
refresh_fallback_path = fallback.GetFullPath().ToStdString(); refresh_fallback_path = fallback.GetFullPath().ToStdString();