summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-05-27 20:47:35 +0200
committerStanislaw Halik <sthalik@misaki.pl>2023-05-28 05:13:47 +0200
commit71dbcd16b2b54c695eae1395d65129b037983d0f (patch)
treefff62e1fc7b4e52fb0c67f811a97bb0882e52112
parent94e7374bad0da0d9d132401bd57526b509497c7d (diff)
gui/init: fix crash on startup
Fixes: #1661 Reported, diagnosed and tested by: @wfurney13
-rw-r--r--gui/init.cpp78
1 files changed, 42 insertions, 36 deletions
diff --git a/gui/init.cpp b/gui/init.cpp
index b54a085e..5984269c 100644
--- a/gui/init.cpp
+++ b/gui/init.cpp
@@ -193,51 +193,56 @@ static void apply_dark_windows_theme_if_needed()
static void add_win32_path()
{
- // see https://software.intel.com/en-us/articles/limitation-to-the-length-of-the-system-path-variable
- static char env_path[4096] { '\0', };
+ // see https://web.archive.org/web/20180924055536/https://software.intel.com/en-us/articles/limitation-to-the-length-of-the-system-path-variable
{
QString lib_path = OPENTRACK_BASE_PATH;
lib_path.replace("/", "\\");
- const QByteArray lib_path_ = QFile::encodeName(lib_path);
-
QString mod_path = OPENTRACK_BASE_PATH + OPENTRACK_LIBRARY_PATH;
mod_path.replace("/", "\\");
- const QByteArray mod_path_ = QFile::encodeName(mod_path);
-
- const char* contents[] {
- "PATH=",
- lib_path_.constData(),
- ";",
- mod_path_.constData(),
- ";",
- getenv("PATH"),
- };
- bool ok = true;
+ const QString orig_path = qgetenv("PATH");
- for (const char* ptr : contents)
- {
- if (ptr)
- strcat_s(env_path, sizeof(env_path), ptr);
+ QString env_path; env_path.reserve(4096);
- if (!ptr || ptr[0] == '\0' || env_path[0] == '\0')
- {
- qDebug() << "bad path element"
- << (ptr == nullptr ? "<null>" : ptr);
- ok = false;
- break;
- }
- }
+#if 0
+ qDebug() << "orig" << orig_path;
+ qDebug() << "libpath" << lib_path;
+ qDebug() << "modpath" << mod_path;
+#endif
- if (ok)
+ if (lib_path.isEmpty())
+ qDebug() << "env: empty lib_path!";
+ else
{
- const int error = _putenv(env_path);
-
- if (error)
- qDebug() << "can't _putenv win32 path";
+ if (!QFile(lib_path).exists())
+ qDebug() << "env: lib_path doesn't exist, this shouldn't happen!";
+ env_path += lib_path;
+ env_path += ';';
}
+ if (mod_path.isEmpty())
+ qDebug() << "env: can't add mod_path to env PATH";
else
- qDebug() << "can't set win32 path";
+ {
+ if (!QFile(mod_path).exists())
+ qDebug() << "env: mod_path doesn't exist, did you install it correctly?";
+ env_path += mod_path;
+ env_path += ';';
+ }
+
+ if (orig_path.isEmpty())
+ qDebug() << "env: empty PATH";
+ else
+ env_path += orig_path;
+
+#if 0
+ qDebug() << "data" << env_path.constData();
+#endif
+
+ // better length limit than putenv() and SetEnvironmentVariableA
+ bool ret = SetEnvironmentVariableW(L"PATH", (const wchar_t*)env_path.constData());
+
+ if (!ret)
+ qDebug() << "_putenv() failed with" << (void*)GetLastError();
}
}
@@ -300,11 +305,13 @@ int otr_main(int argc, char** argv, std::function<std::unique_ptr<QWidget>()> co
QApplication app(argc, argv);
#ifdef _WIN32
- apply_dark_windows_theme_if_needed();
- add_win32_path();
attach_parent_console();
#endif
(void)qInstallMessageHandler(qdebug_to_console);
+#ifdef _WIN32
+ apply_dark_windows_theme_if_needed();
+ add_win32_path();
+#endif
QDir::setCurrent(OPENTRACK_BASE_PATH);
@@ -345,4 +352,3 @@ int otr_main(int argc, char** argv, std::function<std::unique_ptr<QWidget>()> co
return ret;
}
-