From 41989da7326b82eda5e25130457ad64f8333f112 Mon Sep 17 00:00:00 2001 From: Ted John Date: Tue, 6 Jun 2017 23:42:28 +0100 Subject: [PATCH 1/2] Fix #4959: Compiler warning: tmpnam Load download parks directly from memory without writing to a temporary file. --- src/openrct2/Context.cpp | 17 ++++++++------- src/openrct2/FileClassifier.cpp | 7 +++++-- src/openrct2/network/http.cpp | 37 ++++++++++++--------------------- src/openrct2/network/http.h | 10 +++++++-- 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index ed17a64208..4397536bdf 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -24,6 +24,7 @@ #include "core/File.h" #include "core/FileStream.hpp" #include "core/Guard.hpp" +#include "core/MemoryStream.h" #include "core/String.hpp" #include "FileClassifier.h" #include "network/network.h" @@ -322,14 +323,16 @@ namespace OpenRCT2 { #ifndef DISABLE_HTTP // Download park and open it using its temporary filename - char tmpPath[MAX_PATH]; - if (!http_download_park(gOpenRCT2StartupActionPath, tmpPath)) + void * data; + size_t dataSize = http_download_park(gOpenRCT2StartupActionPath, &data); + if (dataSize == 0) { title_load(); break; } - parkLoaded = OpenParkAutoDetectFormat(tmpPath); + auto ms = MemoryStream(data, dataSize, MEMORY_ACCESS::OWNER); + parkLoaded = OpenParkAutoDetectFormat(&ms); #endif } else @@ -491,10 +494,10 @@ namespace OpenRCT2 sprite_position_tween_restore(); } - bool OpenParkAutoDetectFormat(const utf8 * path) + bool OpenParkAutoDetectFormat(IStream * stream) { ClassifiedFile info; - if (TryClassifyFile(path, &info)) + if (TryClassifyFile(stream, &info)) { if (info.Type == FILE_TYPE::SAVED_GAME || info.Type == FILE_TYPE::SCENARIO) @@ -513,7 +516,7 @@ namespace OpenRCT2 { try { - parkImporter->LoadSavedGame(path); + parkImporter->LoadFromStream(stream, false); parkImporter->Import(); game_fix_save_vars(); sprite_position_tween_reset(); @@ -531,7 +534,7 @@ namespace OpenRCT2 { try { - parkImporter->LoadScenario(path); + parkImporter->LoadFromStream(stream, true); parkImporter->Import(); game_fix_save_vars(); sprite_position_tween_reset(); diff --git a/src/openrct2/FileClassifier.cpp b/src/openrct2/FileClassifier.cpp index d10dd82e79..eeb1a49b25 100644 --- a/src/openrct2/FileClassifier.cpp +++ b/src/openrct2/FileClassifier.cpp @@ -71,6 +71,8 @@ bool TryClassifyFile(IStream * stream, ClassifiedFile * result) static bool TryClassifyAsS6(IStream * stream, ClassifiedFile * result) { + bool success = false; + uint64 originalPosition = stream->GetPosition(); try { auto chunkReader = SawyerChunkReader(stream); @@ -84,12 +86,13 @@ static bool TryClassifyAsS6(IStream * stream, ClassifiedFile * result) result->Type = FILE_TYPE::SCENARIO; } result->Version = s6Header.version; - return true; + success = true; } catch (Exception) { } - return false; + stream->SetPosition(originalPosition); + return success; } static bool TryClassifyAsS4(IStream * stream, ClassifiedFile * result) diff --git a/src/openrct2/network/http.cpp b/src/openrct2/network/http.cpp index 3f422b0fc0..74c0b1a035 100644 --- a/src/openrct2/network/http.cpp +++ b/src/openrct2/network/http.cpp @@ -292,7 +292,7 @@ const char *http_get_extension_from_url(const char *url, const char *fallback) } } -bool http_download_park(const char *url, char tmpPath[L_tmpnam + 10]) +size_t http_download_park(const char * url, void * * outData) { // Download park to buffer in memory HttpRequest2 request; @@ -307,35 +307,24 @@ bool http_download_park(const char *url, char tmpPath[L_tmpnam + 10]) if (response != NULL) { http_request_dispose(response); } - return false; + + *outData = NULL; + return 0; } - // Generate temporary filename that includes the original extension - if (tmpnam(tmpPath) == NULL) { - Console::Error::WriteLine("Failed to generate temporary filename for downloaded park '%s'", request.Url.c_str()); - http_request_dispose(response); - return false; + size_t dataSize = response->size - 1; + void * data = malloc(dataSize); + if (data == NULL) { + dataSize = 0; + Console::Error::WriteLine("Failed to allocate memory for downloaded park."); + } else { + memcpy(data, response->body, dataSize); } - size_t remainingBytes = L_tmpnam + 10 - strlen(tmpPath); - - const char *ext = http_get_extension_from_url(request.Url.c_str(), ".sv6"); - strncat(tmpPath, ext, remainingBytes); - - // Store park in temporary file and load it (discard ending NUL in response body) - FILE* tmpFile = fopen(tmpPath, "wb"); - - if (tmpFile == NULL) { - Console::Error::WriteLine("Failed to write downloaded park '%s' to temporary file", request.Url.c_str()); - http_request_dispose(response); - return false; - } - - fwrite(response->body, 1, response->size - 1, tmpFile); - fclose(tmpFile); http_request_dispose(response); - return true; + *outData = data; + return dataSize; } #endif diff --git a/src/openrct2/network/http.h b/src/openrct2/network/http.h index 44b8dd22c4..10f1099cc6 100644 --- a/src/openrct2/network/http.h +++ b/src/openrct2/network/http.h @@ -60,8 +60,14 @@ void http_request_dispose(http_response_t *response); const char *http_get_extension_from_url(const char *url, const char *fallback); -// Padding for extension that is appended to temporary file name -bool http_download_park(const char *url, char tmpPath[L_tmpnam + 10]); +/** + * Download a park via HTTP/S from the given URL into a memory buffer. This is + * a blocking operation. + * @param url The URL to download the park from. + * @param outData The data returned. + * @returns The size of the data or 0 if the download failed. + */ +size_t http_download_park(const char * url, void * * outData); #endif // DISABLE_HTTP // These callbacks are defined anyway, but are dummy if HTTP is disabled From d31870249a2103e68707ee1898b8121fd1a5d9ca Mon Sep 17 00:00:00 2001 From: Ted John Date: Tue, 6 Jun 2017 23:45:04 +0100 Subject: [PATCH 2/2] macOS: Unignore deprecation warning --- src/openrct2/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/openrct2/CMakeLists.txt b/src/openrct2/CMakeLists.txt index 47b24c10b0..4a17e487a8 100644 --- a/src/openrct2/CMakeLists.txt +++ b/src/openrct2/CMakeLists.txt @@ -226,9 +226,8 @@ if (NOT DISABLE_TTF AND UNIX AND NOT APPLE) target_include_directories(${PROJECT} PRIVATE ${FONTCONFIG_INCLUDE_DIRS}) endif () -# macOS builds fail on the use of tmpnam otherwise (#4959) if(APPLE) - set(COMMON_COMPILE_OPTIONS "${COMMON_COMPILE_OPTIONS} -Wno-error=deprecated-declarations -Wno-error=objc-method-access") + set(COMMON_COMPILE_OPTIONS "${COMMON_COMPILE_OPTIONS} -Wno-error=objc-method-access") endif() # Compiler flags