Skip to content

Commit 5a25112

Browse files
Address review: RAII heap buffer and constexpr assets prefix length in RCTAssetCatalogNameForURL
1 parent e85eb1a commit 5a25112

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

packages/react-native/React/Base/RCTUtils.mm

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import <objc/runtime.h>
1414
#import <zlib.h>
1515
#import <atomic>
16+
#import <vector>
1617

1718
#import <UIKit/UIKit.h>
1819

@@ -969,7 +970,7 @@ static BOOL RCTUseAssetCatalog(void)
969970
{
970971
// The "assets/" prefix the packager uses for all image assets. The CLI strips
971972
// it from the identifiers it names the imagesets with.
972-
const NSUInteger assetsPrefixLength = 7;
973+
constexpr NSUInteger assetsPrefixLength = sizeof("assets/") - 1;
973974

974975
NSString *path = RCTBundlePathForURL(URL);
975976
// Packager assets always live under "assets/". Anything else (sub-bundles,
@@ -991,7 +992,12 @@ static BOOL RCTUseAssetCatalog(void)
991992

992993
const NSUInteger length = path.length;
993994
unichar stackBuffer[256];
994-
unichar *chars = length <= 256 ? stackBuffer : (unichar *)malloc(length * sizeof(unichar));
995+
std::vector<unichar> heapBuffer;
996+
unichar *chars = stackBuffer;
997+
if (length > 256) {
998+
heapBuffer.resize(length);
999+
chars = heapBuffer.data();
1000+
}
9951001
[path getCharacters:chars range:NSMakeRange(0, length)];
9961002

9971003
// Strip the file extension (guaranteed present by RCTIsImageAssetsPath) and
@@ -1041,9 +1047,6 @@ static BOOL RCTUseAssetCatalog(void)
10411047
}
10421048

10431049
NSString *name = [NSString stringWithCharacters:chars length:resultLength];
1044-
if (chars != stackBuffer) {
1045-
free(chars);
1046-
}
10471050
return name;
10481051
}
10491052

0 commit comments

Comments
 (0)