Blame SOURCES/0002-logrotate-3.14.0-coverity.patch

f68c4d
From a4ac21e9a8cfe8a73471a195308a742e07d7fe8d Mon Sep 17 00:00:00 2001
f68c4d
From: Kamil Dudka <kdudka@redhat.com>
f68c4d
Date: Wed, 1 Aug 2018 15:32:38 +0200
f68c4d
Subject: [PATCH 1/3] readConfigFile: assign and check 'key' separately
f68c4d
f68c4d
... to make the code readable.  No changes in behavior intended
f68c4d
by this commit.
f68c4d
---
f68c4d
 config.c | 312 +++++++++++++++++++++++++++----------------------------
f68c4d
 1 file changed, 152 insertions(+), 160 deletions(-)
f68c4d
f68c4d
diff --git a/config.c b/config.c
f68c4d
index 84db36f..d2fba10 100644
f68c4d
--- a/config.c
f68c4d
+++ b/config.c
f68c4d
@@ -1037,7 +1037,8 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 			}
f68c4d
 
f68c4d
 			if (isalpha((unsigned char)*start)) {
f68c4d
-				if ((key = isolateWord(&start, &buf, length)) == NULL)
f68c4d
+				key = isolateWord(&start, &buf, length);
f68c4d
+				if (key == NULL)
f68c4d
 					continue;
f68c4d
 				if (!strcmp(key, "compress")) {
f68c4d
 					newlog->flags |= LOG_FLAG_COMPRESS;
f68c4d
@@ -1191,16 +1192,16 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 					}
f68c4d
 				} else if (!strcmp(key, "shredcycles")) {
f68c4d
 					free(key);
f68c4d
-					if ((key = isolateValue(configFile, lineNum, "shred cycles",
f68c4d
-							&start, &buf, length)) != NULL) {
f68c4d
-						newlog->shred_cycles = strtoul(key, &chptr, 0);
f68c4d
-						if (*chptr || newlog->shred_cycles < 0) {
f68c4d
-							message(MESS_ERROR, "%s:%d bad shred cycles '%s'\n",
f68c4d
-									configFile, lineNum, key);
f68c4d
-							goto error;
f68c4d
-						}
f68c4d
+					key = isolateValue(configFile, lineNum, "shred cycles",
f68c4d
+							&start, &buf, length);
f68c4d
+					if (key == NULL)
f68c4d
+						continue;
f68c4d
+					newlog->shred_cycles = strtoul(key, &chptr, 0);
f68c4d
+					if (*chptr || newlog->shred_cycles < 0) {
f68c4d
+						message(MESS_ERROR, "%s:%d bad shred cycles '%s'\n",
f68c4d
+								configFile, lineNum, key);
f68c4d
+						goto error;
f68c4d
 					}
f68c4d
-					else continue;
f68c4d
 				} else if (!strcmp(key, "hourly")) {
f68c4d
 					newlog->criterium = ROT_HOURLY;
f68c4d
 				} else if (!strcmp(key, "daily")) {
f68c4d
@@ -1232,59 +1233,53 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 					newlog->criterium = ROT_YEARLY;
f68c4d
 				} else if (!strcmp(key, "rotate")) {
f68c4d
 					free(key);
f68c4d
-					if ((key = isolateValue
f68c4d
-						(configFile, lineNum, "rotate count", &start,
f68c4d
-						&buf, length)) != NULL) {
f68c4d
-
f68c4d
-						newlog->rotateCount = strtoul(key, &chptr, 0);
f68c4d
-						if (*chptr || newlog->rotateCount < 0) {
f68c4d
-							message(MESS_ERROR,
f68c4d
-								"%s:%d bad rotation count '%s'\n",
f68c4d
-								configFile, lineNum, key);
f68c4d
-							RAISE_ERROR();
f68c4d
-						}
f68c4d
+					key = isolateValue(configFile, lineNum, "rotate count", &start,
f68c4d
+						&buf, length);
f68c4d
+					if (key == NULL)
f68c4d
+						continue;
f68c4d
+					newlog->rotateCount = strtoul(key, &chptr, 0);
f68c4d
+					if (*chptr || newlog->rotateCount < 0) {
f68c4d
+						message(MESS_ERROR,
f68c4d
+							"%s:%d bad rotation count '%s'\n",
f68c4d
+							configFile, lineNum, key);
f68c4d
+						RAISE_ERROR();
f68c4d
 					}
f68c4d
-					else continue;
f68c4d
 				} else if (!strcmp(key, "start")) {
f68c4d
 					free(key);
f68c4d
-					if ((key = isolateValue
f68c4d
-						(configFile, lineNum, "start count", &start,
f68c4d
-						&buf, length)) != NULL) {
f68c4d
-
f68c4d
-						newlog->logStart = strtoul(key, &chptr, 0);
f68c4d
-						if (*chptr || newlog->logStart < 0) {
f68c4d
-							message(MESS_ERROR, "%s:%d bad start count '%s'\n",
f68c4d
-								configFile, lineNum, key);
f68c4d
-							RAISE_ERROR();
f68c4d
-						}
f68c4d
+					key = isolateValue(configFile, lineNum, "start count", &start,
f68c4d
+						&buf, length);
f68c4d
+					if (key == NULL)
f68c4d
+						continue;
f68c4d
+					newlog->logStart = strtoul(key, &chptr, 0);
f68c4d
+					if (*chptr || newlog->logStart < 0) {
f68c4d
+						message(MESS_ERROR, "%s:%d bad start count '%s'\n",
f68c4d
+							configFile, lineNum, key);
f68c4d
+						RAISE_ERROR();
f68c4d
 					}
f68c4d
-					else continue;
f68c4d
 				} else if (!strcmp(key, "minage")) {
f68c4d
 					free(key);
f68c4d
-					if ((key = isolateValue
f68c4d
-						(configFile, lineNum, "minage count", &start,
f68c4d
-						&buf, length)) != NULL) {
f68c4d
-						newlog->rotateMinAge = strtoul(key, &chptr, 0);
f68c4d
-						if (*chptr || newlog->rotateMinAge < 0) {
f68c4d
-							message(MESS_ERROR, "%s:%d bad minimum age '%s'\n",
f68c4d
-								configFile, lineNum, start);
f68c4d
-							RAISE_ERROR();
f68c4d
-						}
f68c4d
+					key = isolateValue(configFile, lineNum, "minage count", &start,
f68c4d
+						&buf, length);
f68c4d
+					if (key == NULL)
f68c4d
+						continue;
f68c4d
+					newlog->rotateMinAge = strtoul(key, &chptr, 0);
f68c4d
+					if (*chptr || newlog->rotateMinAge < 0) {
f68c4d
+						message(MESS_ERROR, "%s:%d bad minimum age '%s'\n",
f68c4d
+							configFile, lineNum, start);
f68c4d
+						RAISE_ERROR();
f68c4d
 					}
f68c4d
-					else continue;
f68c4d
 				} else if (!strcmp(key, "maxage")) {
f68c4d
 					free(key);
f68c4d
-					if ((key = isolateValue
f68c4d
-						(configFile, lineNum, "maxage count", &start,
f68c4d
-						&buf, length)) != NULL) {
f68c4d
-						newlog->rotateAge = strtoul(key, &chptr, 0);
f68c4d
-						if (*chptr || newlog->rotateAge < 0) {
f68c4d
-							message(MESS_ERROR, "%s:%d bad maximum age '%s'\n",
f68c4d
-								configFile, lineNum, start);
f68c4d
-							RAISE_ERROR();
f68c4d
-						}
f68c4d
+					key = isolateValue(configFile, lineNum, "maxage count", &start,
f68c4d
+						&buf, length);
f68c4d
+					if (key == NULL)
f68c4d
+						continue;
f68c4d
+					newlog->rotateAge = strtoul(key, &chptr, 0);
f68c4d
+					if (*chptr || newlog->rotateAge < 0) {
f68c4d
+						message(MESS_ERROR, "%s:%d bad maximum age '%s'\n",
f68c4d
+							configFile, lineNum, start);
f68c4d
+						RAISE_ERROR();
f68c4d
 					}
f68c4d
-					else continue;
f68c4d
 				} else if (!strcmp(key, "errors")) {
f68c4d
 					message(MESS_DEBUG,
f68c4d
 						"%s: %d: the errors directive is deprecated and no longer used.\n",
f68c4d
@@ -1337,48 +1332,48 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 						continue;
f68c4d
 					}
f68c4d
 					free(key);
f68c4d
-					if ((key = isolateValue(configFile, lineNum, "tabooext", &start,
f68c4d
-							&buf, length)) != NULL) {
f68c4d
-						endtag = key;
f68c4d
-						if (*endtag == '+') {
f68c4d
+					key = isolateValue(configFile, lineNum, "tabooext", &start,
f68c4d
+							&buf, length);
f68c4d
+					if (key == NULL)
f68c4d
+						continue;
f68c4d
+					endtag = key;
f68c4d
+					if (*endtag == '+') {
f68c4d
+						endtag++;
f68c4d
+						while (isspace((unsigned char)*endtag) && *endtag)
f68c4d
 							endtag++;
f68c4d
-							while (isspace((unsigned char)*endtag) && *endtag)
f68c4d
-								endtag++;
f68c4d
-						} else {
f68c4d
-							free_2d_array(tabooPatterns, tabooCount);
f68c4d
-							tabooCount = 0;
f68c4d
-							/* realloc of NULL is safe by definition */
f68c4d
-							tabooPatterns = NULL;
f68c4d
-						}
f68c4d
-
f68c4d
-						while (*endtag) {
f68c4d
-							int bytes;
f68c4d
-							char *pattern = NULL;
f68c4d
+					} else {
f68c4d
+						free_2d_array(tabooPatterns, tabooCount);
f68c4d
+						tabooCount = 0;
f68c4d
+						/* realloc of NULL is safe by definition */
f68c4d
+						tabooPatterns = NULL;
f68c4d
+					}
f68c4d
 
f68c4d
-							chptr = endtag;
f68c4d
-							while (!isspace((unsigned char)*chptr) && *chptr != ',' && *chptr)
f68c4d
-								chptr++;
f68c4d
+					while (*endtag) {
f68c4d
+						int bytes;
f68c4d
+						char *pattern = NULL;
f68c4d
 
f68c4d
-							/* accept only non-empty patterns to avoid exclusion of everything */
f68c4d
-							if (endtag < chptr) {
f68c4d
-								tabooPatterns = realloc(tabooPatterns, sizeof(*tabooPatterns) *
f68c4d
-											(tabooCount + 1));
f68c4d
-								bytes = asprintf(&pattern, "*%.*s", (int)(chptr - endtag), endtag);
f68c4d
+						chptr = endtag;
f68c4d
+						while (!isspace((unsigned char)*chptr) && *chptr != ',' && *chptr)
f68c4d
+							chptr++;
f68c4d
 
f68c4d
-								/* should test for malloc() failure */
f68c4d
-								assert(bytes != -1);
f68c4d
-								tabooPatterns[tabooCount] = pattern;
f68c4d
-								tabooCount++;
f68c4d
-							}
f68c4d
+						/* accept only non-empty patterns to avoid exclusion of everything */
f68c4d
+						if (endtag < chptr) {
f68c4d
+							tabooPatterns = realloc(tabooPatterns, sizeof(*tabooPatterns) *
f68c4d
+										(tabooCount + 1));
f68c4d
+							bytes = asprintf(&pattern, "*%.*s", (int)(chptr - endtag), endtag);
f68c4d
 
f68c4d
-							endtag = chptr;
f68c4d
-							if (*endtag == ',')
f68c4d
-								endtag++;
f68c4d
-							while (*endtag && isspace((unsigned char)*endtag))
f68c4d
-								endtag++;
f68c4d
+							/* should test for malloc() failure */
f68c4d
+							assert(bytes != -1);
f68c4d
+							tabooPatterns[tabooCount] = pattern;
f68c4d
+							tabooCount++;
f68c4d
 						}
f68c4d
+
f68c4d
+						endtag = chptr;
f68c4d
+						if (*endtag == ',')
f68c4d
+							endtag++;
f68c4d
+						while (*endtag && isspace((unsigned char)*endtag))
f68c4d
+							endtag++;
f68c4d
 					}
f68c4d
-					else continue;
f68c4d
 				} else if (!strcmp(key, "taboopat")) {
f68c4d
 					if (newlog != defConfig) {
f68c4d
 						message(MESS_ERROR,
f68c4d
@@ -1389,68 +1384,68 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 						continue;
f68c4d
 					}
f68c4d
 					free(key);
f68c4d
-					if ((key = isolateValue(configFile, lineNum, "taboopat", &start,
f68c4d
-							&buf, length)) != NULL) {
f68c4d
-						endtag = key;
f68c4d
-						if (*endtag == '+') {
f68c4d
+					key = isolateValue(configFile, lineNum, "taboopat", &start,
f68c4d
+							&buf, length);
f68c4d
+					if (key == NULL)
f68c4d
+						continue;
f68c4d
+
f68c4d
+					endtag = key;
f68c4d
+					if (*endtag == '+') {
f68c4d
+						endtag++;
f68c4d
+						while (isspace((unsigned char)*endtag) && *endtag)
f68c4d
 							endtag++;
f68c4d
-							while (isspace((unsigned char)*endtag) && *endtag)
f68c4d
-								endtag++;
f68c4d
-						} else {
f68c4d
-							free_2d_array(tabooPatterns, tabooCount);
f68c4d
-							tabooCount = 0;
f68c4d
-							/* realloc of NULL is safe by definition */
f68c4d
-							tabooPatterns = NULL;
f68c4d
-						}
f68c4d
+					} else {
f68c4d
+						free_2d_array(tabooPatterns, tabooCount);
f68c4d
+						tabooCount = 0;
f68c4d
+						/* realloc of NULL is safe by definition */
f68c4d
+						tabooPatterns = NULL;
f68c4d
+					}
f68c4d
 
f68c4d
-						while (*endtag) {
f68c4d
-							int bytes;
f68c4d
-							char *pattern = NULL;
f68c4d
+					while (*endtag) {
f68c4d
+						int bytes;
f68c4d
+						char *pattern = NULL;
f68c4d
 
f68c4d
-							chptr = endtag;
f68c4d
-							while (!isspace((unsigned char)*chptr) && *chptr != ',' && *chptr)
f68c4d
-								chptr++;
f68c4d
+						chptr = endtag;
f68c4d
+						while (!isspace((unsigned char)*chptr) && *chptr != ',' && *chptr)
f68c4d
+							chptr++;
f68c4d
 
f68c4d
-							tabooPatterns = realloc(tabooPatterns, sizeof(*tabooPatterns) *
f68c4d
-										(tabooCount + 1));
f68c4d
-							bytes = asprintf(&pattern, "%.*s", (int)(chptr - endtag), endtag);
f68c4d
+						tabooPatterns = realloc(tabooPatterns, sizeof(*tabooPatterns) *
f68c4d
+									(tabooCount + 1));
f68c4d
+						bytes = asprintf(&pattern, "%.*s", (int)(chptr - endtag), endtag);
f68c4d
 
f68c4d
-							/* should test for malloc() failure */
f68c4d
-							assert(bytes != -1);
f68c4d
-							tabooPatterns[tabooCount] = pattern;
f68c4d
-							tabooCount++;
f68c4d
+						/* should test for malloc() failure */
f68c4d
+						assert(bytes != -1);
f68c4d
+						tabooPatterns[tabooCount] = pattern;
f68c4d
+						tabooCount++;
f68c4d
 
f68c4d
-							endtag = chptr;
f68c4d
-							if (*endtag == ',')
f68c4d
-								endtag++;
f68c4d
-							while (*endtag && isspace((unsigned char)*endtag))
f68c4d
-								endtag++;
f68c4d
-						}
f68c4d
+						endtag = chptr;
f68c4d
+						if (*endtag == ',')
f68c4d
+							endtag++;
f68c4d
+						while (*endtag && isspace((unsigned char)*endtag))
f68c4d
+							endtag++;
f68c4d
 					}
f68c4d
-					else continue;
f68c4d
 				} else if (!strcmp(key, "include")) {
f68c4d
 					free(key);
f68c4d
-					if ((key = isolateValue(configFile, lineNum, "include", &start,
f68c4d
-							&buf, length)) != NULL) {
f68c4d
-
f68c4d
-						message(MESS_DEBUG, "including %s\n", key);
f68c4d
-						if (recursion_depth >= MAX_NESTING) {
f68c4d
-							message(MESS_ERROR, "%s:%d include nesting too deep\n",
f68c4d
-									configFile, lineNum);
f68c4d
-							logerror = 1;
f68c4d
-							continue;
f68c4d
-						}
f68c4d
+					key = isolateValue(configFile, lineNum, "include", &start,
f68c4d
+							&buf, length);
f68c4d
+					if (key == NULL)
f68c4d
+						continue;
f68c4d
+					message(MESS_DEBUG, "including %s\n", key);
f68c4d
+					if (recursion_depth >= MAX_NESTING) {
f68c4d
+						message(MESS_ERROR, "%s:%d include nesting too deep\n",
f68c4d
+								configFile, lineNum);
f68c4d
+						logerror = 1;
f68c4d
+						continue;
f68c4d
+					}
f68c4d
 
f68c4d
-						++recursion_depth;
f68c4d
-						rv = readConfigPath(key, newlog);
f68c4d
-						--recursion_depth;
f68c4d
+					++recursion_depth;
f68c4d
+					rv = readConfigPath(key, newlog);
f68c4d
+					--recursion_depth;
f68c4d
 
f68c4d
-						if (rv) {
f68c4d
-							logerror = 1;
f68c4d
-							continue;
f68c4d
-						}
f68c4d
+					if (rv) {
f68c4d
+						logerror = 1;
f68c4d
+						continue;
f68c4d
 					}
f68c4d
-					else continue;
f68c4d
 				} else if (!strcmp(key, "olddir")) {
f68c4d
 					freeLogItem (oldDir);
f68c4d
 
f68c4d
@@ -1460,28 +1455,23 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 					}
f68c4d
 					message(MESS_DEBUG, "olddir is now %s\n", newlog->oldDir);
f68c4d
 				} else if (!strcmp(key, "extension")) {
f68c4d
-					if ((key = isolateValue
f68c4d
-						(configFile, lineNum, "extension name", &start,
f68c4d
-							&buf, length)) != NULL) {
f68c4d
-						freeLogItem (extension);
f68c4d
-						newlog->extension = key;
f68c4d
-						key = NULL;
f68c4d
-					}
f68c4d
-					else continue;
f68c4d
-
f68c4d
-					message(MESS_DEBUG, "extension is now %s\n",
f68c4d
-						newlog->extension);
f68c4d
+					key = isolateValue(configFile, lineNum, "extension name", &start,
f68c4d
+							&buf, length);
f68c4d
+					if (key == NULL)
f68c4d
+						continue;
f68c4d
+					freeLogItem (extension);
f68c4d
+					newlog->extension = key;
f68c4d
+					key = NULL;
f68c4d
+					message(MESS_DEBUG, "extension is now %s\n", newlog->extension);
f68c4d
 
f68c4d
 				} else if (!strcmp(key, "addextension")) {
f68c4d
-					if ((key = isolateValue
f68c4d
-						(configFile, lineNum, "addextension name", &start,
f68c4d
-							&buf, length)) != NULL) {
f68c4d
-						freeLogItem (addextension);
f68c4d
-						newlog->addextension = key;
f68c4d
-						key = NULL;
f68c4d
-					}
f68c4d
-					else continue;
f68c4d
-
f68c4d
+					key = isolateValue(configFile, lineNum, "addextension name", &start,
f68c4d
+							&buf, length);
f68c4d
+					if (key == NULL)
f68c4d
+						continue;
f68c4d
+					freeLogItem (addextension);
f68c4d
+					newlog->addextension = key;
f68c4d
+					key = NULL;
f68c4d
 					message(MESS_DEBUG, "addextension is now %s\n",
f68c4d
 						newlog->addextension);
f68c4d
 
f68c4d
@@ -1827,7 +1817,8 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 			break;
f68c4d
 		case STATE_LOAD_SCRIPT:
f68c4d
 		case STATE_LOAD_SCRIPT | STATE_SKIP_CONFIG:
f68c4d
-			if ((key = isolateWord(&start, &buf, length)) == NULL)
f68c4d
+			key = isolateWord(&start, &buf, length);
f68c4d
+			if (key == NULL)
f68c4d
 				continue;
f68c4d
 
f68c4d
 			if (strcmp(key, "endscript") == 0) {
f68c4d
@@ -1862,7 +1853,8 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 				newlog = defConfig;
f68c4d
 			}
f68c4d
 			else {
f68c4d
-				if ((key = isolateWord(&start, &buf, length)) == NULL)
f68c4d
+				key = isolateWord(&start, &buf, length);
f68c4d
+				if (key == NULL)
f68c4d
 					continue;
f68c4d
 				if (
f68c4d
 					(strcmp(key, "postrotate") == 0) ||
f68c4d
-- 
f68c4d
2.17.1
f68c4d
f68c4d
f68c4d
From a3a955494999bd4861f14b846c345cbc96715262 Mon Sep 17 00:00:00 2001
f68c4d
From: Kamil Dudka <kdudka@redhat.com>
f68c4d
Date: Wed, 1 Aug 2018 15:09:40 +0200
f68c4d
Subject: [PATCH 2/3] readConfigFile: assign and free 'key' consistently
f68c4d
f68c4d
This commit fixes the following memory leaks (detected by Coverity):
f68c4d
f68c4d
Error: RESOURCE_LEAK:
f68c4d
config.c:1466: overwrite_var: Overwriting "key" in "key = isolateValue(configFile, lineNum, "extension name", &start, &buf, length)" leaks the storage that "key" points to.
f68c4d
f68c4d
Error: RESOURCE_LEAK:
f68c4d
config.c:1479: overwrite_var: Overwriting "key" in "key = isolateValue(configFile, lineNum, "addextension name", &start, &buf, length)" leaks the storage that "key" points to.
f68c4d
f68c4d
Error: RESOURCE_LEAK:
f68c4d
config.c:1043: alloc_fn: Storage is returned from allocation function "isolateWord".
f68c4d
config.c:219:2: alloc_fn: Storage is returned from allocation function "strndup".
f68c4d
config.c:219:2: assign: Assigning: "key" = "strndup(start, endtag - start)".
f68c4d
config.c:221:2: return_alloc: Returning allocated memory "key".
f68c4d
config.c:1043: var_assign: Assigning: "key" = storage returned from "isolateWord(&start, &buf, length)".
f68c4d
config.c:1928: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
f68c4d
f68c4d
Error: RESOURCE_LEAK:
f68c4d
config.c:1153: alloc_fn: Storage is returned from allocation function "isolateValue".
f68c4d
config.c:204:2: alloc_fn: Storage is returned from allocation function "isolateLine".
f68c4d
config.c:178:2: alloc_fn: Storage is returned from allocation function "strndup".
f68c4d
config.c:178:2: assign: Assigning: "key" = "strndup(start, endtag - start + 1L)".
f68c4d
config.c:180:2: return_alloc: Returning allocated memory "key".
f68c4d
config.c:204:2: return_alloc_fn: Directly returning storage allocated by "isolateLine".
f68c4d
config.c:1153: var_assign: Assigning: "key" = storage returned from "isolateValue(configFile, lineNum, opt, &start, &buf, length)".
f68c4d
config.c:1928: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
f68c4d
f68c4d
Error: RESOURCE_LEAK:
f68c4d
config.c:1219: alloc_fn: Storage is returned from allocation function "isolateLine".
f68c4d
config.c:178:2: alloc_fn: Storage is returned from allocation function "strndup".
f68c4d
config.c:178:2: assign: Assigning: "key" = "strndup(start, endtag - start + 1L)".
f68c4d
config.c:180:2: return_alloc: Returning allocated memory "key".
f68c4d
config.c:1219: var_assign: Assigning: "key" = storage returned from "isolateLine(&start, &buf, length)".
f68c4d
config.c:1928: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
f68c4d
f68c4d
Closes #208
f68c4d
---
f68c4d
 config.c | 19 +++++++------------
f68c4d
 1 file changed, 7 insertions(+), 12 deletions(-)
f68c4d
f68c4d
diff --git a/config.c b/config.c
f68c4d
index d2fba10..39c9bc7 100644
f68c4d
--- a/config.c
f68c4d
+++ b/config.c
f68c4d
@@ -1022,10 +1022,6 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 
f68c4d
 	start = buf;
f68c4d
     for (start = buf; start - buf < length; start++) {
f68c4d
-	if (key) {
f68c4d
-		free(key);
f68c4d
-		key = NULL;
f68c4d
-	}
f68c4d
 	switch (state) {
f68c4d
 		case STATE_DEFAULT:
f68c4d
 			if (isblank((unsigned char)*start))
f68c4d
@@ -1037,6 +1033,7 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 			}
f68c4d
 
f68c4d
 			if (isalpha((unsigned char)*start)) {
f68c4d
+				free(key);
f68c4d
 				key = isolateWord(&start, &buf, length);
f68c4d
 				if (key == NULL)
f68c4d
 					continue;
f68c4d
@@ -1455,6 +1452,7 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 					}
f68c4d
 					message(MESS_DEBUG, "olddir is now %s\n", newlog->oldDir);
f68c4d
 				} else if (!strcmp(key, "extension")) {
f68c4d
+					free(key);
f68c4d
 					key = isolateValue(configFile, lineNum, "extension name", &start,
f68c4d
 							&buf, length);
f68c4d
 					if (key == NULL)
f68c4d
@@ -1465,6 +1463,7 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 					message(MESS_DEBUG, "extension is now %s\n", newlog->extension);
f68c4d
 
f68c4d
 				} else if (!strcmp(key, "addextension")) {
f68c4d
+					free(key);
f68c4d
 					key = isolateValue(configFile, lineNum, "addextension name", &start,
f68c4d
 							&buf, length);
f68c4d
 					if (key == NULL)
f68c4d
@@ -1557,8 +1556,6 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 					if (*start != '\n')
f68c4d
 						state = STATE_SKIP_LINE;
f68c4d
 				}
f68c4d
-				free(key);
f68c4d
-				key = NULL;
f68c4d
 			} else if (*start == '/' || *start == '"' || *start == '\''
f68c4d
 #ifdef GLOB_TILDE
f68c4d
                                                                            || *start == '~'
f68c4d
@@ -1817,6 +1814,7 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 			break;
f68c4d
 		case STATE_LOAD_SCRIPT:
f68c4d
 		case STATE_LOAD_SCRIPT | STATE_SKIP_CONFIG:
f68c4d
+			free(key);
f68c4d
 			key = isolateWord(&start, &buf, length);
f68c4d
 			if (key == NULL)
f68c4d
 				continue;
f68c4d
@@ -1853,6 +1851,7 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 				newlog = defConfig;
f68c4d
 			}
f68c4d
 			else {
f68c4d
+				free(key);
f68c4d
 				key = isolateWord(&start, &buf, length);
f68c4d
 				if (key == NULL)
f68c4d
 					continue;
f68c4d
@@ -1884,8 +1883,6 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 						state = STATE_SKIP_LINE | STATE_SKIP_CONFIG;
f68c4d
 					}
f68c4d
 				}
f68c4d
-				free(key);
f68c4d
-				key = NULL;
f68c4d
 			}
f68c4d
 			break;
f68c4d
 		default:
f68c4d
@@ -1893,10 +1890,6 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 				"%s: %d: readConfigFile() unknown state\n",
f68c4d
 				configFile, lineNum);
f68c4d
 	}
f68c4d
-	if (key) {
f68c4d
-		free(key);
f68c4d
-		key = NULL;
f68c4d
-	}
f68c4d
 	if (*start == '\n') {
f68c4d
 	    lineNum++;
f68c4d
 	}
f68c4d
@@ -1910,6 +1903,8 @@ static int readConfigFile(const char *configFile, struct logInfo *defConfig)
f68c4d
 	goto error;
f68c4d
     }
f68c4d
 
f68c4d
+    free(key);
f68c4d
+
f68c4d
 	munmap(buf, (size_t) length);
f68c4d
 	close(fd);
f68c4d
     return logerror;
f68c4d
-- 
f68c4d
2.17.1
f68c4d
f68c4d
f68c4d
From 771af94fd6c6299a7cb3d20c8b247591775653d3 Mon Sep 17 00:00:00 2001
f68c4d
From: Kamil Dudka <kdudka@redhat.com>
f68c4d
Date: Wed, 1 Aug 2018 16:06:27 +0200
f68c4d
Subject: [PATCH 3/3] simplify code of prerotateSingleLog()
f68c4d
f68c4d
... to eliminate a use-after-free false positive reported by Coverity:
f68c4d
f68c4d
Error: USE_AFTER_FREE:
f68c4d
logrotate.c:1800: freed_arg: "free" frees "oldName".
f68c4d
logrotate.c:1779: use_after_free: Using freed pointer "oldName".
f68c4d
f68c4d
Closes #209
f68c4d
---
f68c4d
 logrotate.c | 10 +++++-----
f68c4d
 1 file changed, 5 insertions(+), 5 deletions(-)
f68c4d
f68c4d
diff --git a/logrotate.c b/logrotate.c
f68c4d
index 02d45e9..95fd70b 100644
f68c4d
--- a/logrotate.c
f68c4d
+++ b/logrotate.c
f68c4d
@@ -1353,7 +1353,7 @@ static int prerotateSingleLog(struct logInfo *log, int logNum,
f68c4d
 			      struct logState *state, struct logNames *rotNames)
f68c4d
 {
f68c4d
     struct tm now = *localtime(&nowSecs);
f68c4d
-    char *oldName, *newName = NULL;
f68c4d
+    char *oldName = NULL;
f68c4d
     const char *compext = "";
f68c4d
     const char *fileext = "";
f68c4d
     int hasErrors = 0;
f68c4d
@@ -1670,6 +1670,7 @@ static int prerotateSingleLog(struct logInfo *log, int logNum,
f68c4d
 	free(glob_pattern);
f68c4d
     } else {
f68c4d
 	int i;
f68c4d
+	char *newName = NULL;
f68c4d
 	if (log->rotateAge) {
f68c4d
 	    struct stat fst_buf;
f68c4d
 	    for (i = 1; i <= rotateCount + 1; i++) {
f68c4d
@@ -1697,7 +1698,6 @@ static int prerotateSingleLog(struct logInfo *log, int logNum,
f68c4d
 		compext) < 0) {
f68c4d
 	    message(MESS_FATAL, "could not allocate disposeName memory\n");
f68c4d
 	}
f68c4d
-	newName = strdup(oldName);
f68c4d
 
f68c4d
 	rotNames->disposeName = strdup(oldName);
f68c4d
 
f68c4d
@@ -1711,6 +1711,8 @@ static int prerotateSingleLog(struct logInfo *log, int logNum,
f68c4d
 		if (asprintf(&oldName, "%s/%s.%d%s%s", rotNames->dirName,
f68c4d
 		    rotNames->baseName, i, fileext, compext) < 0) {
f68c4d
 		    message(MESS_FATAL, "could not allocate oldName memory\n");
f68c4d
+		    oldName = NULL;
f68c4d
+		    break;
f68c4d
 		}
f68c4d
 
f68c4d
 	    message(MESS_DEBUG,
f68c4d
@@ -1727,11 +1729,9 @@ static int prerotateSingleLog(struct logInfo *log, int logNum,
f68c4d
 		    hasErrors = 1;
f68c4d
 		}
f68c4d
 	    }
f68c4d
-	    if (hasErrors || i - 1 < 0)
f68c4d
-		    free(oldName);
f68c4d
-
f68c4d
 	}
f68c4d
 	free(newName);
f68c4d
+	free(oldName);
f68c4d
     }				/* !LOG_FLAG_DATEEXT */
f68c4d
 
f68c4d
 	if (log->flags & LOG_FLAG_DATEEXT) {
f68c4d
-- 
f68c4d
2.17.1
f68c4d