From 79f1705ed1fe73b328c31ce2a44cf4c78ace9d84 Mon Sep 17 00:00:00 2001 From: zhangbin Date: Thu, 23 Feb 2017 16:53:14 +0800 Subject: [PATCH] Solve the error when parameter of FileUtils:: fullPathForFilename contains ".." --- .../cocos/platform/apple/CCFileUtils-apple.mm | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/native/cocos/platform/apple/CCFileUtils-apple.mm b/native/cocos/platform/apple/CCFileUtils-apple.mm index 39f5c2e859..73d5d3bd73 100644 --- a/native/cocos/platform/apple/CCFileUtils-apple.mm +++ b/native/cocos/platform/apple/CCFileUtils-apple.mm @@ -314,9 +314,24 @@ std::string FileUtilsApple::getFullPathForDirectoryAndFilename(const std::string { if (directory[0] != '/') { + NSString* dirStr = [NSString stringWithUTF8String:directory.c_str()]; + // The following logic is used for remove the "../" in the directory + // Because method "pathForResource" will return nil if the directory contains "../". + auto theIdx = directory.find(".."); + if (theIdx != std::string::npos && theIdx > 0) { + NSArray* pathComps = [dirStr pathComponents]; + NSUInteger idx = [pathComps indexOfObject:@".."]; + while (idx != NSNotFound && idx > 0) { // if found ".." & it's not at the beginning of the string + [pathComps removeObjectAtIndex: idx]; // remove the item ".." + [pathComps removeObjectAtIndex: idx - 1]; // remove the item before ".." + idx = [pathComps indexOfObject:@".."]; // find ".." again + } + dirStr = [NSString pathWithComponents:pathComps]; + } + NSString* fullpath = [pimpl_->getBundle() pathForResource:[NSString stringWithUTF8String:filename.c_str()] ofType:nil - inDirectory:[NSString stringWithUTF8String:directory.c_str()]]; + inDirectory:dirStr]; if (fullpath != nil) { return [fullpath UTF8String]; }