diff --git a/runtime/shims/xgameruntime.cpp b/runtime/shims/xgameruntime.cpp index 2b44229be4..d79021afa1 100644 --- a/runtime/shims/xgameruntime.cpp +++ b/runtime/shims/xgameruntime.cpp @@ -522,7 +522,11 @@ HRESULT STDMETHODCALLTYPE pls_get_path(void*, size_t path_size, char* path, size if (!path) return E_POINTER; const std::string folder = utf8_of(persistent_storage_dir().native()); if (folder.empty()) return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND); - if (path_size < folder.size() + 1) return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); + if (path_size < folder.size() + 1) { + // A short buffer is left untouched but learns the size it needs: the path with its NUL, as slot 3 reports. + if (path_used) *path_used = folder.size() + 1; + return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); + } std::memcpy(path, folder.c_str(), folder.size() + 1); if (path_used) *path_used = folder.size() + 1; return S_OK; diff --git a/runtime/test/xgameruntime_test.cpp b/runtime/test/xgameruntime_test.cpp index af67045d42..0cba9ebd4c 100644 --- a/runtime/test/xgameruntime_test.cpp +++ b/runtime/test/xgameruntime_test.cpp @@ -579,8 +579,13 @@ void check_run_time_load() { char tiny[2] = {'x', 'x'}; used = 99; check(reinterpret_cast(slot(pls, 4))(pls, 2, tiny, &used) == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) && - used == 0, - "slot 4 refuses a buffer that is too small"); + used == size && tiny[0] == 'x' && tiny[1] == 'x', + "slot 4 refuses a buffer that is too small, reports the size it needs and leaves the buffer alone"); + check(reinterpret_cast(slot(pls, 4))(pls, size - 1, path.data(), &used) == + HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) && used == size, + "slot 4 reports the size for a buffer one byte short"); + check(reinterpret_cast(slot(pls, 4))(pls, 2, tiny, nullptr) == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), + "slot 4 tolerates a null used pointer on a short buffer"); { std::ofstream cache_file(folder / L"cache.bin", std::ios::binary);