From f0051877e9f6a869dee6015efdc1c4ed88501fc5 Mon Sep 17 00:00:00 2001 From: "xinpeng.wang" Date: Mon, 23 Mar 2026 14:32:51 +0800 Subject: [PATCH] fix: handle filesystem permission errors in tab completion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed filesystem error handling to prevent crashes when encountering permission denied errors during tab completion Added std::error_code usage to avoid exceptions on filesystem operations Improved path handling with safer filesystem::path concatenation Added proper error code clearing between filesystem operations Updated Makefile with debug flag for troubleshooting Log: Fixed filesystem permission error handling in tab completion Influence: 1. Test tab completion in directories with restricted permissions 2. Verify no crashes occur when encountering permission denied errors 3. Test filesystem operations with various permission scenarios 4. Validate error handling in different directory access situations 5. Confirm proper behavior when changing working directories fails fix: 修复标签页补全中的文件系统权限错误处理 修复文件系统错误处理,防止在标签页补全时遇到权限拒绝错误时崩溃 添加 std::error_code 使用以避免文件系统操作中的异常 使用更安全的 filesystem::path 连接改进路径处理 在文件系统操作之间添加适当的错误代码清除 更新 Makefile 添加调试标志用于故障排除 Log: 修复标签页补全中的文件系统权限错误处理 Influence: 1. 在受限权限目录中测试标签页补全功能 2. 验证遇到权限拒绝错误时不会发生崩溃 3. 测试各种权限场景下的文件系统操作 4. 验证不同目录访问情况下的错误处理 5. 确认更改工作目录失败时的正确行为 PMS: BUG-353821 --- Makefile | 2 +- bash-pinyin-completion.cpp | 32 +++++++++++++++++++++++--------- debian/changelog | 6 ++++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 9dc8d9f..10b4f91 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all: bash-pinyin-completion bash-pinyin-completion: bash-pinyin-completion.cpp - c++ -std=c++23 bash-pinyin-completion.cpp -o bash-pinyin-completion + c++ -std=c++23 -g bash-pinyin-completion.cpp -o bash-pinyin-completion clean: rm -f bash-pinyin-completion diff --git a/bash-pinyin-completion.cpp b/bash-pinyin-completion.cpp index e6510c7..0192ee7 100644 --- a/bash-pinyin-completion.cpp +++ b/bash-pinyin-completion.cpp @@ -54,14 +54,22 @@ unordered_map read_dict() { if (env != nullptr) { dirs = split_string(env, ':'); } + dirs.push_back("/usr/share"); string dict_file; - for (auto dir : dirs) { - if (filesystem::exists(dir) && - filesystem::exists(dir.append("/bash-pinyin-completion/char-pinyin.txt"))) { - dict_file = dir; - break; + std::error_code ec; // Reusable error code object + + for (const auto& dir : dirs) { + // Use the error_code overload to prevent exceptions on Permission Denied + if (filesystem::exists(dir, ec) && !ec) { + // Use path operator/ for cleaner and safer path concatenation + auto p = filesystem::path(dir) / "bash-pinyin-completion" / "char-pinyin.txt"; + if (filesystem::exists(p, ec) && !ec) { + dict_file = p.string(); + break; + } } + ec.clear(); // Ensure ec is cleared for the next iteration } ifstream in(dict_file); while (true) { @@ -130,15 +138,21 @@ int main(int argc, char **argv) { } auto pinyin = string_pinyin(final); auto compgen_opts = string(argv[2]); + // Navigate to the dir we need to complete auto dest = filesystem::path(input.c_str()).parent_path(); if (!dest.empty()) { - if (!filesystem::exists(dest) || !filesystem::is_directory(dest)) { + std::error_code ec; + + // Check if path exists and is a directory without throwing exceptions + if (!filesystem::exists(dest, ec) || ec || !filesystem::is_directory(dest, ec) || ec) { return 0; } - try { - filesystem::current_path(dest); - } catch (filesystem::filesystem_error const& code) { + + // Attempt to change the current working directory + filesystem::current_path(dest, ec); + if (ec) { + // If chdir fails (e.g., EACCES), exit silently to avoid crashing the shell completion return 0; } } diff --git a/debian/changelog b/debian/changelog index 4d128d7..f57011f 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +bash-pinyin-completion (0.0.3+num2) unstable; urgency=medium + + * fix filesystem error when tab. + + -- xinpeng.wang Mon, 23 Mar 2026 14:21:13 +0800 + bash-pinyin-completion (0.0.3+num1) unstable; urgency=medium * update changelog