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

3e90b9
From 3afc4c0ed28d443bb71956b07fd45c8cfb07566f Mon Sep 17 00:00:00 2001
3e90b9
From: Nathaniel McCallum <npmccallum@redhat.com>
3e90b9
Date: Fri, 2 Mar 2018 14:59:32 -0500
3e90b9
Subject: [PATCH 2/8] Change return type in getRootSpecifier()
3e90b9
3e90b9
Rather than returning a new allocation of the prefix, just return the
3e90b9
length of the prefix. This change accomplishes a couple things. First,
3e90b9
it reduces some memory leaks since the return value was often never
3e90b9
freed.  Second, it simplifies the caller who is usually only interested
3e90b9
in the length of the prefix.
3e90b9
---
3e90b9
 grubby.c | 54 +++++++++++++++++++++++++++---------------------------
3e90b9
 1 file changed, 27 insertions(+), 27 deletions(-)
3e90b9
3e90b9
diff --git a/grubby.c b/grubby.c
3e90b9
index d4ebb86168d..a062ef8e567 100644
3e90b9
--- a/grubby.c
3e90b9
+++ b/grubby.c
3e90b9
@@ -675,7 +675,7 @@ static int lineWrite(FILE * out, struct singleLine * line,
3e90b9
 		     struct configFileInfo * cfi);
3e90b9
 static int getNextLine(char ** bufPtr, struct singleLine * line,
3e90b9
 		       struct configFileInfo * cfi);
3e90b9
-static char * getRootSpecifier(char * str);
3e90b9
+static size_t getRootSpecifier(const char *str);
3e90b9
 static void requote(struct singleLine *line, struct configFileInfo * cfi);
3e90b9
 static void insertElement(struct singleLine * line,
3e90b9
 			  const char * item, int insertHere,
3e90b9
@@ -1840,7 +1840,7 @@ int suitableImage(struct singleEntry * entry, const char * bootPrefix,
3e90b9
     char * fullName;
3e90b9
     int i;
3e90b9
     char * dev;
3e90b9
-    char * rootspec;
3e90b9
+    size_t rs;
3e90b9
     char * rootdev;
3e90b9
 
3e90b9
     if (skipRemoved && entry->skip) {
3e90b9
@@ -1866,12 +1866,11 @@ int suitableImage(struct singleEntry * entry, const char * bootPrefix,
3e90b9
 
3e90b9
     fullName = alloca(strlen(bootPrefix) + 
3e90b9
 		      strlen(line->elements[1].item) + 1);
3e90b9
-    rootspec = getRootSpecifier(line->elements[1].item);
3e90b9
-    int rootspec_offset = rootspec ? strlen(rootspec) : 0;
3e90b9
+    rs = getRootSpecifier(line->elements[1].item);
3e90b9
     int hasslash = endswith(bootPrefix, '/') ||
3e90b9
-    	beginswith(line->elements[1].item + rootspec_offset, '/');
3e90b9
+	    beginswith(line->elements[1].item + rs, '/');
3e90b9
     sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
3e90b9
-            line->elements[1].item + rootspec_offset);
3e90b9
+		line->elements[1].item + rs);
3e90b9
     if (access(fullName, R_OK)) {
3e90b9
 	notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
3e90b9
 	return 0;
3e90b9
@@ -1952,7 +1951,6 @@ struct singleEntry * findEntryByPath(struct grubConfig * config,
3e90b9
     struct singleLine * line;
3e90b9
     int i;
3e90b9
     char * chptr;
3e90b9
-    char * rootspec = NULL;
3e90b9
     enum lineType_e checkType = LT_KERNEL;
3e90b9
 
3e90b9
     if (isdigit(*kernel)) {
3e90b9
@@ -2044,11 +2042,10 @@ struct singleEntry * findEntryByPath(struct grubConfig * config,
3e90b9
 
3e90b9
 		if (line && line->type != LT_MENUENTRY &&
3e90b9
 			line->numElements >= 2) {
3e90b9
-		    rootspec = getRootSpecifier(line->elements[1].item);
3e90b9
-		    if (!strcmp(line->elements[1].item + 
3e90b9
-				((rootspec != NULL) ? strlen(rootspec) : 0),
3e90b9
-				kernel + strlen(prefix)))
3e90b9
-			break;
3e90b9
+                       if (!strcmp(line->elements[1].item +
3e90b9
+                               getRootSpecifier(line->elements[1].item),
3e90b9
+                               kernel + strlen(prefix)))
3e90b9
+                           break;
3e90b9
 		}
3e90b9
 		if(line->type == LT_MENUENTRY &&
3e90b9
 			!strcmp(line->elements[1].item, kernel))
3e90b9
@@ -2797,11 +2794,11 @@ struct singleLine * addLineTmpl(struct singleEntry * entry,
3e90b9
 
3e90b9
 	/* but try to keep the rootspec from the template... sigh */
3e90b9
 	if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI|LT_KERNEL_16|LT_INITRD_16)) {
3e90b9
-	    char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
3e90b9
-	    if (rootspec != NULL) {
3e90b9
-		free(newLine->elements[1].item);
3e90b9
-		newLine->elements[1].item = 
3e90b9
-		    sdupprintf("%s%s", rootspec, val);
3e90b9
+            size_t rs = getRootSpecifier(tmplLine->elements[1].item);
3e90b9
+            if (rs > 0) {
3e90b9
+                free(newLine->elements[1].item);
3e90b9
+                newLine->elements[1].item = sdupprintf("%.*s%s", (int) rs,
3e90b9
+                    tmplLine->elements[1].item, val);
3e90b9
 	    }
3e90b9
 	}
3e90b9
     }
3e90b9
@@ -3729,15 +3726,19 @@ int checkForElilo(struct grubConfig * config) {
3e90b9
     return 1;
3e90b9
 }
3e90b9
 
3e90b9
-static char * getRootSpecifier(char * str) {
3e90b9
-    char * idx, * rootspec = NULL;
3e90b9
+static size_t getRootSpecifier(const char *str)
3e90b9
+{
3e90b9
+    size_t rs = 0;
3e90b9
 
3e90b9
     if (*str == '(') {
3e90b9
-        idx = rootspec = strdup(str);
3e90b9
-        while(*idx && (*idx != ')') && (!isspace(*idx))) idx++;
3e90b9
-        *(++idx) = '\0';
3e90b9
+        for (; str[rs] != ')' && !isspace(str[rs]); rs++) {
3e90b9
+	    if (!str[rs])
3e90b9
+                return rs;
3e90b9
+        }
3e90b9
+        rs++;
3e90b9
     }
3e90b9
-    return rootspec;
3e90b9
+
3e90b9
+    return rs;
3e90b9
 }
3e90b9
 
3e90b9
 static char * getInitrdVal(struct grubConfig * config,
3e90b9
@@ -4616,7 +4617,7 @@ int main(int argc, const char ** argv) {
3e90b9
     if (displayDefault) {
3e90b9
 	struct singleLine * line;
3e90b9
 	struct singleEntry * entry;
3e90b9
-        char * rootspec;
3e90b9
+        size_t rs;
3e90b9
 
3e90b9
 	if (config->defaultImage == -1) return 0;
3e90b9
 	if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
3e90b9
@@ -4629,9 +4630,8 @@ int main(int argc, const char ** argv) {
3e90b9
 	line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3e90b9
 	if (!line) return 0;
3e90b9
 
3e90b9
-        rootspec = getRootSpecifier(line->elements[1].item);
3e90b9
-        printf("%s%s\n", bootPrefix, line->elements[1].item + 
3e90b9
-               ((rootspec != NULL) ? strlen(rootspec) : 0));
3e90b9
+        rs = getRootSpecifier(line->elements[1].item);
3e90b9
+        printf("%s%s\n", bootPrefix, line->elements[1].item + rs);
3e90b9
 
3e90b9
 	return 0;
3e90b9
 
3e90b9
-- 
3e90b9
2.17.1
3e90b9