diff mbox

[02/12] libmultipath: fix basenamecpy

Message ID 1521049605-22050-3-git-send-email-bmarzins@redhat.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show

Commit Message

Benjamin Marzinski March 14, 2018, 5:46 p.m. UTC
basenamecpy was returning the wrong answer in multiple cases, as
shown by the unit tests for it. Now it will properly find the
basename (as defined by GNU basename, which works well for all of
multipath's uses) and return a copy, if the basename can fit in
provided buffer.

Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
---
 libmultipath/util.c | 25 ++++++++-----------------
 libmultipath/util.h |  2 +-
 2 files changed, 9 insertions(+), 18 deletions(-)
diff mbox

Patch

diff --git a/libmultipath/util.c b/libmultipath/util.c
index d3dd3eb..7251ad0 100644
--- a/libmultipath/util.c
+++ b/libmultipath/util.c
@@ -30,30 +30,21 @@  strchop(char *str)
 }
 
 int
-basenamecpy (const char * str1, char * str2, int str2len)
+basenamecpy (const char *src, char *dst, size_t size)
 {
-	const char *p;
+	const char *p, *e;
 
-	if (!str1 || !strlen(str1))
+	if (!src || !dst || !strlen(src))
 		return 0;
 
-	if (strlen(str1) >= str2len)
-		return 0;
+	p = basename(src);
 
-	if (!str2)
+	for (e = p + strlen(p) - 1; e >= p && isspace(*e); --e) ;
+	if (e < p || e - p > size - 2)
 		return 0;
 
-	p = str1 + (strlen(str1) - 1);
-
-	while (*--p != '/' && p != str1)
-		continue;
-
-	if (p != str1)
-		p++;
-
-	strncpy(str2, p, str2len);
-	str2[str2len - 1] = '\0';
-	return strchop(str2);
+	strlcpy(dst, p, e - p + 2);
+	return strlen(dst);
 }
 
 int
diff --git a/libmultipath/util.h b/libmultipath/util.h
index 51a6d54..a3ab894 100644
--- a/libmultipath/util.h
+++ b/libmultipath/util.h
@@ -5,7 +5,7 @@ 
 #include <inttypes.h>
 
 size_t strchop(char *);
-int basenamecpy (const char * src, char * dst, int);
+int basenamecpy (const char *src, char *dst, size_t size);
 int filepresent (char * run);
 char *get_next_string(char **temp, char *split_char);
 int get_word (char * sentence, char ** word);