]> Savannah Git Hosting - gnulib.git/commitdiff
findprog-in: Don't exit upon out-of-memory.
authorBruno Haible <bruno@clisp.org>
Thu, 10 Dec 2020 20:32:54 +0000 (21:32 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 10 Dec 2020 20:49:04 +0000 (21:49 +0100)
* lib/findprog.h (find_in_given_path): Document ENOMEM as possible error
code.
* lib/findprog-in.c: Don't include xalloc.h.
(find_in_given_path): Call concatenated_filename, not
xconcatenated_filename. Call strdup, not xstrdup. Upon out-of-memory,
return NULL with errno set.
* modules/findprog-in (Depends-on): Remove xconcat-filename, xalloc. Add
concat-filename, strdup-posix, malloc-posix.

ChangeLog
lib/findprog-in.c
lib/findprog.h
modules/findprog-in

index 8c05ee6e9e222e083e45b4e7508a20eaf0a8efec..d4c17d059b93e0931177961acff6f29fe30d94c3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2020-12-10  Bruno Haible  <bruno@clisp.org>
+
+       findprog-in: Don't exit upon out-of-memory.
+       * lib/findprog.h (find_in_given_path): Document ENOMEM as possible error
+       code.
+       * lib/findprog-in.c: Don't include xalloc.h.
+       (find_in_given_path): Call concatenated_filename, not
+       xconcatenated_filename. Call strdup, not xstrdup. Upon out-of-memory,
+       return NULL with errno set.
+       * modules/findprog-in (Depends-on): Remove xconcat-filename, xalloc. Add
+       concat-filename, strdup-posix, malloc-posix.
+
 2020-12-09  Bruno Haible  <bruno@clisp.org>
 
        fmaf: Work around a bug on FreeBSD 12.2/arm.
index 0f76e36ca43ea1ae05d07df0cfdacff69877d571..863f67e13fdec507f780837af8ca540eb0219c39 100644 (file)
@@ -30,7 +30,6 @@
 
 #include "filename.h"
 #include "concat-filename.h"
-#include "xalloc.h"
 
 #if (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
   /* Native Windows, OS/2, DOS */
@@ -129,7 +128,10 @@ find_in_given_path (const char *progname, const char *path,
                   {
                     /* Concatenate progname and suffix.  */
                     char *progpathname =
-                      xconcatenated_filename ("", progname, suffix);
+                      concatenated_filename ("", progname, suffix);
+
+                    if (progpathname == NULL)
+                      return NULL; /* errno is set here */
 
                     /* On systems which have the eaccess() system call, let's
                        use it.  On other systems, let's hope that this program
@@ -178,9 +180,12 @@ find_in_given_path (const char *progname, const char *path,
     path = "";
 
   {
-    int failure_errno;
     /* Make a copy, to prepare for destructive modifications.  */
-    char *path_copy = xstrdup (path);
+    char *path_copy = strdup (path);
+    if (path_copy == NULL)
+      return NULL; /* errno is set here */
+
+    int failure_errno;
     char *path_rest;
     char *cp;
 
@@ -215,7 +220,14 @@ find_in_given_path (const char *progname, const char *path,
               {
                 /* Concatenate dir, progname, and suffix.  */
                 char *progpathname =
-                  xconcatenated_filename (dir, progname, suffix);
+                  concatenated_filename (dir, progname, suffix);
+
+                if (progpathname == NULL)
+                  {
+                    /* errno is set here.  */
+                    failure_errno = errno;
+                    goto failed;
+                  }
 
                 /* On systems which have the eaccess() system call, let's
                    use it.  On other systems, let's hope that this program
@@ -241,7 +253,13 @@ find_in_given_path (const char *progname, const char *path,
                                    This avoids a second PATH search when the
                                    caller uses execl/execv/execlp/execvp.  */
                                 progpathname =
-                                  XNMALLOC (2 + strlen (progname) + 1, char);
+                                  (char *) malloc (2 + strlen (progname) + 1);
+                                if (progpathname == NULL)
+                                  {
+                                    /* errno is set here.  */
+                                    failure_errno = errno;
+                                    goto failed;
+                                  }
                                 progpathname[0] = '.';
                                 progpathname[1] = NATIVE_SLASH;
                                 memcpy (progpathname + 2, progname,
@@ -267,6 +285,7 @@ find_in_given_path (const char *progname, const char *path,
           break;
       }
 
+   failed:
     /* Not found in PATH.  */
     free (path_copy);
 
index aef628914416e354beb7d4185ad578bf778c4fe0..c020290d87497759d3b8775a5452add9d36a9eaa 100644 (file)
@@ -53,6 +53,7 @@ extern const char *find_in_path (const char *progname);
        - EACCES: means that the program's file cannot be accessed (due to some
          issue with one of the ancestor directories) or lacks the execute
          permissions.
+       - ENOMEM: means out of memory.
    If OPTIMIZE_FOR_EXEC is true, the function saves some work, under the
    assumption that the resulting pathname will not be accessed directly,
    only through execl/execv or execlp/execvp.
index 84787a644e620f90f59c7a4d57c63d28b23762da..694c5031e5e8a42355e4eed483b84227f32ca9b6 100644 (file)
@@ -11,10 +11,11 @@ Depends-on:
 stdbool
 sys_stat
 filename
-xalloc
-xconcat-filename
+concat-filename
 access
 stat
+strdup-posix
+malloc-posix
 unistd
 
 configure.ac: