From 0d7bea5763aa5e18cbfb67aa7ae73431a689e409 Mon Sep 17 00:00:00 2001 From: HanifKoh <76276251+HanifKoh@users.noreply.github.com> Date: Sat, 21 Nov 2026 05:44:04 +0800 Subject: [PATCH] List and Load Resource Textures Whose Paths Pass MAX_PATH on Windows (#17730) * Test Resource Textures Whose Paths Pass MAX_PATH * List and Load Resource Textures Whose Paths Pass MAX_PATH on Windows --- src/slic3r/GUI/TextureLibrary.cpp | 11 +++- src/slic3r/plugin/PluginFsUtils.cpp | 14 ++++- .../test_plugin_resource_texture.cpp | 56 +++++++++++++++++++ 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/slic3r/GUI/TextureLibrary.cpp b/src/slic3r/GUI/TextureLibrary.cpp index 3d76791027..08e3935084 100644 --- a/src/slic3r/GUI/TextureLibrary.cpp +++ b/src/slic3r/GUI/TextureLibrary.cpp @@ -17,6 +17,8 @@ #include #include +#include +#include #include #include #include @@ -219,7 +221,8 @@ wxImage image_from_decoded(const DecodedHeightTexture &image, std::vector read_file_bytes(const std::string &path) { - std::ifstream ifs(path, std::ios::binary); + // A plugin pack can extract a texture past Windows' MAX_PATH, which only extended_path() reaches. + boost::filesystem::ifstream ifs(extended_path(boost::filesystem::path(path)), std::ios::binary); return std::vector(std::istreambuf_iterator(ifs), std::istreambuf_iterator()); } @@ -451,8 +454,10 @@ std::shared_ptr> load_texture_image_data(const std::s // Not a PNG at all (a jpg somebody copied into the folder by hand, say): convert it the same way // an import would - keeping its colour if it has any - but leave the file on disk alone. - wxImage image; - if (!image.LoadFile(from_u8(path)) || !image.IsOk()) { + // Decoded from the bytes already read: reopening the file by name would hit MAX_PATH again. + wxImage image; + wxMemoryInputStream stream(bytes.data(), bytes.size()); + if (!image.LoadFile(stream) || !image.IsOk()) { error = _u8L("Could not load the selected image."); return nullptr; } diff --git a/src/slic3r/plugin/PluginFsUtils.cpp b/src/slic3r/plugin/PluginFsUtils.cpp index a1215949a3..dea29d3e82 100644 --- a/src/slic3r/plugin/PluginFsUtils.cpp +++ b/src/slic3r/plugin/PluginFsUtils.cpp @@ -1701,8 +1701,12 @@ std::vector collect_resource_texture_files(const std::string& dir, for (fs::directory_iterator it(resolved_dir, ec), end; it != end && !ec; it.increment(ec)) { const fs::path& path = it->path(); + // Plugin extraction writes through extended_path(), so a pack file can sit past Windows' + // MAX_PATH; the plain path cannot reach it there. + const fs::path io_path = extended_path(path); + boost::system::error_code file_ec; - if (!fs::is_regular_file(path, ec) || ec) + if (!fs::is_regular_file(io_path, file_ec) || file_ec) continue; std::string ext = path.extension().string(); @@ -1710,8 +1714,12 @@ std::vector collect_resource_texture_files(const std::string& dir, if (ext != ".png" && ext != ".jpg" && ext != ".jpeg" && ext != ".bmp") continue; - // Re-check per file: a symlink inside the pack must not resolve outside it. - if (!is_inside_allowed_root(path, resolved_root)) + // Re-check per file: a symlink inside the pack must not resolve outside it. Any other entry is a + // direct child of resolved_dir, checked above. is_inside_allowed_root() resolves the plain path, + // so a symlink that path cannot reach is refused rather than trusted. + const fs::file_status status = fs::symlink_status(io_path, file_ec); + if (file_ec || (fs::is_symlink(status) && (!fs::exists(path, file_ec) || file_ec || + !is_inside_allowed_root(path, resolved_root)))) continue; files.push_back(path.string()); diff --git a/tests/slic3rutils/test_plugin_resource_texture.cpp b/tests/slic3rutils/test_plugin_resource_texture.cpp index c4c9feab9e..951cd4b591 100644 --- a/tests/slic3rutils/test_plugin_resource_texture.cpp +++ b/tests/slic3rutils/test_plugin_resource_texture.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -305,6 +306,26 @@ bool load_project(const std::string& path, Model& model, const ScopedTemporaryDi return loaded; } +// A gray texture (".png", or ".bmp" for the decode fallback) whose full path is longer than the +// 260-character MAX_PATH the Win32 APIs enforce when LongPathsEnabled is off. A 240-character name stays +// under the NTFS 255 limit per component. It is written through extended_path(), as plugin extraction +// writes it. +fs::path write_long_path_texture(const fs::path& dir, const std::string& ext = ".png") +{ + const fs::path file = dir / (std::string(240, 'a') + ext); + REQUIRE(file.string().size() > 260); + + ScopedTemporaryDir staging("tex-long-src"); + const fs::path source = staging.path() / ("src" + ext); + if (ext == ".bmp") + write_bytes(source, make_gray_bmp(4, 4, 128)); + else + write_gray_png(source, 128); + fs::create_directories(dir); + fs::copy_file(source, extended_path(file)); + return file; +} + } // namespace // ── Validation (collect_resource_texture_files, no Python) ────────────────────────────────────── @@ -323,6 +344,41 @@ TEST_CASE("A texture file directly inside the plugin folder is listed", "[Plugin CHECK(fs::path(files.front()).filename() == "grain.png"); } +TEST_CASE("A texture whose full path is longer than MAX_PATH is listed", "[PluginResourceTexture]") +{ + ScopedTemporaryDir root("tex-long"); + const fs::path file = write_long_path_texture(root.path() / "textures"); + + std::string error; + const std::vector files = + collect_resource_texture_files((root.path() / "textures").string(), root.string(), error); + + CHECK(error.empty()); + REQUIRE(files.size() == 1); + CHECK(fs::path(files.front()).filename() == file.filename()); + + // ScopedTemporaryDir's plain remove_all cannot reach past MAX_PATH on Windows. + fs::remove(extended_path(file)); +} + +TEST_CASE("A texture whose full path is longer than MAX_PATH loads", "[PluginResourceTexture]") +{ + ensure_wx_image_handlers(); + const std::string ext = GENERATE(".png", ".bmp"); + ScopedTemporaryDir root("tex-long"); + const fs::path file = write_long_path_texture(root.path() / "textures", ext); + + std::string error; + const auto bytes = GUI::load_texture_image_data(file.string(), error); + + CHECK(error.empty()); + REQUIRE(bytes != nullptr); + CHECK_FALSE(bytes->empty()); + + // ScopedTemporaryDir's plain remove_all cannot reach past MAX_PATH on Windows. + fs::remove(extended_path(file)); +} + TEST_CASE("A directory reached with a parent-directory escape is refused", "[PluginResourceTexture]") { ScopedTemporaryDir parent("tex-escape");