From 5fde00bdb689884f6de0ab2829f0df907380a010 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 10 Dec 2020 21:32:54 +0100 Subject: [PATCH] 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. --- ChangeLog | 12 ++++++++++++ lib/findprog-in.c | 31 +++++++++++++++++++++++++------ lib/findprog.h | 1 + modules/findprog-in | 5 +++-- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8c05ee6e9e..d4c17d059b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2020-12-10 Bruno Haible + + 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 fmaf: Work around a bug on FreeBSD 12.2/arm. diff --git a/lib/findprog-in.c b/lib/findprog-in.c index 0f76e36ca4..863f67e13f 100644 --- a/lib/findprog-in.c +++ b/lib/findprog-in.c @@ -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); diff --git a/lib/findprog.h b/lib/findprog.h index aef6289144..c020290d87 100644 --- a/lib/findprog.h +++ b/lib/findprog.h @@ -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. diff --git a/modules/findprog-in b/modules/findprog-in index 84787a644e..694c5031e5 100644 --- a/modules/findprog-in +++ b/modules/findprog-in @@ -11,10 +11,11 @@ Depends-on: stdbool sys_stat filename -xalloc -xconcat-filename +concat-filename access stat +strdup-posix +malloc-posix unistd configure.ac: -- 2.39.5