mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
Merge pull request #542 from natural-law/issue5276
Solve the error when parameter of FileUtils:: fullPathForFilename contains ".."
This commit is contained in:
@@ -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<NSString *>* 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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user