[PATCH 1/2] discover: Allow for empty paths

Klaus Heinrich Kiwi klaus at linux.vnet.ibm.com
Fri Jan 15 12:08:48 AEDT 2021


Some grub tests involve a (device)/path structure, where it is
actually legal to have an empty path. Adjust join_path() and
dependant functions to allow for empty pathnames.

Signed-off-by: Klaus Heinrich Kiwi <klaus at linux.vnet.ibm.com>
---
 discover/grub2/builtins.c |  8 ++++----
 discover/grub2/grub2.c    | 19 ++++++++++---------
 discover/paths.c          |  8 +++++---
 test/parser/utils.c       |  2 +-
 4 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/discover/grub2/builtins.c b/discover/grub2/builtins.c
index ab1407a..31cbe0e 100644
--- a/discover/grub2/builtins.c
+++ b/discover/grub2/builtins.c
@@ -216,7 +216,7 @@ static int parse_to_device_path(struct grub2_script *script,
 		return -1;
 
 	*devp = dev;
-	*pathp = talloc_strdup(script, file->path);
+	*pathp = !file->path ? NULL : talloc_strdup(script, file->path);
 
 	talloc_free(file);
 
@@ -247,13 +247,13 @@ static bool builtin_test_op_file(struct grub2_script *script, char op,
 	switch (op) {
 	case 's':
 		/* -s: return true if file exists and has non-zero size */
-		result = statbuf.st_size > 0;
+		result = !path ? false : statbuf.st_size > 0;
 		break;
 	case 'f':
 		/* -f: return true if file exists and is not a directory. This is
 		 * different than the behavior of "test", but is what GRUB does
 		 * (though note as above that we follow symlinks unlike GRUB). */
-		result = !S_ISDIR(statbuf.st_mode);
+		result = !path ? false : !S_ISDIR(statbuf.st_mode);
 		break;
 	default:
 		result = false;
@@ -284,7 +284,7 @@ static bool builtin_test_op_dir(struct grub2_script *script, char op,
 	if (rc)
 		return false;
 
-	return S_ISDIR(statbuf.st_mode);
+	return !path ? false : S_ISDIR(statbuf.st_mode);
 }
 
 static bool builtin_test_op(struct grub2_script *script,
diff --git a/discover/grub2/grub2.c b/discover/grub2/grub2.c
index b176ce2..52c75e9 100644
--- a/discover/grub2/grub2.c
+++ b/discover/grub2/grub2.c
@@ -118,7 +118,6 @@ struct grub2_file *grub2_parse_file(struct grub2_script *script,
 		const char *str)
 {
 	struct grub2_file *file;
-	size_t dev_len;
 	char *pos;
 
 	if (!str)
@@ -129,6 +128,7 @@ struct grub2_file *grub2_parse_file(struct grub2_script *script,
 	if (*str != '(') {
 		/* just a path - no device, return path as-is */
 		file->path = talloc_strdup(file, str);
+		file->dev = NULL;
 
 	} else {
 		/* device plus path - split into components */
@@ -137,17 +137,18 @@ struct grub2_file *grub2_parse_file(struct grub2_script *script,
 
 		/* no closing bracket, or zero-length path? */
 		if (!pos || *(pos+1) == '\0') {
-			talloc_free(file);
-			return NULL;
+			file->path = NULL;
+		}
+		else {
+			file->path = talloc_strdup(file, pos + 1);
+			file->dev = talloc_strndup(file, str + 1, (size_t) (pos - str - 1));
 		}
-
-		file->path = talloc_strdup(file, pos + 1);
-
-		dev_len = pos - str - 1;
-		if (dev_len)
-			file->dev = talloc_strndup(file, str + 1, dev_len);
 	}
 
+	if (!file->dev && !file->path) {
+		talloc_free(file);
+		return NULL;
+	}
 	return file;
 }
 
diff --git a/discover/paths.c b/discover/paths.c
index 16fdd59..3010ae3 100644
--- a/discover/paths.c
+++ b/discover/paths.c
@@ -55,9 +55,11 @@ char *join_paths(void *alloc_ctx, const char *a, const char *b)
 	full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
 
 	strcpy(full_path, a);
-	if (b[0] != '/' && a[strlen(a) - 1] != '/')
-		strcat(full_path, "/");
-	strcat(full_path, b);
+	if (strlen(b)) {
+		if (b[0] != '/' && a[strlen(a) - 1] != '/')
+			strcat(full_path, "/");
+		strcat(full_path, b);
+	}
 
 	return full_path;
 }
diff --git a/test/parser/utils.c b/test/parser/utils.c
index d8499a4..2705b9a 100644
--- a/test/parser/utils.c
+++ b/test/parser/utils.c
@@ -257,7 +257,7 @@ int parser_stat_path(struct discover_context *ctx,
 	list_for_each_entry(&test->files, file, list) {
 		if (file->dev != dev)
 			continue;
-		if (strcmp(file->name, path))
+		if (path && strcmp(file->name, path))
 			continue;
 
 		statbuf->st_size = (off_t)file->size;
-- 
2.17.1



More information about the Petitboot mailing list