From 6645351bfc2fd6618e6368f6329ec98b935d02d1 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 16 Aug 2017 21:23:26 +0200 Subject: [PATCH] tweaks: avoid trying to stat a file that might be NULL [coverity scan] --- src/files.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/files.c b/src/files.c index 4ba2e4e9..d67e2784 100644 --- a/src/files.c +++ b/src/files.c @@ -2780,6 +2780,11 @@ void load_history(void) char *nanohist = histfilename(); char *legacyhist = legacyhistfilename(); struct stat hstat; + FILE *hist; + + /* If no home directory was found, we can't do anything. */ + if (nanohist == NULL || legacyhist == NULL) + return; /* If there is an old history file, migrate it. */ /* (To be removed in 2018.) */ @@ -2794,13 +2799,11 @@ void load_history(void) legacyhist, nanohist); } - /* Assume do_rcfile() has reported a missing home directory. */ - if (nanohist != NULL) { - FILE *hist = fopen(nanohist, "rb"); + hist = fopen(nanohist, "rb"); if (hist == NULL) { if (errno != ENOENT) { - /* Don't save history when we quit. */ + /* When reading failed, don't save history when we quit. */ UNSET(HISTORYLOG); history_error(N_("Error reading %s: %s"), nanohist, strerror(errno)); @@ -2827,9 +2830,9 @@ void load_history(void) fclose(hist); free(line); } + free(nanohist); free(legacyhist); - } } /* Write the lines of a history list, starting with the line at head, to