Skip to content

Commit cfe3ced

Browse files
Peng Wulotem
authored andcommitted
Fix crash when read the config file
1 parent 7ae51be commit cfe3ced

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/Config.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
#include <sys/stat.h>
1920
#include <fstream>
2021
#include <list>
2122
#include <unordered_map>
@@ -216,6 +217,16 @@ std::string GetParentDirectory(const std::string& path) {
216217
return path.substr(0, pos + 1);
217218
}
218219

220+
bool isRegularFile(const std::string& path) {
221+
struct stat info;
222+
223+
if (stat(path.c_str(), &info) != 0)
224+
return false;
225+
226+
// Check if it's a regular file
227+
return (info.st_mode & S_IFMT) == S_IFREG;
228+
}
229+
219230
} // namespace
220231

221232
Config::Config() : internal(new ConfigInternal()) {}
@@ -241,6 +252,8 @@ ConverterPtr Config::NewFromFile(const std::string& fileName,
241252
impl->paths.push_back(PACKAGE_DATA_DIRECTORY);
242253
}
243254
std::string prefixedFileName = impl->FindConfigFile(fileName);
255+
if (!isRegularFile(prefixedFileName))
256+
throw FileNotFound(prefixedFileName);
244257
std::ifstream ifs(UTF8Util::GetPlatformString(prefixedFileName));
245258
std::string content(std::istreambuf_iterator<char>(ifs),
246259
(std::istreambuf_iterator<char>()));

0 commit comments

Comments
 (0)