From aa90545b890bc77c9a21bd8dc1a596518cf731af Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 4 Mar 2021 19:19:40 -0800 Subject: [PATCH 1/7] Fix warning on Windows compiler for System.IO.Compression.Native --- .../Windows/System.IO.Compression.Native/zlib-intel/inflate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Native/Windows/System.IO.Compression.Native/zlib-intel/inflate.c b/src/libraries/Native/Windows/System.IO.Compression.Native/zlib-intel/inflate.c index 165e465a08471..4a2a217a3e62a 100644 --- a/src/libraries/Native/Windows/System.IO.Compression.Native/zlib-intel/inflate.c +++ b/src/libraries/Native/Windows/System.IO.Compression.Native/zlib-intel/inflate.c @@ -1540,7 +1540,7 @@ z_streamp source; struct inflate_state FAR *state; struct inflate_state FAR *copy; unsigned char FAR *window; - unsigned wsize; + unsigned wsize = 0; /* check input */ if (inflateStateCheck(source) || dest == Z_NULL) From 86db20da7f5612aae50db7080e2a779c77f9830f Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 4 Mar 2021 20:59:38 -0800 Subject: [PATCH 2/7] Fix warnings in corehost when more analysis are enabled. --- src/native/corehost/bundle/extractor.cpp | 6 +++-- src/native/corehost/bundle/reader.h | 3 ++- src/native/corehost/deps_format.cpp | 6 ++--- src/native/corehost/deps_format.h | 2 +- src/native/corehost/fxr/corehost_init.cpp | 10 ++++++-- src/native/corehost/fxr/fx_muxer.cpp | 10 ++++---- src/native/corehost/fxr/hostfxr.cpp | 6 ++--- src/native/corehost/hostmisc/pal.h | 19 +++++++++++---- src/native/corehost/hostmisc/pal.windows.cpp | 23 +++++++++++-------- src/native/corehost/hostmisc/utils.cpp | 4 ++-- src/native/corehost/hostmisc/utils.h | 2 +- .../corehost/hostpolicy/deps_resolver.cpp | 6 ++--- .../corehost/hostpolicy/deps_resolver.h | 2 +- src/native/corehost/hostpolicy/hostpolicy.cpp | 4 ++-- .../corehost/hostpolicy/hostpolicy_init.cpp | 4 ++-- src/native/corehost/json_parser.cpp | 2 +- .../test/nativehost/host_context_test.cpp | 4 ++-- .../resolve_component_dependencies_test.cpp | 2 +- 18 files changed, 68 insertions(+), 47 deletions(-) diff --git a/src/native/corehost/bundle/extractor.cpp b/src/native/corehost/bundle/extractor.cpp index 82a47968685cb..8ac1147e22a1a 100644 --- a/src/native/corehost/bundle/extractor.cpp +++ b/src/native/corehost/bundle/extractor.cpp @@ -103,9 +103,11 @@ void extractor_t::extract(const file_entry_t &entry, reader_t &reader) { FILE* file = create_extraction_file(entry.relative_path()); reader.set_offset(entry.offset()); - size_t size = entry.size(); + int64_t size = entry.size(); + assert(size < static_cast(std::numeric_limits::max())); - if (fwrite(reader, 1, size, file) != size) + size_t cast_size = static_cast(size); + if (fwrite(reader, 1, cast_size, file) != cast_size) { trace::error(_X("Failure extracting contents of the application bundle.")); trace::error(_X("I/O failure when writing extracted files.")); diff --git a/src/native/corehost/bundle/reader.h b/src/native/corehost/bundle/reader.h index 872b04369025e..2306f3d536d6d 100644 --- a/src/native/corehost/bundle/reader.h +++ b/src/native/corehost/bundle/reader.h @@ -40,7 +40,8 @@ namespace bundle void read(void* dest, int64_t len) { bounds_check(len); - memcpy(dest, m_ptr, len); + assert(len < static_cast(std::numeric_limits::max())); + memcpy(dest, m_ptr, static_cast(len)); m_ptr += len; } diff --git a/src/native/corehost/deps_format.cpp b/src/native/corehost/deps_format.cpp index e593c73df0c91..e60ccd59fbb38 100644 --- a/src/native/corehost/deps_format.cpp +++ b/src/native/corehost/deps_format.cpp @@ -45,7 +45,7 @@ void deps_json_t::reconcile_libraries_with_targets( const pal::string_t& deps_path, const json_parser_t::value_t& json, const std::function& library_exists_fn, - const std::function& get_assets_fn) + const std::function& get_assets_fn) { pal::string_t deps_file = get_filename(deps_path); @@ -320,7 +320,7 @@ bool deps_json_t::load_framework_dependent(const pal::string_t& deps_path, const }; const vec_asset_t empty; - auto get_relpaths = [&](const pal::string_t& package, int asset_type_index, bool* rid_specific) -> const vec_asset_t& { + auto get_relpaths = [&](const pal::string_t& package, size_t asset_type_index, bool* rid_specific) -> const vec_asset_t& { *rid_specific = false; @@ -361,7 +361,7 @@ bool deps_json_t::load_self_contained(const pal::string_t& deps_path, const json return m_assets.libs.count(package); }; - auto get_relpaths = [&](const pal::string_t& package, int type_index, bool* rid_specific) -> const vec_asset_t& { + auto get_relpaths = [&](const pal::string_t& package, size_t type_index, bool* rid_specific) -> const vec_asset_t& { *rid_specific = false; return m_assets.libs[package][type_index]; }; diff --git a/src/native/corehost/deps_format.h b/src/native/corehost/deps_format.h index a9d9a2a2cbeed..ceed3147e081d 100644 --- a/src/native/corehost/deps_format.h +++ b/src/native/corehost/deps_format.h @@ -92,7 +92,7 @@ class deps_json_t const pal::string_t& deps_path, const json_parser_t::value_t& json, const std::function& library_exists_fn, - const std::function& get_assets_fn); + const std::function& get_assets_fn); pal::string_t get_current_rid(const rid_fallback_graph_t& rid_fallback_graph); bool perform_rid_fallback(rid_specific_assets_t* portable_assets, const rid_fallback_graph_t& rid_fallback_graph); diff --git a/src/native/corehost/fxr/corehost_init.cpp b/src/native/corehost/fxr/corehost_init.cpp index ab7057babd034..b50e2f57156ba 100644 --- a/src/native/corehost/fxr/corehost_init.cpp +++ b/src/native/corehost/fxr/corehost_init.cpp @@ -35,7 +35,7 @@ corehost_init_t::corehost_init_t( { make_cstr_arr(m_probe_paths, &m_probe_paths_cstr); - int fx_count = fx_definitions.size(); + size_t fx_count = fx_definitions.size(); m_fx_names.reserve(fx_count); m_fx_dirs.reserve(fx_count); m_fx_requested_versions.reserve(fx_count); @@ -132,7 +132,13 @@ const host_interface_t& corehost_init_t::get_host_init_data() hi.host_info_dotnet_root = m_host_info_dotnet_root.c_str(); hi.host_info_app_path = m_host_info_app_path.c_str(); - hi.single_file_bundle_header_offset = bundle::info_t::is_single_file_bundle() ? bundle::info_t::the_app->header_offset() : 0; + hi.single_file_bundle_header_offset = 0; + if (bundle::info_t::is_single_file_bundle()) + { + int64_t offset = bundle::info_t::the_app->header_offset(); + assert(offset < static_cast(std::numeric_limits::max())); + hi.single_file_bundle_header_offset = static_cast(offset); + } return hi; } diff --git a/src/native/corehost/fxr/fx_muxer.cpp b/src/native/corehost/fxr/fx_muxer.cpp index 53a65ea67f8aa..35673fd19d121 100644 --- a/src/native/corehost/fxr/fx_muxer.cpp +++ b/src/native/corehost/fxr/fx_muxer.cpp @@ -849,7 +849,7 @@ int fx_muxer_t::run_app(host_context_t *context) if (!context->is_app) return StatusCode::InvalidArgFailure; - int argc = context->argv.size(); + size_t argc = context->argv.size(); std::vector argv; argv.reserve(argc); for (const auto& str : context->argv) @@ -863,7 +863,7 @@ int fx_muxer_t::run_app(host_context_t *context) if (rc != StatusCode::Success) return rc; - return contract.run_app(argc, argv.data()); + return contract.run_app((int32_t)argc, argv.data()); } } @@ -992,7 +992,7 @@ int fx_muxer_t::handle_exec_host_command( vec_argv.push_back(argv[0]); vec_argv.insert(vec_argv.end(), argv + argoff, argv + argc); new_argv = vec_argv.data(); - new_argc = vec_argv.size(); + new_argc = (int32_t)vec_argv.size(); } trace::info(_X("Using dotnet root path [%s]"), host_info.dotnet_root.c_str()); @@ -1083,7 +1083,7 @@ int fx_muxer_t::handle_cli( int new_argoff; pal::string_t sdk_app_candidate; opt_map_t opts; - int result = command_line::parse_args_for_sdk_command(host_info, new_argv.size(), new_argv.data(), &new_argoff, sdk_app_candidate, opts); + int result = command_line::parse_args_for_sdk_command(host_info, (int32_t)new_argv.size(), new_argv.data(), &new_argoff, sdk_app_candidate, opts); if (!result) { // Transform dotnet [exec] [--additionalprobingpath path] [--depsfile file] [dll] [args] -> dotnet [dll] [args] @@ -1092,7 +1092,7 @@ int fx_muxer_t::handle_cli( host_info, sdk_app_candidate, opts, - new_argv.size(), + (int32_t)new_argv.size(), new_argv.data(), new_argoff, host_mode_t::muxer, diff --git a/src/native/corehost/fxr/hostfxr.cpp b/src/native/corehost/fxr/hostfxr.cpp index 428323273ecf6..4011dbc439785 100644 --- a/src/native/corehost/fxr/hostfxr.cpp +++ b/src/native/corehost/fxr/hostfxr.cpp @@ -145,7 +145,7 @@ SHARED_API int32_t HOSTFXR_CALLTYPE hostfxr_resolve_sdk( return 0; } - unsigned long non_negative_buffer_size = static_cast(buffer_size); + size_t non_negative_buffer_size = static_cast(buffer_size); if (sdk_path.size() < non_negative_buffer_size) { size_t length = sdk_path.copy(buffer, non_negative_buffer_size - 1); @@ -158,7 +158,7 @@ SHARED_API int32_t HOSTFXR_CALLTYPE hostfxr_resolve_sdk( trace::info(_X("hostfxr_resolve_sdk received a buffer that is too small to hold the located SDK path.")); } - return sdk_path.size() + 1; + return static_cast(sdk_path.size() + 1); } enum hostfxr_resolve_sdk2_flags_t : int32_t @@ -325,7 +325,7 @@ SHARED_API int32_t HOSTFXR_CALLTYPE hostfxr_get_available_sdks( sdk_dirs.push_back(sdk_info.full_path.c_str()); } - result(sdk_dirs.size(), &sdk_dirs[0]); + result(static_cast(sdk_dirs.size()), &sdk_dirs[0]); } return StatusCode::Success; diff --git a/src/native/corehost/hostmisc/pal.h b/src/native/corehost/hostmisc/pal.h index 42704f13fe5bd..5300450a01057 100644 --- a/src/native/corehost/hostmisc/pal.h +++ b/src/native/corehost/hostmisc/pal.h @@ -149,19 +149,28 @@ namespace pal inline int cstrcasecmp(const char* str1, const char* str2) { return ::_stricmp(str1, str2); } inline int strcmp(const char_t* str1, const char_t* str2) { return ::wcscmp(str1, str2); } inline int strcasecmp(const char_t* str1, const char_t* str2) { return ::_wcsicmp(str1, str2); } - inline int strncmp(const char_t* str1, const char_t* str2, int len) { return ::wcsncmp(str1, str2, len); } - inline int strncasecmp(const char_t* str1, const char_t* str2, int len) { return ::_wcsnicmp(str1, str2, len); } + inline int strncmp(const char_t* str1, const char_t* str2, size_t len) { return ::wcsncmp(str1, str2, len); } + inline int strncasecmp(const char_t* str1, const char_t* str2, size_t len) { return ::_wcsnicmp(str1, str2, len); } inline int pathcmp(const pal::string_t &path1, const pal::string_t &path2) { return strcasecmp(path1.c_str(), path2.c_str()); } inline string_t to_string(int value) { return std::to_wstring(value); } inline size_t strlen(const char_t* str) { return ::wcslen(str); } - inline FILE * file_open(const string_t& path, const char_t* mode) { return ::_wfopen(path.c_str(), mode); } + inline FILE * file_open(const string_t& path, const char_t* mode) + { + FILE* fd; + errno_t err = ::_wfopen_s(&fd, path.c_str(), mode); + if (err == 0) + return NULL; + return fd; + } inline void file_vprintf(FILE* f, const char_t* format, va_list vl) { ::vfwprintf(f, format, vl); ::fputwc(_X('\n'), f); } inline void err_fputs(const char_t* message) { ::fputws(message, stderr); ::fputwc(_X('\n'), stderr); } inline void out_vprintf(const char_t* format, va_list vl) { ::vfwprintf(stdout, format, vl); ::fputwc(_X('\n'), stdout); } - inline int str_vprintf(char_t* buffer, size_t count, const char_t* format, va_list vl) { return ::_vsnwprintf(buffer, count, format, vl); } - inline const char_t* strerror(int errnum) { return ::_wcserror(errnum); } + inline int str_vprintf(char_t* buffer, size_t count, const char_t* format, va_list vl) { return ::_vsnwprintf_s(buffer, count, _TRUNCATE, format, vl); } + +#pragma warning(suppress : 4996) // error C4996: '_wcserror': This function or variable may be unsafe. + inline const char_t* strerror(int errnum){ return ::_wcserror(errnum); } bool pal_utf8string(const string_t& str, std::vector* out); bool pal_clrstring(const string_t& str, std::vector* out); diff --git a/src/native/corehost/hostmisc/pal.windows.cpp b/src/native/corehost/hostmisc/pal.windows.cpp index 2b7699c546e34..78033c6259860 100644 --- a/src/native/corehost/hostmisc/pal.windows.cpp +++ b/src/native/corehost/hostmisc/pal.windows.cpp @@ -52,7 +52,10 @@ pal::string_t pal::get_timestamp() std::time_t t = std::time(0); const std::size_t elems = 100; char_t buf[elems]; - std::wcsftime(buf, elems, _X("%c GMT"), std::gmtime(&t)); + + tm dest; + ::gmtime_s(&dest, &t); + std::wcsftime(buf, elems, _X("%c GMT"), &dest); return pal::string_t(buf); } @@ -141,7 +144,7 @@ bool pal::getcwd(pal::string_t* recv) { std::vector str; str.resize(result); - result = GetCurrentDirectoryW(str.size(), str.data()); + result = GetCurrentDirectoryW(static_cast(str.size()), str.data()); assert(result <= str.size()); if (result != 0) { @@ -443,8 +446,8 @@ pal::string_t pal::get_current_os_rid_platform() if ((*pRtlGetVersion)(&osinfo) == 0) { // Win7 RID is the minimum supported version. - int majorVer = 6; - int minorVer = 1; + uint32_t majorVer = 6; + uint32_t minorVer = 1; if (osinfo.dwMajorVersion > majorVer) { @@ -608,18 +611,18 @@ bool pal::get_default_bundle_extraction_base_dir(pal::string_t& extraction_dir) return realpath(&extraction_dir); } -static bool wchar_convert_helper(DWORD code_page, const char* cstr, int len, pal::string_t* out) +static bool wchar_convert_helper(DWORD code_page, const char* cstr, size_t len, pal::string_t* out) { out->clear(); // No need of explicit null termination, so pass in the actual length. - size_t size = ::MultiByteToWideChar(code_page, 0, cstr, len, nullptr, 0); + size_t size = ::MultiByteToWideChar(code_page, 0, cstr, static_cast(len), nullptr, 0); if (size == 0) { return false; } out->resize(size, '\0'); - return ::MultiByteToWideChar(code_page, 0, cstr, len, &(*out)[0], out->size()) != 0; + return ::MultiByteToWideChar(code_page, 0, cstr, static_cast(len), &(*out)[0], static_cast(out->size())) != 0; } bool pal::pal_utf8string(const pal::string_t& str, std::vector* out) @@ -633,7 +636,7 @@ bool pal::pal_utf8string(const pal::string_t& str, std::vector* out) return false; } out->resize(size, '\0'); - return ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, out->data(), out->size(), nullptr, nullptr) != 0; + return ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, out->data(), static_cast(out->size()), nullptr, nullptr) != 0; } bool pal::pal_clrstring(const pal::string_t& str, std::vector* out) @@ -660,7 +663,7 @@ bool pal::realpath(string_t* path, bool skip_error_logging) } char_t buf[MAX_PATH]; - auto size = ::GetFullPathNameW(path->c_str(), MAX_PATH, buf, nullptr); + size_t size = ::GetFullPathNameW(path->c_str(), MAX_PATH, buf, nullptr); if (size == 0) { if (!skip_error_logging) @@ -679,7 +682,7 @@ bool pal::realpath(string_t* path, bool skip_error_logging) { str.resize(size + LongFile::UNCExtendedPathPrefix.length(), 0); - size = ::GetFullPathNameW(path->c_str(), size, (LPWSTR)str.data(), nullptr); + size = ::GetFullPathNameW(path->c_str(), static_cast(size), (LPWSTR)str.data(), nullptr); assert(size <= str.size()); if (size == 0) diff --git a/src/native/corehost/hostmisc/utils.cpp b/src/native/corehost/hostmisc/utils.cpp index ff8ae846e0f25..055b4f6a31dd8 100644 --- a/src/native/corehost/hostmisc/utils.cpp +++ b/src/native/corehost/hostmisc/utils.cpp @@ -329,7 +329,7 @@ bool get_file_path_from_env(const pal::char_t* env_key, pal::string_t* recv) return false; } -size_t index_of_non_numeric(const pal::string_t& str, unsigned i) +size_t index_of_non_numeric(const pal::string_t& str, size_t i) { return str.find_first_not_of(_X("0123456789"), i); } @@ -340,7 +340,7 @@ bool try_stou(const pal::string_t& str, unsigned* num) { return false; } - if (index_of_non_numeric(str, 0) != pal::string_t::npos) + if (index_of_non_numeric(str, 0u) != pal::string_t::npos) { return false; } diff --git a/src/native/corehost/hostmisc/utils.h b/src/native/corehost/hostmisc/utils.h index 21fe334ec4ea9..27f52d79209c5 100644 --- a/src/native/corehost/hostmisc/utils.h +++ b/src/native/corehost/hostmisc/utils.h @@ -40,7 +40,7 @@ bool get_global_shared_store_dirs(std::vector* dirs, const pal::s bool multilevel_lookup_enabled(); void get_framework_and_sdk_locations(const pal::string_t& dotnet_dir, std::vector* locations); bool get_file_path_from_env(const pal::char_t* env_key, pal::string_t* recv); -size_t index_of_non_numeric(const pal::string_t& str, unsigned i); +size_t index_of_non_numeric(const pal::string_t& str, size_t i); bool try_stou(const pal::string_t& str, unsigned* num); pal::string_t get_dotnet_root_env_var_name(); pal::string_t get_deps_from_app_binary(const pal::string_t& app_base, const pal::string_t& app); diff --git a/src/native/corehost/hostpolicy/deps_resolver.cpp b/src/native/corehost/hostpolicy/deps_resolver.cpp index 31140b19c0daf..08b2967446e39 100644 --- a/src/native/corehost/hostpolicy/deps_resolver.cpp +++ b/src/native/corehost/hostpolicy/deps_resolver.cpp @@ -243,7 +243,7 @@ void deps_resolver_t::setup_probe_config( m_probes.push_back(probe_config_t::published_deps_dir()); // The framework locations, starting with highest level framework. - for (size_t i = 1; i < m_fx_definitions.size(); ++i) + for (int32_t i = 1; i < static_cast(m_fx_definitions.size()); ++i) { if (pal::directory_exists(m_fx_definitions[i]->get_dir())) { @@ -572,7 +572,7 @@ bool deps_resolver_t::resolve_tpa_list( // Probe FX deps entries after app assemblies are added. if (m_is_framework_dependent) { - for (size_t i = 1; i < m_fx_definitions.size(); ++i) + for (int32_t i = 1; i < static_cast(m_fx_definitions.size()); ++i) { const auto& deps_entries = m_fx_definitions[i]->get_deps().get_entries(deps_entry_t::asset_types::runtime); for (const auto& entry : deps_entries) @@ -864,7 +864,7 @@ bool deps_resolver_t::resolve_probe_dirs( } // Add fx package locations to fx_dir - for (size_t i = 1; i < m_fx_definitions.size(); ++i) + for (int32_t i = 1; i < static_cast(m_fx_definitions.size()); ++i) { const auto& fx_entries = m_fx_definitions[i]->get_deps().get_entries(asset_type); diff --git a/src/native/corehost/hostpolicy/deps_resolver.h b/src/native/corehost/hostpolicy/deps_resolver.h index 92749558e78ce..76e1739978c3a 100644 --- a/src/native/corehost/hostpolicy/deps_resolver.h +++ b/src/native/corehost/hostpolicy/deps_resolver.h @@ -53,7 +53,7 @@ class deps_resolver_t , m_core_servicing(args.core_servicing) , m_is_framework_dependent(is_framework_dependent) { - int lowest_framework = m_fx_definitions.size() - 1; + int lowest_framework = static_cast(m_fx_definitions.size()) - 1; int root_framework = -1; if (root_framework_rid_fallback_graph == nullptr) { diff --git a/src/native/corehost/hostpolicy/hostpolicy.cpp b/src/native/corehost/hostpolicy/hostpolicy.cpp index c2ce9db4a07ab..656eabd9b0ee6 100644 --- a/src/native/corehost/hostpolicy/hostpolicy.cpp +++ b/src/native/corehost/hostpolicy/hostpolicy.cpp @@ -247,7 +247,7 @@ int run_app_for_context( // Execute the application unsigned int exit_code; auto hr = context.coreclr->execute_assembly( - argv_local.size(), + (int32_t)argv_local.size(), argv_local.data(), managed_app.data(), &exit_code); @@ -448,7 +448,7 @@ SHARED_API int HOSTPOLICY_CALLTYPE corehost_main_with_output_buffer(const int ar return rc; // Get length in character count not including null terminator - int len = output_string.length(); + int32_t len = static_cast(output_string.length()); if (len + 1 > buffer_size) { diff --git a/src/native/corehost/hostpolicy/hostpolicy_init.cpp b/src/native/corehost/hostpolicy/hostpolicy_init.cpp index dbeb5b6d05211..ce6f26497f459 100644 --- a/src/native/corehost/hostpolicy/hostpolicy_init.cpp +++ b/src/native/corehost/hostpolicy/hostpolicy_init.cpp @@ -5,10 +5,10 @@ #include #include "bundle/runner.h" -void make_palstr_arr(int argc, const pal::char_t** argv, std::vector* out) +void make_palstr_arr(size_t argc, const pal::char_t** argv, std::vector* out) { out->reserve(argc); - for (int i = 0; i < argc; ++i) + for (size_t i = 0; i < argc; ++i) { out->push_back(argv[i]); } diff --git a/src/native/corehost/json_parser.cpp b/src/native/corehost/json_parser.cpp index 740b71494aa8d..56c3d4b7a40e2 100644 --- a/src/native/corehost/json_parser.cpp +++ b/src/native/corehost/json_parser.cpp @@ -150,7 +150,7 @@ bool json_parser_t::parse_file(const pal::string_t& path) file.seekg(current_pos, file.beg); - realloc_buffer(stream_size - current_pos); + realloc_buffer(static_cast(stream_size - current_pos)); file.read(m_json.data(), stream_size - current_pos); return parse_raw_data(m_json.data(), m_json.size(), path); diff --git a/src/native/corehost/test/nativehost/host_context_test.cpp b/src/native/corehost/test/nativehost/host_context_test.cpp index de2cf6ef39951..763fbf7644404 100644 --- a/src/native/corehost/test/nativehost/host_context_test.cpp +++ b/src/native/corehost/test/nativehost/host_context_test.cpp @@ -654,7 +654,7 @@ bool host_context_test::mixed( argv_local.push_back(argv[i]); hostfxr_handle handle; - int rc = hostfxr.init_command_line(argv_local.size(), argv_local.data(), nullptr, &handle); + int rc = hostfxr.init_command_line(static_cast(argv_local.size()), argv_local.data(), nullptr, &handle); if (rc != StatusCode::Success) { test_output << _X("hostfxr_initialize_for_command_line failed: ") << std::hex << std::showbase << rc << std::endl; @@ -719,7 +719,7 @@ bool host_context_test::non_context_mixed( auto run_app = [&]{ // Imitate running as dotnet by passing empty as app_path to hostfxr_main_startupinfo const pal::char_t *app_path_local = launch_as_if_dotnet ? _X("") : app_path; - int rc = hostfxr.main_startupinfo(argv_local.size(), argv_local.data(), host_path.c_str(), get_dotnet_root_from_fxr_path(hostfxr_path).c_str(), app_path_local); + int rc = hostfxr.main_startupinfo(static_cast(argv_local.size()), argv_local.data(), host_path.c_str(), get_dotnet_root_from_fxr_path(hostfxr_path).c_str(), app_path_local); if (rc != StatusCode::Success) run_app_output << _X("hostfxr_main_startupinfo failed: ") << std::hex << std::showbase << rc << std::endl; }; diff --git a/src/native/corehost/test/nativehost/resolve_component_dependencies_test.cpp b/src/native/corehost/test/nativehost/resolve_component_dependencies_test.cpp index e7b3804d2f058..a94aae55b4035 100644 --- a/src/native/corehost/test/nativehost/resolve_component_dependencies_test.cpp +++ b/src/native/corehost/test/nativehost/resolve_component_dependencies_test.cpp @@ -61,7 +61,7 @@ namespace std::vector argv; argv.push_back(app_path.c_str()); - rc = hostfxr.init_command_line(argv.size(), argv.data(), nullptr, &handle); + rc = hostfxr.init_command_line(static_cast(argv.size()), argv.data(), nullptr, &handle); if (rc != StatusCode::Success) { test_output << _X("hostfxr_initialize_for_command_line failed: ") << std::hex << std::showbase << rc << std::endl; From 3901184521c9518045fac1416a8138eb01e2000d Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 4 Mar 2021 21:07:57 -0800 Subject: [PATCH 3/7] Incorrect success check. --- src/native/corehost/hostmisc/pal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/corehost/hostmisc/pal.h b/src/native/corehost/hostmisc/pal.h index 5300450a01057..0d5c0cfbcbc85 100644 --- a/src/native/corehost/hostmisc/pal.h +++ b/src/native/corehost/hostmisc/pal.h @@ -159,7 +159,7 @@ namespace pal { FILE* fd; errno_t err = ::_wfopen_s(&fd, path.c_str(), mode); - if (err == 0) + if (err != 0) return NULL; return fd; } From a7407737ffb6d5cd84f50d20ae65974b838954f0 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Fri, 5 Mar 2021 00:16:31 -0800 Subject: [PATCH 4/7] Revert to unsafe _wfopen(). --- src/native/corehost/hostmisc/pal.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/native/corehost/hostmisc/pal.h b/src/native/corehost/hostmisc/pal.h index 0d5c0cfbcbc85..9d99740d6e5fc 100644 --- a/src/native/corehost/hostmisc/pal.h +++ b/src/native/corehost/hostmisc/pal.h @@ -155,20 +155,17 @@ namespace pal inline string_t to_string(int value) { return std::to_wstring(value); } inline size_t strlen(const char_t* str) { return ::wcslen(str); } - inline FILE * file_open(const string_t& path, const char_t* mode) - { - FILE* fd; - errno_t err = ::_wfopen_s(&fd, path.c_str(), mode); - if (err != 0) - return NULL; - return fd; - } + +#pragma warning(suppress : 4996) // error C4996: '_wfopen': This function or variable may be unsafe. + inline FILE * file_open(const string_t& path, const char_t* mode) { return ::_wfopen(path.c_str(), mode); } inline void file_vprintf(FILE* f, const char_t* format, va_list vl) { ::vfwprintf(f, format, vl); ::fputwc(_X('\n'), f); } inline void err_fputs(const char_t* message) { ::fputws(message, stderr); ::fputwc(_X('\n'), stderr); } inline void out_vprintf(const char_t* format, va_list vl) { ::vfwprintf(stdout, format, vl); ::fputwc(_X('\n'), stdout); } inline int str_vprintf(char_t* buffer, size_t count, const char_t* format, va_list vl) { return ::_vsnwprintf_s(buffer, count, _TRUNCATE, format, vl); } + // Suppressing warning since the 'safe' version requires an input buffer that is unnecessary for + // uses of this function. #pragma warning(suppress : 4996) // error C4996: '_wcserror': This function or variable may be unsafe. inline const char_t* strerror(int errnum){ return ::_wcserror(errnum); } From 4f8b6ce402582e98d31045186ebe19c00d6e8077 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Fri, 5 Mar 2021 08:34:59 -0800 Subject: [PATCH 5/7] Supress gmtime usage instead of using gmtime_s --- src/native/corehost/hostmisc/pal.windows.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/native/corehost/hostmisc/pal.windows.cpp b/src/native/corehost/hostmisc/pal.windows.cpp index 78033c6259860..812900dd3edef 100644 --- a/src/native/corehost/hostmisc/pal.windows.cpp +++ b/src/native/corehost/hostmisc/pal.windows.cpp @@ -53,9 +53,8 @@ pal::string_t pal::get_timestamp() const std::size_t elems = 100; char_t buf[elems]; - tm dest; - ::gmtime_s(&dest, &t); - std::wcsftime(buf, elems, _X("%c GMT"), &dest); +#pragma warning(suppress : 4996) // error C4996: 'gmtime': This function or variable may be unsafe. + std::wcsftime(buf, elems, _X("%c GMT"), std::gmtime(&t)); return pal::string_t(buf); } From 4b3c674b4d70c46947131733fdcdb6b02211ddf5 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Fri, 5 Mar 2021 12:43:25 -0800 Subject: [PATCH 6/7] Revert use of gmtime. Revert usage of _vsnwprintf_s since it has different semantics than _vsnwprintf. Verified _vsnwprintf is being used correctly. --- src/native/corehost/hostmisc/pal.h | 5 ++++- src/native/corehost/hostmisc/pal.windows.cpp | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/native/corehost/hostmisc/pal.h b/src/native/corehost/hostmisc/pal.h index 9d99740d6e5fc..b778111b907c1 100644 --- a/src/native/corehost/hostmisc/pal.h +++ b/src/native/corehost/hostmisc/pal.h @@ -162,7 +162,10 @@ namespace pal inline void file_vprintf(FILE* f, const char_t* format, va_list vl) { ::vfwprintf(f, format, vl); ::fputwc(_X('\n'), f); } inline void err_fputs(const char_t* message) { ::fputws(message, stderr); ::fputwc(_X('\n'), stderr); } inline void out_vprintf(const char_t* format, va_list vl) { ::vfwprintf(stdout, format, vl); ::fputwc(_X('\n'), stdout); } - inline int str_vprintf(char_t* buffer, size_t count, const char_t* format, va_list vl) { return ::_vsnwprintf_s(buffer, count, _TRUNCATE, format, vl); } + + // This API is being used correctly and querying for needed size first. +#pragma warning(suppress : 4996) // error C4996: '_vsnwprintf': This function or variable may be unsafe. + inline int str_vprintf(char_t* buffer, size_t count, const char_t* format, va_list vl) { return ::_vsnwprintf(buffer, count, format, vl); } // Suppressing warning since the 'safe' version requires an input buffer that is unnecessary for // uses of this function. diff --git a/src/native/corehost/hostmisc/pal.windows.cpp b/src/native/corehost/hostmisc/pal.windows.cpp index 812900dd3edef..966e099481a4a 100644 --- a/src/native/corehost/hostmisc/pal.windows.cpp +++ b/src/native/corehost/hostmisc/pal.windows.cpp @@ -49,12 +49,13 @@ pal::string_t pal::to_lower(const pal::string_t& in) pal::string_t pal::get_timestamp() { - std::time_t t = std::time(0); + std::time_t t = std::time(nullptr); const std::size_t elems = 100; char_t buf[elems]; -#pragma warning(suppress : 4996) // error C4996: 'gmtime': This function or variable may be unsafe. - std::wcsftime(buf, elems, _X("%c GMT"), std::gmtime(&t)); + tm tm_l{}; + ::gmtime_s(&tm_l, &t); + std::wcsftime(buf, elems, _X("%c GMT"), &tm_l); return pal::string_t(buf); } From 9d046134f025f238d5fdd4b0c13df2d5a5b69d4d Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 18 Mar 2021 15:04:04 -0700 Subject: [PATCH 7/7] make cast helping function. --- src/native/corehost/bundle/extractor.cpp | 8 +++----- src/native/corehost/bundle/reader.h | 4 ++-- src/native/corehost/fxr/corehost_init.cpp | 3 +-- src/native/corehost/hostmisc/utils.h | 9 +++++++++ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/native/corehost/bundle/extractor.cpp b/src/native/corehost/bundle/extractor.cpp index 8ac1147e22a1a..be3f2077a3789 100644 --- a/src/native/corehost/bundle/extractor.cpp +++ b/src/native/corehost/bundle/extractor.cpp @@ -14,8 +14,8 @@ pal::string_t& extractor_t::extraction_dir() if (m_extraction_dir.empty()) { // Compute the final extraction location as: - // m_extraction_dir = $DOTNET_BUNDLE_EXTRACT_BASE_DIR///... - // + // m_extraction_dir = $DOTNET_BUNDLE_EXTRACT_BASE_DIR///... + // // If DOTNET_BUNDLE_EXTRACT_BASE_DIR is not set in the environment, // a default is choosen within the temporary directory. @@ -104,9 +104,7 @@ void extractor_t::extract(const file_entry_t &entry, reader_t &reader) FILE* file = create_extraction_file(entry.relative_path()); reader.set_offset(entry.offset()); int64_t size = entry.size(); - assert(size < static_cast(std::numeric_limits::max())); - - size_t cast_size = static_cast(size); + size_t cast_size = to_size_t_dbgchecked(size); if (fwrite(reader, 1, cast_size, file) != cast_size) { trace::error(_X("Failure extracting contents of the application bundle.")); diff --git a/src/native/corehost/bundle/reader.h b/src/native/corehost/bundle/reader.h index 2306f3d536d6d..0ad5a2651b68b 100644 --- a/src/native/corehost/bundle/reader.h +++ b/src/native/corehost/bundle/reader.h @@ -6,6 +6,7 @@ #include #include "pal.h" +#include "utils.h" namespace bundle { @@ -40,8 +41,7 @@ namespace bundle void read(void* dest, int64_t len) { bounds_check(len); - assert(len < static_cast(std::numeric_limits::max())); - memcpy(dest, m_ptr, static_cast(len)); + memcpy(dest, m_ptr, to_size_t_dbgchecked(len)); m_ptr += len; } diff --git a/src/native/corehost/fxr/corehost_init.cpp b/src/native/corehost/fxr/corehost_init.cpp index b50e2f57156ba..eea9b768f5f90 100644 --- a/src/native/corehost/fxr/corehost_init.cpp +++ b/src/native/corehost/fxr/corehost_init.cpp @@ -136,8 +136,7 @@ const host_interface_t& corehost_init_t::get_host_init_data() if (bundle::info_t::is_single_file_bundle()) { int64_t offset = bundle::info_t::the_app->header_offset(); - assert(offset < static_cast(std::numeric_limits::max())); - hi.single_file_bundle_header_offset = static_cast(offset); + hi.single_file_bundle_header_offset = to_size_t_dbgchecked(offset); } return hi; diff --git a/src/native/corehost/hostmisc/utils.h b/src/native/corehost/hostmisc/utils.h index 27f52d79209c5..932cb80467524 100644 --- a/src/native/corehost/hostmisc/utils.h +++ b/src/native/corehost/hostmisc/utils.h @@ -6,6 +6,7 @@ #include "pal.h" #include "trace.h" +#include #define _STRINGIFY(s) _X(s) #if defined(_WIN32) @@ -115,4 +116,12 @@ class error_writer_scope_t } }; +template +size_t to_size_t_dbgchecked(T value) +{ + assert(value >= 0); + assert(value < static_cast(std::numeric_limits::max())); + return static_cast(value); +} + #endif