Skip to content

fix(iOS): Add null check for fontName in RCTGetFontWeight to prevent crash#55414

Open
25harsh wants to merge 2 commits into
facebook:mainfrom
25harsh:fix/ios-null-fontname-crash
Open

fix(iOS): Add null check for fontName in RCTGetFontWeight to prevent crash#55414
25harsh wants to merge 2 commits into
facebook:mainfrom
25harsh:fix/ios-null-fontname-crash

Conversation

@25harsh
Copy link
Copy Markdown
Contributor

@25harsh 25harsh commented Feb 5, 2026

Summary:

Adds defensive null check for fontName in RCTGetFontWeight before passing it to CFStringFind(), preventing EXC_BAD_ACCESS crash.

Problem

RCTGetFontWeight passes font.fontName directly to CFStringFind() without checking if the result is nil. Unlike Objective-C methods, Core Foundation functions like CFStringFind do not gracefully handle NULL pointers and will
crash with EXC_BAD_ACCESS.

While standard React Native apps typically register fonts at compile time. There are legitimate scenarios where fonts may not be immediately available. eg.
Brownfield applications - runtime font registration using CTFontManagerRegisterGraphicsFont, this causes RN to crash

Changelog:

[IOS][FIXED] - Added null check for fontName in RCTGetFontWeight to prevent crash

Test Plan:

When calling RCTGetFontWeight with nil UIFont (either empty string or unregistered font), no crash observed.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 5, 2026
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Feb 5, 2026
@25harsh
Copy link
Copy Markdown
Contributor Author

25harsh commented Feb 19, 2026

@cipolleschi can you please check this

@isaacrowntree
Copy link
Copy Markdown

We're hitting this exact crash in production on React Native 0.83.2 with Fabric enabled (iOS).

Crash stack from Crashlytics:

Thread 11 Crashed:
0  CoreFoundation    CF_IS_OBJC + 12
1  CoreFoundation    CFStringGetLength + 232
2  CoreFoundation    CFStringFind + 60
3  React             RCTGetFontWeight + 212
4  React             RCTFontWithFontProperties + 3024
5  React             RCTNSTextAttributesFromTextAttributes + 316
6  React             RCTNSAttributedStringFromAttributedString + 364
7  React             -[RCTTextLayoutManager measureAttributedString:...] + 76

Happens during concurrent Yoga layout measurement on the Hermes JS thread when a UIFont fails to resolve. We've applied this same nil guard locally via patch-package as a workaround. Would love to see this merged — it's a one-liner that prevents a fatal SIGSEGV.

@isaacrowntree
Copy link
Copy Markdown

We're hitting this same crash in production on RN 0.83.2 with Fabric enabled (posted a comment here previously with our Crashlytics stack).

One thing worth noting: your PR guards fontName but not the font parameter itself. We've applied both guards locally via patch-package and would suggest adding a nil check for font at the top of the function as well:

RCTFontWeight RCTGetFontWeight(UIFont *font)
{
  if (!font) {
    return UIFontWeightRegular;
  }
  // ... existing code ...
  NSString *fontName = font.fontName;
  if (fontName) {
    // ... suffix matching loop ...
  }

  auto traits = (__bridge_transfer NSDictionary *)CTFontCopyTraits((CTFontRef)font);
  return (RCTFontWeight)[traits[UIFontWeightTrait] doubleValue];
}

If font is nil, font.fontName returns nil (ObjC message to nil), which then hits the same CFStringFind(NULL, ...) SIGSEGV. The !font guard is defense-in-depth — while the fontName guard catches the case we're seeing (non-nil UIFont with a nil fontName from a partially-resolved descriptor during concurrent Fabric layout), guarding font too makes the function robust against both scenarios.

@25harsh 25harsh force-pushed the fix/ios-null-fontname-crash branch from c1bd1fb to 76e72f6 Compare March 23, 2026 09:05
@25harsh
Copy link
Copy Markdown
Contributor Author

25harsh commented Mar 23, 2026

hey @isaacrowntree thanks for the suggestion. I've gone ahead and added the !font guard at the top as well, so both scenarios are now covered.

@cipolleschi would you be able to take a look when you get a chance? Would be great to get this landed given the production crashes folks are seeing.

@25harsh 25harsh force-pushed the fix/ios-null-fontname-crash branch from b095e09 to 2198915 Compare May 14, 2026 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants