File logrotate-3.8.7-hashIndex-return.patch of Package logrotate.5563

From 2265928c5e85d56d6c64cf696cfaeba325ef39b3 Mon Sep 17 00:00:00 2001
From: Kamil Dudka <kdudka@redhat.com>
Date: Wed, 8 Feb 2017 10:41:47 +0100
Subject: [PATCH] hashIndex: return failure if hash table is not yet allocated

... instead of raising SIGFPE due to division by zero
---
 logrotate.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

Index: logrotate-3.8.7/logrotate.c
===================================================================
--- logrotate-3.8.7.orig/logrotate.c
+++ logrotate-3.8.7/logrotate.c
@@ -215,9 +215,12 @@ static int allocateHash(unsigned int hs)
 }
 
 #define HASH_CONST 13
-static unsigned hashIndex(const char *fn)
+static int hashIndex(const char *fn)
 {
 	unsigned hash = 0;
+	if (!hashSize)
+	    /* hash table not yet allocated */
+	    return -1;
 
 	while (*fn) {
 		hash *= HASH_CONST;
@@ -259,8 +262,11 @@ static struct logState *newState(const c
 
 static struct logState *findState(const char *fn)
 {
-	unsigned int i = hashIndex(fn);
+	const int i = hashIndex(fn);
 	struct logState *p;
+	if (i < 0)
+	    /* hash table not yet allocated */
+	    return NULL;
 
 	for (p = states[i]->head.lh_first; p != NULL; p = p->list.le_next)
 		if (!strcmp(fn, p->fn))
@@ -841,6 +847,9 @@ int findNeedRotating(struct logInfo *log
     }
 
     state = findState(log->files[logNum]);
+    if (!state)
+	    return 1;
+
     state->doRotate = 0;
     state->sb = sb;
 
@@ -1653,11 +1662,12 @@ int rotateLogSet(struct logInfo *log, in
 	}
 
     for (i = 0; i < log->numFiles; i++) {
+	struct logState *logState;
 	logHasErrors[i] = findNeedRotating(log, i, force);
 	hasErrors |= logHasErrors[i];
 
 	/* sure is a lot of findStating going on .. */
-	if ((findState(log->files[i]))->doRotate)
+	if (((logState = findState(log->files[i]))) && logState->doRotate)
 	    numRotated++;
     }
 
@@ -1693,6 +1703,8 @@ int rotateLogSet(struct logInfo *log, in
 	     ((log->flags & LOG_FLAG_SHAREDSCRIPTS) && i < log->numFiles)
 	     || (!(log->flags & LOG_FLAG_SHAREDSCRIPTS) && i == j); i++) {
 	    state[i] = findState(log->files[i]);
+	    if (!state[i])
+		logHasErrors[i] = 1;
 
 	    rotNames[i] = malloc(sizeof(struct logNames));
 	    memset(rotNames[i], 0, sizeof(struct logNames));
openSUSE Build Service is sponsored by