Skip to content

Commit

Permalink
Improve opening of authentication file
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasKorbar authored and dormando committed Oct 26, 2020
1 parent b031143 commit 8335dd8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
17 changes: 5 additions & 12 deletions authfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion authfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
9 changes: 7 additions & 2 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8335dd8

Please sign in to comment.