diff --git a/src/corehost/cli/deps_format.cpp b/src/corehost/cli/deps_format.cpp index 58c477c97..5e2f97c21 100644 --- a/src/corehost/cli/deps_format.cpp +++ b/src/corehost/cli/deps_format.cpp @@ -341,6 +341,11 @@ bool deps_json_t::load(bool portable, const pal::string_t& deps_path, const rid_ return false; } + if (skip_utf8_bom(&file)) + { + trace::verbose(_X("UTF-8 BOM skipped while reading [%s]"), deps_path.c_str()); + } + try { const auto json = json_value::parse(file); @@ -355,8 +360,10 @@ bool deps_json_t::load(bool portable, const pal::string_t& deps_path, const rid_ return (portable) ? load_portable(json, name, rid_fallback_graph) : load_standalone(json, name); } - catch (...) + catch (const web::json::json_exception& je) { + pal::string_t jes = pal::to_palstring(je.what()); + trace::error(_X("A JSON parsing exception occurred in [%s]: %s"), deps_path.c_str(), jes.c_str()); return false; } } diff --git a/src/corehost/cli/fxr/fx_muxer.cpp b/src/corehost/cli/fxr/fx_muxer.cpp index 709f30c5a..bb5f8fa06 100644 --- a/src/corehost/cli/fxr/fx_muxer.cpp +++ b/src/corehost/cli/fxr/fx_muxer.cpp @@ -83,6 +83,11 @@ pal::string_t fx_muxer_t::resolve_cli_version(const pal::string_t& global_json) return retval; } + if (skip_utf8_bom(&file)) + { + trace::verbose(_X("UTF-8 BOM skipped while reading [%s]"), global_json.c_str()); + } + try { const auto root = json_value::parse(file); @@ -106,7 +111,7 @@ pal::string_t fx_muxer_t::resolve_cli_version(const pal::string_t& global_json) catch (const web::json::json_exception& je) { pal::string_t jes = pal::to_palstring(je.what()); - trace::info(_X("A JSON parsing exception occurred: %s"), jes.c_str()); + trace::error(_X("A JSON parsing exception occurred in [%s]: %s"), global_json.c_str(), jes.c_str()); } trace::verbose(_X("CLI version is [%s] in global json file [%s]"), retval.c_str(), global_json.c_str()); return retval; diff --git a/src/corehost/cli/runtime_config.cpp b/src/corehost/cli/runtime_config.cpp index f588bbd50..a68174a9f 100644 --- a/src/corehost/cli/runtime_config.cpp +++ b/src/corehost/cli/runtime_config.cpp @@ -76,6 +76,11 @@ bool runtime_config_t::ensure_parsed() return false; } + if (skip_utf8_bom(&file)) + { + trace::verbose(_X("UTF-8 BOM skipped while reading [%s]"), m_path.c_str()); + } + try { const auto root = json_value::parse(file); @@ -89,7 +94,7 @@ bool runtime_config_t::ensure_parsed() catch (const web::json::json_exception& je) { pal::string_t jes = pal::to_palstring(je.what()); - trace::info(_X("A JSON parsing exception occurred: %s"), jes.c_str()); + trace::error(_X("A JSON parsing exception occurred in [%s]: %s"), m_path.c_str(), jes.c_str()); return false; } return true; diff --git a/src/corehost/common/pal.windows.cpp b/src/corehost/common/pal.windows.cpp index c67c33c9a..621fe78d0 100644 --- a/src/corehost/common/pal.windows.cpp +++ b/src/corehost/common/pal.windows.cpp @@ -241,8 +241,12 @@ void pal::readdir(const string_t& path, std::vector* list) search_string.push_back(DIR_SEPARATOR); search_string.push_back(L'*'); - WIN32_FIND_DATAW data; - auto handle = ::FindFirstFileW(search_string.c_str(), &data); + WIN32_FIND_DATAW data = { 0 }; + auto handle = ::FindFirstFileExW(search_string.c_str(), FindExInfoStandard, &data, FindExSearchNameMatch, NULL, 0); + if (handle == INVALID_HANDLE_VALUE) + { + return; + } do { string_t filepath(data.cFileName); diff --git a/src/corehost/common/utils.cpp b/src/corehost/common/utils.cpp index 4bf595df0..75491d634 100644 --- a/src/corehost/common/utils.cpp +++ b/src/corehost/common/utils.cpp @@ -191,3 +191,30 @@ bool parse_known_args( return true; } +// Try to match 0xEF 0xBB 0xBF byte sequence (no endianness here.) +bool skip_utf8_bom(pal::ifstream_t* stream) +{ + if (stream->eof() || !stream->good()) + { + return false; + } + + int peeked = stream->peek(); + if (peeked == EOF || ((peeked & 0xFF) != 0xEF)) + { + return false; + } + + unsigned char bytes[3]; + stream->read((char*) bytes, 3); + if ((stream->gcount() < 3) || + (bytes[1] != 0xBB) || + (bytes[2] != 0xBF)) + { + // Reset to 0 if returning false. + stream->seekg(0, stream->beg); + return false; + } + + return true; +} diff --git a/src/corehost/common/utils.h b/src/corehost/common/utils.h index 29aee0487..5f53e5068 100644 --- a/src/corehost/common/utils.h +++ b/src/corehost/common/utils.h @@ -26,4 +26,5 @@ bool parse_known_args( const std::vector& known_opts, std::unordered_map* opts, int* num_args); +bool skip_utf8_bom(pal::ifstream_t* stream); #endif