Skip to content

Commit 698815c

Browse files
committed
[JSC] Return written bytes in JSStringGetUTF8CString when it encounters invalid source
https://bugs.webkit.org/show_bug.cgi?id=278587 rdar://133747399 Reviewed by Yijia Huang. As API document describes, we should return written bytes even when we encountered the invalid source (like, unpaired surrogate) and stopped conversion in the middle of the string. * Source/JavaScriptCore/API/JSStringRef.cpp: (JSStringGetUTF8CString): * Source/JavaScriptCore/API/tests/testapi.c: (checkJSStringInvalid): (main): Canonical link: https://commits.webkit.org/282689@main
1 parent ea5e661 commit 698815c

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎Source/JavaScriptCore/API/JSStringRef.cpp‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ size_t JSStringGetUTF8CString(JSStringRef string, char* buffer, size_t bufferSiz
104104
result = WTF::Unicode::convert(string->span8(), target);
105105
else
106106
result = WTF::Unicode::convert(string->span16(), target);
107-
if (result.code == WTF::Unicode::ConversionResultCode::SourceInvalid)
108-
return 0;
109-
110107
buffer[result.buffer.size()] = '\0';
111108
return result.buffer.size() + 1;
112109
}

‎Source/JavaScriptCore/API/tests/testapi.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,31 @@ static void checkJSStringOOB(void)
13031303
printf("PASS: checkJSStringOOB\n");
13041304
}
13051305

1306+
static void checkJSStringInvalid(void)
1307+
{
1308+
printf("Test: checkJSStringInvalid\n");
1309+
JSChar* source = (JSChar*)malloc(sizeof(JSChar) * 4);
1310+
source[0] = 'a';
1311+
source[1] = 'b';
1312+
source[2] = 'c';
1313+
source[3] = 0xD800;
1314+
JSStringRef string = JSStringCreateWithCharacters(source, 4);
1315+
1316+
char* out = (char*)malloc(sizeof(char) * 32);
1317+
memset(out, 1, sizeof(char) * 32);
1318+
size_t bytesWritten = JSStringGetUTF8CString(string, out, sizeof(char) * 32);
1319+
1320+
assertTrue(bytesWritten == 4, "we report 4 bytes written precisely");
1321+
assertTrue(out[0] == 'a', "a");
1322+
assertTrue(out[1] == 'b', "b");
1323+
assertTrue(out[2] == 'c', "c");
1324+
assertTrue(out[3] == '\0', "string terminated");
1325+
1326+
JSStringRelease(string);
1327+
free(out);
1328+
free(source);
1329+
}
1330+
13061331
static const unsigned numWeakRefs = 10000;
13071332

13081333
static void markingConstraint(JSMarkerRef marker, void *userData)
@@ -1888,6 +1913,7 @@ int main(int argc, char* argv[])
18881913
assertEqualsAsUTF8String(jsOneString, "1");
18891914

18901915
checkJSStringOOB();
1916+
checkJSStringInvalid();
18911917

18921918
checkConstnessInJSObjectNames();
18931919

0 commit comments

Comments
 (0)