From 8335dd84ca85651115d518d7e3d963de9d82e22f Mon Sep 17 00:00:00 2001 From: Tomas Korbar Date: Wed, 16 Sep 2020 14:05:05 +0200 Subject: [PATCH] Improve opening of authentication file --- authfile.c | 17 +++++------------ authfile.h | 2 +- memcached.c | 9 +++++++-- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/authfile.c b/authfile.c index f8935af1b2..5f001d1cc5 100644 --- a/authfile.c +++ b/authfile.c @@ -33,23 +33,16 @@ enum authfile_ret authfile_load(const char *file) { char *auth_data = NULL; auth_t auth_entries[MAX_ENTRIES]; - if (stat(file, &sb) == -1) { - return AUTHFILE_MISSING; - } - - auth_data = calloc(1, sb.st_size); - - if (auth_data == NULL) { - return AUTHFILE_OOM; - } - FILE *pwfile = fopen(file, "r"); if (pwfile == NULL) { - // not strictly necessary but to be safe. - free(auth_data); return AUTHFILE_OPENFAIL; + } else if (fstat(fileno(pwfile), &sb)) { + fclose(pwfile); + return AUTHFILE_STATFAIL; } + auth_data = calloc(1, sb.st_size); + char *auth_cur = auth_data; auth_t *entry_cur = auth_entries; int used = 0; diff --git a/authfile.h b/authfile.h index d68c494f29..1ecc1e96e6 100644 --- a/authfile.h +++ b/authfile.h @@ -3,8 +3,8 @@ enum authfile_ret { AUTHFILE_OK = 0, - AUTHFILE_MISSING, AUTHFILE_OOM, + AUTHFILE_STATFAIL, // not likely, but just to be sure AUTHFILE_OPENFAIL, AUTHFILE_MALFORMED, }; diff --git a/memcached.c b/memcached.c index ebcbdee1ca..4cbbc6c161 100644 --- a/memcached.c +++ b/memcached.c @@ -6102,9 +6102,14 @@ int main (int argc, char **argv) { } switch (authfile_load(settings.auth_file)) { - case AUTHFILE_MISSING: // fall through. + case AUTHFILE_STATFAIL: + vperror("Could not stat authfile [%s], error %s", settings.auth_file + , strerror(errno)); + exit(EXIT_FAILURE); + break; case AUTHFILE_OPENFAIL: - vperror("Could not open authfile [%s] for reading", settings.auth_file); + vperror("Could not open authfile [%s] for reading, error %s", settings.auth_file + , strerror(errno)); exit(EXIT_FAILURE); break; case AUTHFILE_OOM: