[PATCH 2/7] lib/glob: treat trailing backslash as literal character

Josh Law posted 7 patches 3 weeks, 1 day ago
[PATCH 2/7] lib/glob: treat trailing backslash as literal character
Posted by Josh Law 3 weeks, 1 day ago
A pattern ending with a lone backslash (e.g., "path\\") reads the
NUL terminator as the escaped character, making the backslash
effectively match end-of-string rather than a literal '\' character.
This means glob_match("path\\", "path\\") would fail since the
pattern consumes the NUL early, while glob_match("path\\", "path")
would unexpectedly succeed.

Guard the escape so that a trailing backslash keeps d = '\\' and
falls through to literal matching, which is the intuitive behavior:
a trailing backslash matches a literal backslash character.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/glob.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/glob.c b/lib/glob.c
index cb45a9a47f28..df7f00619b1b 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -112,7 +112,8 @@ bool __pure glob_match(char const *pat, char const *str)
 			}
 			break;
 		case '\\':
-			d = *pat++;
+			if (*pat != '\0')
+				d = *pat++;
 			fallthrough;
 		default:	/* Literal character */
 literal:
-- 
2.34.1