Blame SOURCES/0046-Fix-incorrect-test-case-and-remove-args-with-a-value.patch

cca0c4
From 92d22c7cb9a100a601b9a28cb78b8fbde9dac9a8 Mon Sep 17 00:00:00 2001
cca0c4
From: Peter Jones <pjones@redhat.com>
cca0c4
Date: Wed, 27 Sep 2017 11:28:00 -0400
cca0c4
Subject: [PATCH 46/55] Fix incorrect test case and --remove-args with a value.
cca0c4
cca0c4
Currently we have this test case:
cca0c4
cca0c4
  grubTest grub.3 updargs/g3.4 --update-kernel=ALL --remove-args="hdd=foobar"
cca0c4
cca0c4
This fails to notice that the actual argument in grub.3 is hdd=ide-scsi,
cca0c4
and removes it anyway, and the data in g3.4 supports that behavior.
cca0c4
This is clearly wrong, and so this patch introduces updargs/g3.5, which
cca0c4
leaves hdd=ide-scsi intact, and fixes the code so that it won't modify
cca0c4
the command line in that case.
cca0c4
cca0c4
Resolves: rhbz#1476273
cca0c4
cca0c4
Signed-off-by: Peter Jones <pjones@redhat.com>
cca0c4
---
cca0c4
 grubby.c                  | 71 ++++++++++++++++++++++++++++++++-------
cca0c4
 test.sh                   |  2 +-
cca0c4
 test/results/updargs/g3.5 | 16 +++++++++
cca0c4
 3 files changed, 76 insertions(+), 13 deletions(-)
cca0c4
 create mode 100644 test/results/updargs/g3.5
cca0c4
cca0c4
diff --git a/grubby.c b/grubby.c
cca0c4
index 11ee64a02b9..9af04dea01b 100644
cca0c4
--- a/grubby.c
cca0c4
+++ b/grubby.c
cca0c4
@@ -3563,23 +3563,67 @@ static void removeElement(struct singleLine *line, int removeHere)
cca0c4
 	line->numElements--;
cca0c4
 }
cca0c4
 
cca0c4
-int argMatch(const char *one, const char *two)
cca0c4
+static int argNameMatch(const char *one, const char *two)
cca0c4
 {
cca0c4
 	char *first, *second;
cca0c4
+	char *chptra, *chptrb;
cca0c4
+	int rc;
cca0c4
+
cca0c4
+	first = strcpy(alloca(strlen(one) + 1), one);
cca0c4
+	second = strcpy(alloca(strlen(two) + 1), two);
cca0c4
+
cca0c4
+	chptra = strchr(first, '=');
cca0c4
+	if (chptra)
cca0c4
+		*chptra = '\0';
cca0c4
+
cca0c4
+	chptrb = strchr(second, '=');
cca0c4
+	if (chptrb)
cca0c4
+		*chptrb = '\0';
cca0c4
+
cca0c4
+	rc = strcmp(first, second);
cca0c4
+
cca0c4
+	if (chptra)
cca0c4
+		*chptra = '=';
cca0c4
+	if (chptrb)
cca0c4
+		*chptrb = '=';
cca0c4
+
cca0c4
+	return rc;
cca0c4
+}
cca0c4
+
cca0c4
+static int argHasValue(const char *arg)
cca0c4
+{
cca0c4
 	char *chptr;
cca0c4
 
cca0c4
+	chptr = strchr(arg, '=');
cca0c4
+	if (chptr)
cca0c4
+		return 1;
cca0c4
+	return 0;
cca0c4
+}
cca0c4
+
cca0c4
+static int argValueMatch(const char *one, const char *two)
cca0c4
+{
cca0c4
+	char *first, *second;
cca0c4
+	char *chptra, *chptrb;
cca0c4
+
cca0c4
 	first = strcpy(alloca(strlen(one) + 1), one);
cca0c4
 	second = strcpy(alloca(strlen(two) + 1), two);
cca0c4
 
cca0c4
-	chptr = strchr(first, '=');
cca0c4
-	if (chptr)
cca0c4
-		*chptr = '\0';
cca0c4
+	chptra = strchr(first, '=');
cca0c4
+	if (chptra)
cca0c4
+		chptra += 1;
cca0c4
 
cca0c4
-	chptr = strchr(second, '=');
cca0c4
-	if (chptr)
cca0c4
-		*chptr = '\0';
cca0c4
+	chptrb = strchr(second, '=');
cca0c4
+	if (chptrb)
cca0c4
+		chptrb += 1;
cca0c4
 
cca0c4
-	return strcmp(first, second);
cca0c4
+	if (!chptra && !chptrb)
cca0c4
+		return 0;
cca0c4
+	else if (!chptra)
cca0c4
+		return *chptrb - 0;
cca0c4
+	else if (!chptrb)
cca0c4
+		return 0 - *chptra;
cca0c4
+	else
cca0c4
+		return strcmp(chptra, chptrb);
cca0c4
 }
cca0c4
 
cca0c4
 int updateActualImage(struct grubConfig *cfg, const char *image,
cca0c4
@@ -3723,7 +3767,7 @@ int updateActualImage(struct grubConfig *cfg, const char *image,
cca0c4
 				}
cca0c4
 				if (usedElements[i])
cca0c4
 					continue;
cca0c4
-				if (!argMatch(line->elements[i].item, *arg)) {
cca0c4
+				if (!argNameMatch(line->elements[i].item, *arg)) {
cca0c4
 					usedElements[i] = 1;
cca0c4
 					break;
cca0c4
 				}
cca0c4
@@ -3782,9 +3826,12 @@ int updateActualImage(struct grubConfig *cfg, const char *image,
cca0c4
 				    !strcmp(line->elements[i].item, "--"))
cca0c4
 					/* reached the end of hyper args, stop here */
cca0c4
 					break;
cca0c4
-				if (!argMatch(line->elements[i].item, *arg)) {
cca0c4
-					removeElement(line, i);
cca0c4
-					break;
cca0c4
+				if (!argNameMatch(line->elements[i].item, *arg)) {
cca0c4
+					if (!argHasValue(*arg) ||
cca0c4
+					    !argValueMatch(line->elements[i].item, *arg)) {
cca0c4
+						removeElement(line, i);
cca0c4
+						break;
cca0c4
+					}
cca0c4
 				}
cca0c4
 			}
cca0c4
 			/* handle removing LT_ROOT line too */
cca0c4
diff --git a/test.sh b/test.sh
cca0c4
index aaa6e9323a6..a97ada94d22 100755
cca0c4
--- a/test.sh
cca0c4
+++ b/test.sh
cca0c4
@@ -386,7 +386,7 @@ grubTest grub.3 updargs/g3.2 --update-kernel=DEFAULT \
cca0c4
     --args "root=/dev/hdd1 hdd=notide-scsi"
cca0c4
 grubTest grub.3 updargs/g3.4 --update-kernel=ALL --remove-args="hdd"
cca0c4
 grubTest grub.3 updargs/g3.4 --update-kernel=ALL --remove-args="hdd=ide-scsi"
cca0c4
-grubTest grub.3 updargs/g3.4 --update-kernel=ALL --remove-args="hdd=foobar"
cca0c4
+grubTest grub.3 updargs/g3.5 --update-kernel=ALL --remove-args="hdd=foobar"
cca0c4
 grubTest grub.3 updargs/g3.7 --update-kernel=ALL \
cca0c4
     --remove-args="hdd root ro"
cca0c4
 grubTest grub.7 updargs/g7.2 --boot-filesystem=/    \
cca0c4
diff --git a/test/results/updargs/g3.5 b/test/results/updargs/g3.5
cca0c4
new file mode 100644
cca0c4
index 00000000000..7d50bb87d84
cca0c4
--- /dev/null
cca0c4
+++ b/test/results/updargs/g3.5
cca0c4
@@ -0,0 +1,16 @@
cca0c4
+#boot=/dev/hda
cca0c4
+timeout=10
cca0c4
+splashimage=(hd0,1)/grub/splash.xpm.gz
cca0c4
+title Red Hat Linux (2.4.7-2smp)
cca0c4
+	root (hd0,1)
cca0c4
+	kernel /vmlinuz-2.4.7-2smp ro root=/dev/hda5 hdd=ide-scsi
cca0c4
+	initrd /initrd-2.4.7-2smp.img
cca0c4
+title Red Hat Linux-up (2.4.7-2)
cca0c4
+	root (hd0,1)
cca0c4
+	kernel /vmlinuz-2.4.7-2 ro root=/dev/hda5 hdd=ide-scsi
cca0c4
+	initrd /initrd-2.4.7-2.img
cca0c4
+title DOS
cca0c4
+	rootnoverify (hd0,0)
cca0c4
+	chainloader +1
cca0c4
+
cca0c4
+
cca0c4
-- 
cca0c4
2.17.1
cca0c4