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

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