An attempt at resolving crash in RCTStringFromNSString

Summary:
Changelog: [Internal]

# Why we crash?

`cStringUsingEncoding` returns `char *`, not `std::string`. Compiler uses implicit conversion to construct and copy `char *` into  `std::string`. Maybe optimiser does something unexpected there? Maybe something weird happens there? I think it is worth trying to be more explicit about it and construct std::string there explicitly. Also if you do a google search, this seems to be a go to strategy when converting `NSString` to `std::string`.

This is all just an assumption, I can't repro the crash

# Why get rid of 2nd argument in RCTStringFromNSString

2nd argument is `NSStringEncoding`. It isn't being used, we always use default value.

Also, if you pass in `NSUTF16StringEncoding` or `NSUTF32StringEncoding`, you get undefined behaviour.
Check https://developer.apple.com/documentation/foundation/nsstring/1408489-cstringusingencoding?language=objc# section "Special Considerations"

Reviewed By: shergin

Differential Revision: D22089694

fbshipit-source-id: d449b383c61983c3822bc589c0a01fa97c0b6e64
This commit is contained in:
Samuel Susla 2020-06-17 14:17:54 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 8e1b812779
Коммит 93f9d8dd6b
1 изменённых файлов: 2 добавлений и 2 удалений

Просмотреть файл

@ -30,9 +30,9 @@ inline NSString *_Nullable RCTNSStringFromStringNilIfEmpty(
return string.empty() ? nil : RCTNSStringFromString(string, encoding);
}
inline std::string RCTStringFromNSString(NSString *string, const NSStringEncoding &encoding = NSUTF8StringEncoding)
inline std::string RCTStringFromNSString(NSString *string)
{
return [string cStringUsingEncoding:encoding];
return std::string([string UTF8String]);
}
inline UIColor *_Nullable RCTUIColorFromSharedColor(const facebook::react::SharedColor &sharedColor)