From eb6329423193292c3c13e50c0b1f8cf56354b89a Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Fri, 5 Jul 2013 14:00:46 -0400 Subject: [PATCH] Don't call tolower() on the whole filenames, only on extensions tolower() will trigger an assertion failure in the case where the string contains UTF-8 codes. This is not a problem as only the file extension needs to be processed, and that one should contain only ASCII. --- src/filelister.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/filelister.cpp b/src/filelister.cpp index b33186c..6bd3208 100644 --- a/src/filelister.cpp +++ b/src/filelister.cpp @@ -83,14 +83,12 @@ void FileLister::browse(bool clean) vector vfilter; split(vfilter, getFilter(), ","); - string filepath, file, file_lowercase; + string filepath, file; struct stat st; struct dirent *dptr; while ((dptr = readdir(dirp))) { file = dptr->d_name; - file_lowercase = file; - std::transform(file_lowercase.begin(), file_lowercase.end(), file_lowercase.begin(), ::tolower); if (file[0] == '.' && file != "..") continue; @@ -120,8 +118,18 @@ void FileLister::browse(bool clean) continue; for (vector::iterator it = vfilter.begin(); it != vfilter.end(); ++it) { - if (it->length() <= file.length()) { - if (file_lowercase.compare(file.length() - it->length(), it->length(), *it) == 0) { + if (it->length() < file.length()) { + string file_lowercase = + file.substr(file.length() - it->length()); + if (file_lowercase[0] != '.') + continue; + + /* XXX: This won't accept UTF-8 codes. + * Thanksfully file extensions shouldn't contain any. */ + transform(file_lowercase.begin(), file_lowercase.end(), + file_lowercase.begin(), ::tolower); + + if (file_lowercase.compare(0, it->length(), *it) == 0) { files.push_back(file); break; }