From 7ca10e7f0c512b814d0feea9578678e8ec92fc4e Mon Sep 17 00:00:00 2001 From: smilediver Date: Sat, 25 May 2024 03:07:31 +0300 Subject: [PATCH] Fix passing non null terminated strings for null terminated params (#1924) * Fix passing non null terminated strings for null terminated params * Change stringWithUTF8String with initWithBytes --- core/media/AvfMediaEngine.mm | 4 ++-- core/platform/apple/FileUtils-apple.mm | 12 ++++++------ core/platform/ios/Device-ios.mm | 2 +- core/platform/mac/Device-mac.mm | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/media/AvfMediaEngine.mm b/core/media/AvfMediaEngine.mm index 8525360cf2c8..b5dda9744c18 100644 --- a/core/media/AvfMediaEngine.mm +++ b/core/media/AvfMediaEngine.mm @@ -194,14 +194,14 @@ - (void)observeValueForKeyPath:(NSString*)keyPath // Media Framework doesn't percent encode the URL, so the path portion is just a native file path. // Extract it and then use it create a proper URL. Path = sourceUri.substr(7); - NSString* nsPath = [NSString stringWithUTF8String:Path.data()]; + NSString* nsPath = [[NSString alloc] initWithBytes:Path.data() length:Path.size() encoding:NSUTF8StringEncoding]; nsMediaUrl = [NSURL fileURLWithPath:nsPath isDirectory:NO]; } else { // Assume that this has been percent encoded for now - when we support HTTP Live Streaming we will need to check // for that. - NSString* nsUri = [NSString stringWithUTF8String:sourceUri.data()]; + NSString* nsUri = [[NSString alloc] initWithBytes:sourceUri.data() length:sourceUri.size() encoding:NSUTF8StringEncoding]; nsMediaUrl = [NSURL URLWithString:nsUri]; } diff --git a/core/platform/apple/FileUtils-apple.mm b/core/platform/apple/FileUtils-apple.mm index c91e2e997752..cd4662975a9b 100644 --- a/core/platform/apple/FileUtils-apple.mm +++ b/core/platform/apple/FileUtils-apple.mm @@ -159,7 +159,7 @@ of this software and associated documentation files (the "Software"), to deal { // Search path is an absolute path. BOOL isDir = NO; - if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:filePath.data()] isDirectory:&isDir] && !isDir) + if ([s_fileManager fileExistsAtPath:[[NSString alloc] initWithBytes:filePath.data() length:filePath.size() encoding:NSUTF8StringEncoding] isDirectory:&isDir] && !isDir) { ret = true; } @@ -207,7 +207,7 @@ static int unlink_cb(const char* fpath, const struct stat* sb, int typeflag, str if (path[0] == '/') { BOOL isDir = NO; - if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:path.data()] isDirectory:&isDir]) + if ([s_fileManager fileExistsAtPath:[[NSString alloc] initWithBytes:path.data() length:path.size() encoding:NSUTF8StringEncoding] isDirectory:&isDir]) { return appendTrailingSlashToDir(isDir ? path : ""); } @@ -232,9 +232,9 @@ static int unlink_cb(const char* fpath, const struct stat* sb, int typeflag, str // Build full path for the file if (directory[0] != '/') { - NSString* path = [pimpl_->getBundle() pathForResource:[NSString stringWithUTF8String:filename.data()] + NSString* path = [pimpl_->getBundle() pathForResource:[[NSString alloc] initWithBytes:filename.data() length:filename.size() encoding:NSUTF8StringEncoding] ofType:nil - inDirectory:[NSString stringWithUTF8String:directory.data()]]; + inDirectory:[[NSString alloc] initWithBytes:directory.data() length:directory.size() encoding:NSUTF8StringEncoding]]; if (path != nil) { fullPath = [path UTF8String]; @@ -265,14 +265,14 @@ static int unlink_cb(const char* fpath, const struct stat* sb, int typeflag, str NSError* error; - bool result = [s_fileManager createDirectoryAtPath:[NSString stringWithUTF8String:path.data()] + bool result = [s_fileManager createDirectoryAtPath:[[NSString alloc] initWithBytes:path.data() length:path.size() encoding:NSUTF8StringEncoding] withIntermediateDirectories:YES attributes:nil error:&error]; if (!result && error != nil) { - AXLOGERROR("Fail to create directory \"%s\": %s", path.data(), [error.localizedDescription UTF8String]); + AXLOGERROR("Fail to create directory \"%s\": %s", std::string(path).c_str(), [error.localizedDescription UTF8String]); } return result; diff --git a/core/platform/ios/Device-ios.mm b/core/platform/ios/Device-ios.mm index 3823d5ad9337..5d8951467e1c 100644 --- a/core/platform/ios/Device-ios.mm +++ b/core/platform/ios/Device-ios.mm @@ -463,7 +463,7 @@ static bool _initWithString(std::string_view text, AX_BREAK_IF(!font); - NSString* str = [NSString stringWithUTF8String:text.data()]; + NSString* str = [[NSString alloc] initWithBytes:text.data() length:text.size() encoding:NSUTF8StringEncoding]; AX_BREAK_IF(!str); CGSize dimensions; diff --git a/core/platform/mac/Device-mac.mm b/core/platform/mac/Device-mac.mm index 48c85bdfaf59..4acab133f768 100644 --- a/core/platform/mac/Device-mac.mm +++ b/core/platform/mac/Device-mac.mm @@ -295,7 +295,7 @@ static bool _initWithString(std::string_view text, do { - NSString* string = [NSString stringWithUTF8String:text.data()]; + NSString* string = [[NSString alloc] initWithBytes:text.data() length:text.size() encoding:NSUTF8StringEncoding]; AX_BREAK_IF(!string); id font = _createSystemFont(fontName, size);