From 170a7e263c713fd5fe56b32336de8beaf9afe667 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Mon, 3 Jan 2022 23:43:16 -0700 Subject: [PATCH] Userland: Fail Core::find_executable_in_path on empty inputs Before this patch, `which ""` or `type ""` would say that the empty string is `/usr/local/bin/`. Convert callers to consistently call is_empty() on the returned string while we're at it, to support eventually removing the is_null() String state in the future. --- Userland/Libraries/LibCore/DirIterator.cpp | 3 +++ Userland/Shell/Builtin.cpp | 2 +- Userland/Utilities/which.cpp | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCore/DirIterator.cpp b/Userland/Libraries/LibCore/DirIterator.cpp index 11758a328c..527c36b579 100644 --- a/Userland/Libraries/LibCore/DirIterator.cpp +++ b/Userland/Libraries/LibCore/DirIterator.cpp @@ -97,6 +97,9 @@ String DirIterator::next_full_path() String find_executable_in_path(String filename) { + if (filename.is_empty()) + return {}; + if (filename.starts_with('/')) { if (access(filename.characters(), X_OK) == 0) return filename; diff --git a/Userland/Shell/Builtin.cpp b/Userland/Shell/Builtin.cpp index 2ef26a999a..f6ae701c11 100644 --- a/Userland/Shell/Builtin.cpp +++ b/Userland/Shell/Builtin.cpp @@ -227,7 +227,7 @@ int Shell::builtin_type(int argc, const char** argv) // check if its an executable in PATH auto fullpath = Core::find_executable_in_path(command); - if (!fullpath.is_null()) { + if (!fullpath.is_empty()) { printf("%s is %s\n", command, escape_token(fullpath).characters()); continue; } diff --git a/Userland/Utilities/which.cpp b/Userland/Utilities/which.cpp index 42183261f8..f0abc156c3 100644 --- a/Userland/Utilities/which.cpp +++ b/Userland/Utilities/which.cpp @@ -21,7 +21,7 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.parse(arguments); auto fullpath = Core::find_executable_in_path(filename); - if (fullpath.is_null()) { + if (fullpath.is_empty()) { warnln("no '{}' in path", filename); return 1; }