Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
32 changes: 23 additions & 9 deletions bash-pinyin-completion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,22 @@ unordered_map<string, string> 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) {
Expand Down Expand Up @@ -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;
}
}
Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
bash-pinyin-completion (0.0.3+num2) unstable; urgency=medium

* fix filesystem error when tab.

-- xinpeng.wang <wangxinpeng@uniontech.com> Mon, 23 Mar 2026 14:21:13 +0800

bash-pinyin-completion (0.0.3+num1) unstable; urgency=medium

* update changelog
Expand Down
Loading