From 82320a9ca7de3df9a612cc0d3a11014862dc8f31 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Thu, 15 Jan 2015 23:44:38 -0800 Subject: [PATCH] (API Change) Make os_get_config_path safer to use Changed: char *os_get_config_path(const char *name); To: int os_get_config_path(char *dst, size_t size, const char *name); Also added: char *os_get_config_path_ptr(const char *name); I don't like this function returning an allocation by default. Similarly to what was done with the wide character conversion functions, this function now operates on an array argument, and if you really want to just get a pointer for convenience, you use the *_ptr version of the function that clearly indicates that it's returning an allocation. --- libobs/util/platform-cocoa.m | 15 +++++++++- libobs/util/platform-nix.c | 11 +++++++- libobs/util/platform-windows.c | 20 ++++++++++++- libobs/util/platform.h | 3 +- obs/obs-app.cpp | 25 +++++++++++------ obs/window-basic-main.cpp | 51 +++++++++++++++++++++++++--------- 6 files changed, 100 insertions(+), 25 deletions(-) diff --git a/libobs/util/platform-cocoa.m b/libobs/util/platform-cocoa.m index 8c11d8a9b..2c51936fd 100644 --- a/libobs/util/platform-cocoa.m +++ b/libobs/util/platform-cocoa.m @@ -68,7 +68,20 @@ uint64_t os_gettime_ns(void) } /* gets the location ~/Library/Application Support/[name] */ -char *os_get_config_path(const char *name) +int os_get_config_path(char *dst, size_t size, const char *name) +{ + NSArray *paths = NSSearchPathForDirectoriesInDomains( + NSApplicationSupportDirectory, NSUserDomainMask, YES); + + if([paths count] == 0) + bcrash("Could not get home directory (platform-cocoa)"); + + NSString *application_support = paths[0]; + const char *base_path = [application_support UTF8String]; + return snprintf(dst, size, "%s/%s", base_path, name); +} + +char *os_get_config_path_ptr(const char *name) { NSArray *paths = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory, NSUserDomainMask, YES); diff --git a/libobs/util/platform-nix.c b/libobs/util/platform-nix.c index 5dda75b47..efdc615f2 100644 --- a/libobs/util/platform-nix.c +++ b/libobs/util/platform-nix.c @@ -154,7 +154,16 @@ uint64_t os_gettime_ns(void) } /* should return $HOME/.[name] */ -char *os_get_config_path(const char *name) +int os_get_config_path(char *dst, size_t size, const char *name) +{ + char *path_ptr = getenv("HOME"); + if (path_ptr == NULL) + bcrash("Could not get $HOME\n"); + + return snprintf(dst, size, "%s/.%s", path_ptr, name); +} + +char *os_get_config_path_ptr(const char *name) { char *path_ptr = getenv("HOME"); if (path_ptr == NULL) diff --git a/libobs/util/platform-windows.c b/libobs/util/platform-windows.c index 1b4cd5a4f..0db7acd1b 100644 --- a/libobs/util/platform-windows.c +++ b/libobs/util/platform-windows.c @@ -191,7 +191,25 @@ uint64_t os_gettime_ns(void) } /* returns %appdata%\[name] on windows */ -char *os_get_config_path(const char *name) +int os_get_config_path(char *dst, size_t size, const char *name) +{ + wchar_t path_utf16[MAX_PATH]; + + SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, + path_utf16); + + if (os_wcs_to_utf8(path_utf16, 0, dst, size) != 0) { + if (strcat_s(dst, size, "\\") == 0) { + if (strcat_s(dst, size, name) == 0) { + return (int)strlen(dst); + } + } + } + + return -1; +} + +char *os_get_config_path_ptr(const char *name) { char *ptr; wchar_t path_utf16[MAX_PATH]; diff --git a/libobs/util/platform.h b/libobs/util/platform.h index a5298c5da..f0786e013 100644 --- a/libobs/util/platform.h +++ b/libobs/util/platform.h @@ -90,7 +90,8 @@ EXPORT void os_sleep_ms(uint32_t duration); EXPORT uint64_t os_gettime_ns(void); -EXPORT char *os_get_config_path(const char *name); +EXPORT int os_get_config_path(char *dst, size_t size, const char *name); +EXPORT char *os_get_config_path_ptr(const char *name); EXPORT bool os_file_exists(const char *path); diff --git a/obs/obs-app.cpp b/obs/obs-app.cpp index ec4b48a5b..a13eb0f5f 100644 --- a/obs/obs-app.cpp +++ b/obs/obs-app.cpp @@ -127,17 +127,20 @@ static bool do_mkdir(const char *path) static bool MakeUserDirs() { - BPtr path; + char path[512]; - path = os_get_config_path("obs-studio"); + if (os_get_config_path(path, sizeof(path), "obs-studio") <= 0) + return false; if (!do_mkdir(path)) return false; - path = os_get_config_path("obs-studio/basic"); + if (os_get_config_path(path, sizeof(path), "obs-studio/basic") <= 0) + return false; if (!do_mkdir(path)) return false; - path = os_get_config_path("obs-studio/logs"); + if (os_get_config_path(path, sizeof(path), "obs-studio/logs") <= 0) + return false; if (!do_mkdir(path)) return false; @@ -146,7 +149,13 @@ static bool MakeUserDirs() bool OBSApp::InitGlobalConfig() { - BPtr path(os_get_config_path("obs-studio/global.ini")); + char path[512]; + + int len = os_get_config_path(path, sizeof(path), + "obs-studio/global.ini"); + if (len <= 0) { + return false; + } int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS); if (errorcode != CONFIG_SUCCESS) { @@ -408,7 +417,7 @@ static uint64_t convert_log_name(const char *name) static void delete_oldest_log(void) { - BPtr logDir(os_get_config_path("obs-studio/logs")); + BPtr logDir(os_get_config_path_ptr("obs-studio/logs")); string oldestLog; uint64_t oldest_ts = (uint64_t)-1; struct os_dirent *entry; @@ -449,7 +458,7 @@ static void delete_oldest_log(void) static void get_last_log(void) { - BPtr logDir(os_get_config_path("obs-studio/logs")); + BPtr logDir(os_get_config_path_ptr("obs-studio/logs")); struct os_dirent *entry; os_dir_t *dir = os_opendir(logDir); uint64_t highest_ts = 0; @@ -522,7 +531,7 @@ static void create_log_file(fstream &logFile) currentLogFile = GenerateTimeDateFilename("txt"); dst << "obs-studio/logs/" << currentLogFile.c_str(); - BPtr path(os_get_config_path(dst.str().c_str())); + BPtr path(os_get_config_path_ptr(dst.str().c_str())); logFile.open(path, ios_base::in | ios_base::out | ios_base::trunc); diff --git a/obs/window-basic-main.cpp b/obs/window-basic-main.cpp index 6cd6c3804..450576ee9 100644 --- a/obs/window-basic-main.cpp +++ b/obs/window-basic-main.cpp @@ -62,10 +62,11 @@ Q_DECLARE_METATYPE(obs_order_movement); static void AddExtraModulePaths() { - BPtr base_module_dir = - os_get_config_path("obs-studio/plugins/%module%"); + char base_module_dir[512]; + int ret = os_get_config_path(base_module_dir, sizeof(base_module_dir), + "obs-studio/plugins/%module%"); - if (!base_module_dir) + if (ret <= 0) return; string path = (char*)base_module_dir; @@ -319,8 +320,10 @@ void OBSBasic::SaveService() if (!service) return; - BPtr serviceJsonPath(os_get_config_path(SERVICE_PATH)); - if (!serviceJsonPath) + char serviceJsonPath[512]; + int ret = os_get_config_path(serviceJsonPath, sizeof(serviceJsonPath), + SERVICE_PATH); + if (ret <= 0) return; obs_data_t *data = obs_data_create(); @@ -341,8 +344,10 @@ bool OBSBasic::LoadService() { const char *type; - BPtr serviceJsonPath(os_get_config_path(SERVICE_PATH)); - if (!serviceJsonPath) + char serviceJsonPath[512]; + int ret = os_get_config_path(serviceJsonPath, sizeof(serviceJsonPath), + SERVICE_PATH); + if (ret <= 0) return false; BPtr jsonText = os_quick_read_utf8_file(serviceJsonPath); @@ -498,7 +503,13 @@ bool OBSBasic::InitBasicConfigDefaults() bool OBSBasic::InitBasicConfig() { - BPtr configPath(os_get_config_path("obs-studio/basic/basic.ini")); + char configPath[512]; + int ret = os_get_config_path(configPath, sizeof(configPath), + "obs-studio/basic/basic.ini"); + if (ret <= 0) { + OBSErrorBox(nullptr, "Failed to get base.ini path"); + return false; + } int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS); if (code != CONFIG_SUCCESS) { @@ -549,7 +560,11 @@ void OBSBasic::InitPrimitives() void OBSBasic::OBSInit() { - BPtr savePath(os_get_config_path("obs-studio/basic/scenes.json")); + char savePath[512]; + int ret = os_get_config_path(savePath, sizeof(savePath), + "obs-studio/basic/scenes.json"); + if (ret <= 0) + throw "Failed to get scenes.json file path"; /* make sure it's fully displayed before doing any initialization */ show(); @@ -562,7 +577,7 @@ void OBSBasic::OBSInit() if (!ResetAudio()) throw "Failed to initialize audio"; - int ret = ResetVideo(); + ret = ResetVideo(); switch (ret) { case OBS_VIDEO_MODULE_NOT_FOUND: @@ -646,7 +661,12 @@ OBSBasic::~OBSBasic() void OBSBasic::SaveProject() { - BPtr savePath(os_get_config_path("obs-studio/basic/scenes.json")); + char savePath[512]; + int ret = os_get_config_path(savePath, sizeof(savePath), + "obs-studio/basic/scenes.json"); + if (ret <= 0) + return; + SaveService(); Save(savePath); } @@ -1966,7 +1986,9 @@ void OBSBasic::on_actionMoveToBottom_triggered() static BPtr ReadLogFile(const char *log) { - BPtr logDir(os_get_config_path("obs-studio/logs")); + char logDir[512]; + if (os_get_config_path(logDir, sizeof(logDir), "obs-studio/logs") <= 0) + return nullptr; string path = (char*)logDir; path += "/"; @@ -2033,7 +2055,10 @@ void OBSBasic::UploadLog(const char *file) void OBSBasic::on_actionShowLogs_triggered() { - BPtr logDir(os_get_config_path("obs-studio/logs")); + char logDir[512]; + if (os_get_config_path(logDir, sizeof(logDir), "obs-studio/logs") <= 0) + return; + QUrl url = QUrl::fromLocalFile(QT_UTF8(logDir)); QDesktopServices::openUrl(url); } -- GitLab