chiark / gitweb /
path-util: also check for existence of binary when given absolute path
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 12 Apr 2014 21:17:49 +0000 (17:17 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 12 Apr 2014 21:23:13 +0000 (17:23 -0400)
In contrast to a filename-only argument, find_binary() did not
actually check if an path exists, allowing the code to fail later on.
This was OK, but it seems nicer to treat both paths identically.

Also take advantage of path_make_absolute_cwd doing strdup() by itself
if necessary to simplify.

src/shared/path-util.c
src/test/test-path-util.c

index 1ad1084b2d5f0653bb56b1e8dee818b39199fe88..e35d7f8d67b502a9864e3a36c9fab689511b25bc 100644 (file)
@@ -427,15 +427,16 @@ int find_binary(const char *name, char **filename) {
         assert(name);
 
         if (strchr(name, '/')) {
+                if (access(name, X_OK) < 0)
+                        return -errno;
+
                 if (filename) {
                         char *p;
 
-                        if (path_is_absolute(name))
-                                p = strdup(name);
-                        else
-                                p = path_make_absolute_cwd(name);
+                        p = path_make_absolute_cwd(name);
                         if (!p)
                                 return -ENOMEM;
+
                         *filename = p;
                 }
 
index a2cf0af75fef675774444ced03a7f49c81acb0b2..527b27565693959b89405da5c3d36115aed9b577 100644 (file)
@@ -104,6 +104,8 @@ static void test_find_binary(void) {
         free(p);
 
         assert(find_binary("xxxx-xxxx", &p) == -ENOENT);
+
+        assert(find_binary("/some/dir/xxxx-xxxx", &p) == -ENOENT);
 }
 
 static void test_prefixes(void) {