- JSLock lock;
- CFIndex length = CFStringGetLength(string);
- UString::Rep* rep;
- if (!length)
- rep = UString("").rep()->ref();
- else {
- UniChar* buffer = static_cast<UniChar*>(fastMalloc(sizeof(UniChar) * length));
- CFStringGetCharacters(string, CFRangeMake(0, length), buffer);
- rep = UString(reinterpret_cast<UChar*>(buffer), length, false).rep()->ref();
+ JSC::initializeThreading();
+
+ // We cannot use CFIndex here since CFStringGetLength can return values larger than
+ // it can hold. (<rdar://problem/6806478>)
+ size_t length = CFStringGetLength(string);
+ if (length) {
+ OwnArrayPtr<UniChar> buffer = adoptArrayPtr(new UniChar[length]);
+ CFStringGetCharacters(string, CFRangeMake(0, length), buffer.get());
+ COMPILE_ASSERT(sizeof(UniChar) == sizeof(UChar), unichar_and_uchar_must_be_same_size);
+ return OpaqueJSString::create(reinterpret_cast<UChar*>(buffer.get()), length).leakRef();
+ } else {
+ return OpaqueJSString::create(0, 0).leakRef();