Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make second argument of UTF8ArrayToString optional. NFC #22696

Merged
merged 1 commit into from
Oct 9, 2024

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Oct 8, 2024

This is mostly a wash from a code size POV but makes the call sites more readable I think.

@sbc100 sbc100 requested a review from kripken October 8, 2024 21:09
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability seems worth it, I agree, but see my question below.

* @param {number=} maxBytesToRead
* @return {string}
*/`,
#if TEXTDECODER
$UTF8ArrayToString__deps: ['$UTF8Decoder'],
#endif
$UTF8ArrayToString: (heapOrArray, idx, maxBytesToRead) => {
$UTF8ArrayToString: (heapOrArray, idx = 0, maxBytesToRead = NaN) => {
#if CAN_ADDRESS_2GB
idx >>>= 0;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on line 41 should replace "undefined" with "NaN"

@@ -103,7 +103,7 @@ if (!isDataURI(wasmSourceMapFile)) {

function getSourceMap() {
var buf = readBinary(wasmSourceMapFile);
return JSON.parse(UTF8ArrayToString(buf, 0, buf.length));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change NFC? Before we would read buf.length bytes at most, but after we read without limit until the end of a UTF8 string. But is wasmSourceMapFile guaranteed to be encoded that way (including a null terminator)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The third argument here is maxBytesToRead which means will read at most buf.length. However UTF8ArrayToString will always stop when it reads a null byte I believe

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But is there a null byte in this input? That is what concerns me. It is reading a source map file, which I don't think is encoded that way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I assumed UTF8ArrayToString would not read read past the end of the input array! I don't see that check though. That's strange.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait I think its fine because if you read of the end of a array or buffer in JS you get undefined back, so every buffer is effectively null terminated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. That looks safe then.

This is mostly a wash from a code size POV but makes the call sites more
readable I think.
@sbc100 sbc100 enabled auto-merge (squash) October 8, 2024 23:54
@sbc100 sbc100 merged commit 94c3f85 into emscripten-core:main Oct 9, 2024
28 checks passed
@sbc100 sbc100 deleted the UTF8ArrayToString branch October 9, 2024 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants