From c7fc22288e7e218e96aaa446bcd6b4fa7d62d469 Mon Sep 17 00:00:00 2001 From: SoftFever Date: Wed, 28 Oct 2026 18:00:29 +0800 Subject: [PATCH] Fix Windows startup failure on an OrcaSlicer.conf without a trailing newline --- src/libslic3r/AppConfig.cpp | 40 +++++++++++++++------------ tests/libslic3r/test_appconfig.cpp | 44 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/src/libslic3r/AppConfig.cpp b/src/libslic3r/AppConfig.cpp index 4d6cd3391f..5614385e05 100644 --- a/src/libslic3r/AppConfig.cpp +++ b/src/libslic3r/AppConfig.cpp @@ -763,6 +763,22 @@ static std::string appconfig_md5_hash_line(const std::string_view data) return "# MD5 checksum " + md5_digest_str + "\n"; } +// write_config_file() saves the JSON configuration followed by a newline and the MD5 checksum line of the JSON. +// Returns the JSON part of the file text and whether that checksum line follows it and matches. A person or a tool +// editing the file may drop the checksum line, and the newline after the closing brace with it. +static std::pair split_json_config_checksum(const std::string &file_text) +{ + const size_t json_end = file_text.find_last_of('}'); + // Without a closing brace there is no JSON object, and parsing the empty text fails. + if (json_end == std::string::npos) + return {}; + std::string json_text = file_text.substr(0, json_end + 1); + // Skip the newline after the closing brace. + const size_t checksum_pos = std::min(json_end + 2, file_text.size()); + const bool checksum_ok = file_text.compare(checksum_pos, std::string::npos, appconfig_md5_hash_line(json_text)) == 0; + return { std::move(json_text), checksum_ok }; +} + // Assume that the last line with the comment inside the config file contains a checksum and that the user didn't modify the config file. static bool verify_config_file_checksum(boost::nowide::ifstream &ifs) { @@ -811,18 +827,12 @@ std::string AppConfig::load(bool read_only) #ifdef WIN32 std::stringstream input_stream; input_stream << ifs.rdbuf(); - std::string total_string = input_stream.str(); - size_t last_pos = total_string.find_last_of('}'); - std::string left_string = total_string.substr(0, last_pos+1); - //skip the "\n" - std::string right_string = total_string.substr(last_pos+2); - - std::string md5_str = appconfig_md5_hash_line({left_string.data()}); + const auto [json_text, checksum_ok] = split_json_config_checksum(input_stream.str()); // Verify the checksum of the config file without taking just for debugging purpose. - if (md5_str != right_string) + if (!checksum_ok) BOOST_LOG_TRIVIAL(info) << "The configuration file " << AppConfig::loading_path() << " has a wrong MD5 checksum or the checksum is missing. This may indicate a file corruption or a harmless user edit."; - j = json::parse(left_string); + j = json::parse(json_text); #else ifs >> j; #endif @@ -837,14 +847,8 @@ std::string AppConfig::load(bool read_only) boost::nowide::ifstream backup_ifs(backup_path); std::stringstream back_input_stream; back_input_stream << backup_ifs.rdbuf(); - std::string back_total_string = back_input_stream.str(); - size_t back_last_pos = back_total_string.find_last_of('}'); - std::string back_left_string = back_total_string.substr(0, back_last_pos+1); - std::string back_right_string = back_total_string.substr(back_last_pos+2); - - std::string back_md5_str = appconfig_md5_hash_line({back_left_string.data()}); - // Verify the checksum of the config file without taking just for debugging purpose. - if (back_md5_str != back_right_string) { + const auto [back_json_text, back_checksum_ok] = split_json_config_checksum(back_input_stream.str()); + if (!back_checksum_ok) { BOOST_LOG_TRIVIAL(error) << format("Both \"%1%\" and \"%2%\" are corrupted. It isn't possible to restore configuration from the backup.", AppConfig::loading_path(), backup_path); backup_ifs.close(); boost::filesystem::remove(backup_path); @@ -857,7 +861,7 @@ std::string AppConfig::load(bool read_only) else { BOOST_LOG_TRIVIAL(info) << format("Configuration file \"%1%\" was corrupted. It has been succesfully restored from the backup \"%2%\".", AppConfig::loading_path(), backup_path); // Try parse configuration file after restore from backup. - j = json::parse(back_left_string); + j = json::parse(back_json_text); recovered = true; } } diff --git a/tests/libslic3r/test_appconfig.cpp b/tests/libslic3r/test_appconfig.cpp index fae3c4d3cb..fe9568d35b 100644 --- a/tests/libslic3r/test_appconfig.cpp +++ b/tests/libslic3r/test_appconfig.cpp @@ -10,6 +10,7 @@ #include #include +#include #include using namespace Slic3r; @@ -262,3 +263,46 @@ TEST_CASE("peek_bool accepts the legacy spellings and tolerates a bad or missing CHECK_FALSE(AppConfig::peek_bool(write("broken", "{not json"), "k")); CHECK_FALSE(AppConfig::peek_bool((tmp.path() / "missing").string(), "k")); } + +TEST_CASE("A configuration without the checksum line loads", "[AppConfig][Regression]") +{ + ScopedDataDir data_dir("no-checksum"); + // A person or a tool editing the file drops the checksum line, often with the newline after the closing brace. + const std::string ending = GENERATE("", "\n"); + std::ofstream(AppConfig::config_path(data_dir.dir.string(), AppConfig::EAppMode::Editor), std::ios::binary) + << R"({"app":{"k":"v"}})" << ending; + + AppConfig config; + REQUIRE(config.load().empty()); + CHECK(config.get("k") == "v"); +} + +#ifdef _WIN32 +// Only Windows saves the checksum line, and a backup to restore a corrupted configuration from. +TEST_CASE("A corrupted configuration is restored from a backup whose checksum matches", "[AppConfig]") +{ + ScopedDataDir data_dir("restore-backup"); + save_main_thread_id(); + AppConfig saved; + saved.set("k", "v"); + saved.save(); + std::ofstream(saved.config_path(), std::ios::binary) << "{not json"; + + AppConfig config; + REQUIRE(config.load().empty()); + CHECK(config.get("k") == "v"); +} + +TEST_CASE("A backup without the checksum line is not restored", "[AppConfig][Regression]") +{ + ScopedDataDir data_dir("unverified-backup"); + const std::string path = AppConfig::config_path(data_dir.dir.string(), AppConfig::EAppMode::Editor); + std::ofstream(path, std::ios::binary) << "{not json"; + const std::string ending = GENERATE("", "\n"); + std::ofstream(path + ".bak", std::ios::binary) << R"({"app":{"k":"v"}})" << ending; + + AppConfig config; + CHECK_FALSE(config.load().empty()); + CHECK(config.get("k").empty()); +} +#endif