Do not segfault if PATH is unset.
authorMatt Kraai <kraai@debian.org>
Mon, 14 Jan 2002 18:30:10 +0000 (18:30 -0000)
committerMatt Kraai <kraai@debian.org>
Mon, 14 Jan 2002 18:30:10 +0000 (18:30 -0000)
findutils/which.c
testsuite/which/which-uses-default-path [new file with mode: 0644]

index eec5fdbfb5d5c070e43f1ce950d8e3fa162b5d68..b2af5a8ea637d2875ce79f6f0261805703be6679 100644 (file)
@@ -38,15 +38,16 @@ extern int which_main(int argc, char **argv)
        argc--;
 
        path_list = getenv("PATH");
-       if (!path_list)
-               path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin";
-
-       /* Replace colons with zeros in path_parsed and count them */
-       for(i=strlen(path_list); i > 0; i--) 
-               if (path_list[i]==':') {
-                       path_list[i]=0;
-                       count++;
-               }
+       if (path_list != NULL) {
+               for(i=strlen(path_list); i > 0; i--)
+                       if (path_list[i]==':') {
+                               path_list[i]=0;
+                               count++;
+                       }
+       } else {
+               path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin";
+               count = 5;
+       }
 
        while(argc-- > 0) { 
                path_n = path_list;
diff --git a/testsuite/which/which-uses-default-path b/testsuite/which/which-uses-default-path
new file mode 100644 (file)
index 0000000..e2a2f38
--- /dev/null
@@ -0,0 +1,8 @@
+BUSYBOX=$(type -p busybox)
+echo $BUSYBOX
+SAVED_PATH=$PATH
+unset PATH
+$BUSYBOX which ls
+STATUS=$?
+export PATH=$SAVED_PATH
+return $STATUS