Blob Blame History Raw
From 92d22c7cb9a100a601b9a28cb78b8fbde9dac9a8 Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Wed, 27 Sep 2017 11:28:00 -0400
Subject: [PATCH 46/55] Fix incorrect test case and --remove-args with a value.

Currently we have this test case:

  grubTest grub.3 updargs/g3.4 --update-kernel=ALL --remove-args="hdd=foobar"

This fails to notice that the actual argument in grub.3 is hdd=ide-scsi,
and removes it anyway, and the data in g3.4 supports that behavior.
This is clearly wrong, and so this patch introduces updargs/g3.5, which
leaves hdd=ide-scsi intact, and fixes the code so that it won't modify
the command line in that case.

Resolves: rhbz#1476273

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 grubby.c                  | 71 ++++++++++++++++++++++++++++++++-------
 test.sh                   |  2 +-
 test/results/updargs/g3.5 | 16 +++++++++
 3 files changed, 76 insertions(+), 13 deletions(-)
 create mode 100644 test/results/updargs/g3.5

diff --git a/grubby.c b/grubby.c
index 11ee64a02b9..9af04dea01b 100644
--- a/grubby.c
+++ b/grubby.c
@@ -3563,23 +3563,67 @@ static void removeElement(struct singleLine *line, int removeHere)
 	line->numElements--;
 }
 
-int argMatch(const char *one, const char *two)
+static int argNameMatch(const char *one, const char *two)
 {
 	char *first, *second;
+	char *chptra, *chptrb;
+	int rc;
+
+	first = strcpy(alloca(strlen(one) + 1), one);
+	second = strcpy(alloca(strlen(two) + 1), two);
+
+	chptra = strchr(first, '=');
+	if (chptra)
+		*chptra = '\0';
+
+	chptrb = strchr(second, '=');
+	if (chptrb)
+		*chptrb = '\0';
+
+	rc = strcmp(first, second);
+
+	if (chptra)
+		*chptra = '=';
+	if (chptrb)
+		*chptrb = '=';
+
+	return rc;
+}
+
+static int argHasValue(const char *arg)
+{
 	char *chptr;
 
+	chptr = strchr(arg, '=');
+	if (chptr)
+		return 1;
+	return 0;
+}
+
+static int argValueMatch(const char *one, const char *two)
+{
+	char *first, *second;
+	char *chptra, *chptrb;
+
 	first = strcpy(alloca(strlen(one) + 1), one);
 	second = strcpy(alloca(strlen(two) + 1), two);
 
-	chptr = strchr(first, '=');
-	if (chptr)
-		*chptr = '\0';
+	chptra = strchr(first, '=');
+	if (chptra)
+		chptra += 1;
 
-	chptr = strchr(second, '=');
-	if (chptr)
-		*chptr = '\0';
+	chptrb = strchr(second, '=');
+	if (chptrb)
+		chptrb += 1;
 
-	return strcmp(first, second);
+	if (!chptra && !chptrb)
+		return 0;
+	else if (!chptra)
+		return *chptrb - 0;
+	else if (!chptrb)
+		return 0 - *chptra;
+	else
+		return strcmp(chptra, chptrb);
 }
 
 int updateActualImage(struct grubConfig *cfg, const char *image,
@@ -3723,7 +3767,7 @@ int updateActualImage(struct grubConfig *cfg, const char *image,
 				}
 				if (usedElements[i])
 					continue;
-				if (!argMatch(line->elements[i].item, *arg)) {
+				if (!argNameMatch(line->elements[i].item, *arg)) {
 					usedElements[i] = 1;
 					break;
 				}
@@ -3782,9 +3826,12 @@ int updateActualImage(struct grubConfig *cfg, const char *image,
 				    !strcmp(line->elements[i].item, "--"))
 					/* reached the end of hyper args, stop here */
 					break;
-				if (!argMatch(line->elements[i].item, *arg)) {
-					removeElement(line, i);
-					break;
+				if (!argNameMatch(line->elements[i].item, *arg)) {
+					if (!argHasValue(*arg) ||
+					    !argValueMatch(line->elements[i].item, *arg)) {
+						removeElement(line, i);
+						break;
+					}
 				}
 			}
 			/* handle removing LT_ROOT line too */
diff --git a/test.sh b/test.sh
index aaa6e9323a6..a97ada94d22 100755
--- a/test.sh
+++ b/test.sh
@@ -386,7 +386,7 @@ grubTest grub.3 updargs/g3.2 --update-kernel=DEFAULT \
     --args "root=/dev/hdd1 hdd=notide-scsi"
 grubTest grub.3 updargs/g3.4 --update-kernel=ALL --remove-args="hdd"
 grubTest grub.3 updargs/g3.4 --update-kernel=ALL --remove-args="hdd=ide-scsi"
-grubTest grub.3 updargs/g3.4 --update-kernel=ALL --remove-args="hdd=foobar"
+grubTest grub.3 updargs/g3.5 --update-kernel=ALL --remove-args="hdd=foobar"
 grubTest grub.3 updargs/g3.7 --update-kernel=ALL \
     --remove-args="hdd root ro"
 grubTest grub.7 updargs/g7.2 --boot-filesystem=/    \
diff --git a/test/results/updargs/g3.5 b/test/results/updargs/g3.5
new file mode 100644
index 00000000000..7d50bb87d84
--- /dev/null
+++ b/test/results/updargs/g3.5
@@ -0,0 +1,16 @@
+#boot=/dev/hda
+timeout=10
+splashimage=(hd0,1)/grub/splash.xpm.gz
+title Red Hat Linux (2.4.7-2smp)
+	root (hd0,1)
+	kernel /vmlinuz-2.4.7-2smp ro root=/dev/hda5 hdd=ide-scsi
+	initrd /initrd-2.4.7-2smp.img
+title Red Hat Linux-up (2.4.7-2)
+	root (hd0,1)
+	kernel /vmlinuz-2.4.7-2 ro root=/dev/hda5 hdd=ide-scsi
+	initrd /initrd-2.4.7-2.img
+title DOS
+	rootnoverify (hd0,0)
+	chainloader +1
+
+
-- 
2.17.1