From bf1badb72f82afefadda503ab746810441358ef7 Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Fri, 8 Jul 2011 13:58:43 +0200 Subject: [PATCH] Fix #1741 about possible problems with fnmatch --- bacula/src/lib/fnmatch.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/bacula/src/lib/fnmatch.c b/bacula/src/lib/fnmatch.c index c1ec13ded3..b395b11e24 100644 --- a/bacula/src/lib/fnmatch.c +++ b/bacula/src/lib/fnmatch.c @@ -56,20 +56,41 @@ #define RANGE_NOMATCH 0 #define RANGE_ERROR (-1) +/* Limit of recursion during matching attempts. */ +#define FNM_MAX_RECUR 64 + #define ISSET(x, y) ((x) & (y)) #define FOLD(c) ((flags & FNM_CASEFOLD) && B_ISUPPER(c) ? tolower(c) : (c)) static int rangematch(const char *, char, int, char **); +static int r_fnmatch(const char *, const char *, int, int); #ifdef SYS int xfnmatch(const char *pattern, const char *string, int flags) #else int fnmatch(const char *pattern, const char *string, int flags) #endif +{ + int e; + + e = r_fnmatch(pattern, string, flags, FNM_MAX_RECUR); + if (e == -1) { /* Too much recursion */ + e = FNM_NOMATCH; + } + return (e); +} + +static +int r_fnmatch(const char *pattern, const char *string, int flags, int recur) { const char *stringstart; char *newp; char c, test; + int e; + + if (recur-- <= 0) { + return (-1); + } stringstart = string; for ( ;; ) { @@ -118,8 +139,9 @@ int fnmatch(const char *pattern, const char *string, int flags) /* General case, use recursion. */ while ((test = *string) != EOS) { - if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) { - return (0); + e = r_fnmatch(pattern, string, flags & ~FNM_PERIOD, recur); + if (e != FNM_NOMATCH) { /* can be NOMATCH, -1 or MATCH */ + return (e); } if (test == '/' && ISSET(flags, FNM_PATHNAME)) { break; -- 2.39.5