Blame SOURCES/0049-Change-return-type-in-getRootSpecifier.patch

cca0c4
From bd789c1aa159fb4992904e7c529789d7defbe332 Mon Sep 17 00:00:00 2001
cca0c4
From: Nathaniel McCallum <npmccallum@redhat.com>
cca0c4
Date: Fri, 2 Mar 2018 14:59:32 -0500
cca0c4
Subject: [PATCH 49/55] Change return type in getRootSpecifier()
cca0c4
cca0c4
Rather than returning a new allocation of the prefix, just return the
cca0c4
length of the prefix. This change accomplishes a couple things. First,
cca0c4
it reduces some memory leaks since the return value was often never
cca0c4
freed.  Second, it simplifies the caller who is usually only interested
cca0c4
in the length of the prefix.
cca0c4
---
cca0c4
 grubby.c | 57 ++++++++++++++++++++++++++------------------------------
cca0c4
 1 file changed, 26 insertions(+), 31 deletions(-)
cca0c4
cca0c4
diff --git a/grubby.c b/grubby.c
cca0c4
index c1b4104892c..1fe850a3ddf 100644
cca0c4
--- a/grubby.c
cca0c4
+++ b/grubby.c
cca0c4
@@ -698,7 +698,7 @@ static int lineWrite(FILE * out, struct singleLine *line,
cca0c4
 		     struct configFileInfo *cfi);
cca0c4
 static int getNextLine(char **bufPtr, struct singleLine *line,
cca0c4
 		       struct configFileInfo *cfi);
cca0c4
-static char *getRootSpecifier(char *str);
cca0c4
+static size_t getRootSpecifier(const char *str);
cca0c4
 static void requote(struct singleLine *line, struct configFileInfo *cfi);
cca0c4
 static void insertElement(struct singleLine *line,
cca0c4
 			  const char *item, int insertHere,
cca0c4
@@ -2122,7 +2122,7 @@ int suitableImage(struct singleEntry *entry, const char *bootPrefix,
cca0c4
 	char *fullName;
cca0c4
 	int i;
cca0c4
 	char *dev;
cca0c4
-	char *rootspec;
cca0c4
+	size_t rs;
cca0c4
 	char *rootdev;
cca0c4
 
cca0c4
 	if (skipRemoved && entry->skip) {
cca0c4
@@ -2150,12 +2150,11 @@ int suitableImage(struct singleEntry *entry, const char *bootPrefix,
cca0c4
 
cca0c4
 	fullName = alloca(strlen(bootPrefix) +
cca0c4
 			  strlen(line->elements[1].item) + 1);
cca0c4
-	rootspec = getRootSpecifier(line->elements[1].item);
cca0c4
-	int rootspec_offset = rootspec ? strlen(rootspec) : 0;
cca0c4
+	rs = getRootSpecifier(line->elements[1].item);
cca0c4
 	int hasslash = endswith(bootPrefix, '/') ||
cca0c4
-	    beginswith(line->elements[1].item + rootspec_offset, '/');
cca0c4
+	    beginswith(line->elements[1].item + rs, '/');
cca0c4
 	sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
cca0c4
-		line->elements[1].item + rootspec_offset);
cca0c4
+		line->elements[1].item + rs);
cca0c4
 	if (access(fullName, R_OK)) {
cca0c4
 		notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
cca0c4
 		return 0;
cca0c4
@@ -2247,7 +2246,6 @@ struct singleEntry *findEntryByPath(struct grubConfig *config,
cca0c4
 	struct singleLine *line;
cca0c4
 	int i;
cca0c4
 	char *chptr;
cca0c4
-	char *rootspec = NULL;
cca0c4
 	enum lineType_e checkType = LT_KERNEL;
cca0c4
 
cca0c4
 	if (isdigit(*kernel)) {
cca0c4
@@ -2352,14 +2350,10 @@ struct singleEntry *findEntryByPath(struct grubConfig *config,
cca0c4
 
cca0c4
 				if (line && line->type != LT_MENUENTRY &&
cca0c4
 				    line->numElements >= 2) {
cca0c4
-					rootspec =
cca0c4
-					    getRootSpecifier(line->elements[1].
cca0c4
-							     item);
cca0c4
-					if (!strcmp
cca0c4
-					    (line->elements[1].item +
cca0c4
-					     ((rootspec !=
cca0c4
-					       NULL) ? strlen(rootspec) : 0),
cca0c4
-					     kernel + strlen(prefix)))
cca0c4
+					if (!strcmp(line->elements[1].item +
cca0c4
+						getRootSpecifier(
cca0c4
+							line->elements[1].item),
cca0c4
+						kernel + strlen(prefix)))
cca0c4
 						break;
cca0c4
 				}
cca0c4
 				if (line->type == LT_MENUENTRY &&
cca0c4
@@ -3268,12 +3262,12 @@ struct singleLine *addLineTmpl(struct singleEntry *entry,
cca0c4
 		    type & (LT_HYPER | LT_KERNEL | LT_MBMODULE | LT_INITRD |
cca0c4
 			    LT_KERNEL_EFI | LT_INITRD_EFI | LT_KERNEL_16 |
cca0c4
 			    LT_INITRD_16)) {
cca0c4
-			char *rootspec =
cca0c4
-			    getRootSpecifier(tmplLine->elements[1].item);
cca0c4
-			if (rootspec != NULL) {
cca0c4
+			size_t rs = getRootSpecifier(tmplLine->elements[1].item);
cca0c4
+			if (rs > 0) {
cca0c4
 				free(newLine->elements[1].item);
cca0c4
-				newLine->elements[1].item =
cca0c4
-				    sdupprintf("%s%s", rootspec, val);
cca0c4
+				newLine->elements[1].item = sdupprintf(
cca0c4
+					"%.*s%s", (int) rs,
cca0c4
+					tmplLine->elements[1].item, val);
cca0c4
 			}
cca0c4
 		}
cca0c4
 	}
cca0c4
@@ -4325,17 +4319,19 @@ int checkForElilo(struct grubConfig *config)
cca0c4
 	return 1;
cca0c4
 }
cca0c4
 
cca0c4
-static char *getRootSpecifier(char *str)
cca0c4
+static size_t getRootSpecifier(const char *str)
cca0c4
 {
cca0c4
-	char *idx, *rootspec = NULL;
cca0c4
+	size_t rs = 0;
cca0c4
 
cca0c4
 	if (*str == '(') {
cca0c4
-		idx = rootspec = strdup(str);
cca0c4
-		while (*idx && (*idx != ')') && (!isspace(*idx)))
cca0c4
-			idx++;
cca0c4
-		*(++idx) = '\0';
cca0c4
+		for (; str[rs] != ')' && !isspace(str[rs]); rs++) {
cca0c4
+			if (!str[rs])
cca0c4
+				return rs;
cca0c4
+		}
cca0c4
+		rs++;
cca0c4
 	}
cca0c4
-	return rootspec;
cca0c4
+
cca0c4
+	return rs;
cca0c4
 }
cca0c4
 
cca0c4
 static char *getInitrdVal(struct grubConfig *config,
cca0c4
@@ -5365,7 +5361,7 @@ int main(int argc, const char **argv)
cca0c4
 	if (displayDefault) {
cca0c4
 		struct singleLine *line;
cca0c4
 		struct singleEntry *entry;
cca0c4
-		char *rootspec;
cca0c4
+		size_t rs;
cca0c4
 
cca0c4
 		if (config->defaultImage == NO_DEFAULT_ENTRY)
cca0c4
 			return 0;
cca0c4
@@ -5384,9 +5380,8 @@ int main(int argc, const char **argv)
cca0c4
 		if (!line)
cca0c4
 			return 0;
cca0c4
 
cca0c4
-		rootspec = getRootSpecifier(line->elements[1].item);
cca0c4
-		printf("%s%s\n", bootPrefix, line->elements[1].item +
cca0c4
-		       ((rootspec != NULL) ? strlen(rootspec) : 0));
cca0c4
+		rs = getRootSpecifier(line->elements[1].item);
cca0c4
+		printf("%s%s\n", bootPrefix, line->elements[1].item + rs);
cca0c4
 
cca0c4
 		return 0;
cca0c4
 
cca0c4
-- 
cca0c4
2.17.1
cca0c4