Skip to content

Commit

Permalink
Relax file_path_set() and open_read() a bit to gracefully handle rela…
Browse files Browse the repository at this point in the history
…tive paths.

Apparently bug #3534002 exhibits a problem with a pre-compiled binary on Debian
that does not supply absolute paths for some configuration files, which prevents
any startup.

Moreover, there is not enough data in the logs to identify who the culprit is,
so we are now loudly complaining when we're given a non-absolute path, without
necessarily crashing immediately.  We will crash later if the first path to
load a file from is not absolute, but otherwise we shall continue, with possibly
a degraded service (e.g. geo IP data missing, or no hostile support).
  • Loading branch information
rmanfredi committed Jun 10, 2012
1 parent 85167c4 commit 243cec4
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/lib/file.c
Expand Up @@ -40,6 +40,7 @@
#include "file.h"
#include "glib-missing.h"
#include "halloc.h"
#include "log.h" /* For s_carp() */
#include "misc.h" /* For is_strsuffix() */
#include "path.h"
#include "timestamp.h"
Expand Down Expand Up @@ -156,7 +157,8 @@ file_locate_from_path(const char *argv0)
* If not found, try with successive alternatives, if supplied.
*
* @attention
* NB: the supplied `fv' argument is a vector of `fvcnt' elements.
* NB: the supplied `fv' argument is a vector of `fvcnt' elements. Items
* with a NULL `dir' field are ignored, but fv[0].dir cannot be NULL.
*
* @param what is what is being opened, for logging purposes.
* @param fv is a vector of files to try to open, in sequence
Expand All @@ -181,6 +183,7 @@ open_read(

g_assert(fv != NULL);
g_assert(fvcnt >= 1);
g_assert(fv->dir != NULL);

path = make_pathname(fv->dir, fv->name);
if (!is_absolute_path(path)) {
Expand Down Expand Up @@ -248,6 +251,8 @@ open_read(

for (xfv = fv + 1, xfvcnt = fvcnt - 1; xfvcnt; xfv++, xfvcnt--) {
HFREE_NULL(path);
if (NULL == xfv->dir) /* In alternatives, dir may be NULL */
continue;
path = make_pathname(xfv->dir, xfv->name);
idx++;
if (NULL != path && NULL != (in = fopen(path, "r")))
Expand Down Expand Up @@ -413,17 +418,38 @@ file_config_preamble(FILE *out, const char *what)

/**
* Initializes `fp' with directory path `dir' and filename `name'.
*
* The directory must be an absolute path or the entry is initialized with NULL
* values and a loud warning is emitted.
*/
void
file_path_set(file_path_t *fp, const char *dir, const char *name)
{
g_assert(fp);
g_assert(dir);
g_assert(name);
g_assert(is_absolute_path(dir));

fp->dir = dir;
fp->name = name;
/*
* For robustness, we no longer assert that the path must be absolute.
*
* The open_read() routine will skip file_path_t entries in the vector
* that are NULL, the only requirement being that the first entry of the
* vector be non-NULL, i.e. an absolute path.
*
* Since this is an abnormal situation, loudly warn so that we may find
* out who the culprit is.
*/

if (!is_absolute_path(dir)) {
s_carp("%s(): ignoring non-absolute path \"%s\" for \"%s\"",
G_STRFUNC, dir, name);

fp->dir = NULL;
fp->name = NULL;
} else {
fp->dir = dir;
fp->name = name;
}
}

/**
Expand Down

0 comments on commit 243cec4

Please sign in to comment.