diff mbox series

[v2,23/49] libmultipath: is_mpath_part(): improve parsing

Message ID 20240712171458.77611-24-mwilck@suse.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show
Series multipath-tools: devmapper API refactored | expand

Commit Message

Martin Wilck July 12, 2024, 5:14 p.m. UTC
Use sscanf to make the parsing of the UUID more robust.

Signed-off-by: Martin Wilck <mwilck@suse.com>
Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
---
 libmultipath/devmapper.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/libmultipath/devmapper.c b/libmultipath/devmapper.c
index 0bc5e34..f80fbdf 100644
--- a/libmultipath/devmapper.c
+++ b/libmultipath/devmapper.c
@@ -857,23 +857,20 @@  int dm_get_uuid(const char *name, char *uuid, int uuid_len)
 
 static int is_mpath_part(const char *part_name, const char *map_name)
 {
-	char *p;
-	char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN];
+	char part_uuid[DM_UUID_LEN], map_uuid[DM_UUID_LEN], c;
+	int np, nc;
 
 	if (dm_get_dm_uuid(part_name, part_uuid) != DMP_OK)
 		return 0;
 
+	if (2 != sscanf(part_uuid, "part%d-%n" UUID_PREFIX "%c", &np, &nc, &c)
+	    || np <= 0)
+		return 0;
+
 	if (dm_get_dm_uuid(map_name, map_uuid) != DMP_OK)
 		return 0;
 
-	if (strncmp(part_uuid, "part", 4) != 0)
-		return 0;
-
-	p = strstr(part_uuid, UUID_PREFIX);
-	if (p && !strcmp(p, map_uuid))
-		return 1;
-
-	return 0;
+	return !strcmp(part_uuid + nc, map_uuid);
 }
 
 int dm_get_status(const char *name, char **outstatus)