Ensure FS#searchPath only selects executable files

On Posix non executable files on the path should be ignored, on Windows
File#canExecute always returns true.

While we are here also add missing braces.

Bug: 575385
Change-Id: I80cd5057bdf9632a17f103db6f1356e975b6e150
This commit is contained in:
Matthias Sohn 2021-08-13 09:36:26 +02:00
parent 3cd7eb1b23
commit 5fb71fb54e
1 changed files with 5 additions and 3 deletions

View File

@ -1267,7 +1267,8 @@ private File defaultUserHomeImpl() {
/** /**
* Searches the given path to see if it contains one of the given files. * Searches the given path to see if it contains one of the given files.
* Returns the first it finds. Returns null if not found or if path is null. * Returns the first it finds which is executable. Returns null if not found
* or if path is null.
* *
* @param path * @param path
* List of paths to search separated by File.pathSeparator * List of paths to search separated by File.pathSeparator
@ -1277,14 +1278,15 @@ private File defaultUserHomeImpl() {
* @since 3.0 * @since 3.0
*/ */
protected static File searchPath(String path, String... lookFor) { protected static File searchPath(String path, String... lookFor) {
if (path == null) if (path == null) {
return null; return null;
}
for (String p : path.split(File.pathSeparator)) { for (String p : path.split(File.pathSeparator)) {
for (String command : lookFor) { for (String command : lookFor) {
final File file = new File(p, command); final File file = new File(p, command);
try { try {
if (file.isFile()) { if (file.isFile() && file.canExecute()) {
return file.getAbsoluteFile(); return file.getAbsoluteFile();
} }
} catch (SecurityException e) { } catch (SecurityException e) {