From 1cdbd1e4592de30ac3329620fb1be0572fdb7c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Sat, 28 May 2016 10:31:02 +0200 Subject: [PATCH] Take care not to overflow buffers in platform_resolve_user_data_path (#3761) --- src/platform/posix.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/platform/posix.c b/src/platform/posix.c index ec9f69f7c1..f6a54f1781 100644 --- a/src/platform/posix.c +++ b/src/platform/posix.c @@ -666,16 +666,24 @@ void platform_resolve_user_data_path() log_error("Failed to create directory \"%s\", make sure you have permissions.", gCustomUserDataPath); return; } - if (realpath(gCustomUserDataPath, _userDataDirectoryPath) == NULL) { + char *path; + if ((path = realpath(gCustomUserDataPath, NULL)) == NULL) { log_error("Could not resolve path \"%s\"", gCustomUserDataPath); return; } + safe_strcpy(_userDataDirectoryPath, path, MAX_PATH); + free(path); + // Ensure path ends with separator int len = strlen(_userDataDirectoryPath); if (_userDataDirectoryPath[len - 1] != separator[0]) { strncat(_userDataDirectoryPath, separator, MAX_PATH - 1); } + log_verbose("User data path resolved to: %s", _userDataDirectoryPath); + if (!platform_directory_exists(_userDataDirectoryPath)) { + log_error("Custom user data directory %s does not exist", _userDataDirectoryPath); + } return; } @@ -694,6 +702,7 @@ void platform_resolve_user_data_path() free(w_buffer); safe_strcpy(_userDataDirectoryPath, path, MAX_PATH); free(path); + log_verbose("User data path resolved to: %s", _userDataDirectoryPath); } void platform_get_openrct_data_path(utf8 *outPath)