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

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