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

atob() clones the output more than necessary #15781

Conversation

kmiller68
Copy link
Contributor

@kmiller68 kmiller68 commented Jul 12, 2023

985e100

atob() clones the output more than necessary
https://bugs.webkit.org/show_bug.cgi?id=258789
rdar://111993365

Reviewed by David Kilzer.

Right now several users of `base64Decode` take the `Vector` returned and immediately convert it to a `String`. This
conversion requires a copy as the `Vector` returned by `base64Decode` uses `VectorBufferMalloc` but `String` requires
that the Vector backing store is malloced with `StringImplMalloc`. This patch simply adds a `base64DecodeToString` that
calls `base64DecodeInternal` with a new template parameter telling `base64DecodeInternal` to use `StringImplMalloc`. Thus,
avoiding the extra string copy.

* Source/JavaScriptCore/jsc.cpp:
(JSC_DEFINE_HOST_FUNCTION):
* Source/WTF/wtf/text/Base64.cpp:
(WTF::base64DecodeInternal):
(WTF::base64DecodeToString):
* Source/WTF/wtf/text/Base64.h:
* Source/WTF/wtf/text/WTFString.h:
* Source/WebCore/page/Base64Utilities.cpp:
(WebCore::Base64Utilities::atob):
* Source/WebCore/page/Page.cpp:
(WebCore::Page::userStyleSheetLocationChanged):

Canonical link: https://commits.webkit.org/266016@main

bd645f8

Misc iOS, tvOS & watchOS macOS Linux Windows
❌ πŸ§ͺ style βœ… πŸ›  ios βœ… πŸ›  mac βœ… πŸ›  wpe βœ… πŸ›  wincairo
βœ… πŸ§ͺ bindings βœ… πŸ›  ios-sim βœ… πŸ›  mac-AS-debug βœ… πŸ§ͺ wpe-wk2
βœ… πŸ§ͺ webkitperl βœ… πŸ§ͺ ios-wk2 βœ… πŸ§ͺ api-mac βœ… πŸ›  gtk
βœ… πŸ§ͺ ios-wk2-wpt βœ… πŸ§ͺ mac-wk1   πŸ§ͺ gtk-wk2
βœ… πŸ›  πŸ§ͺ jsc βœ… πŸ§ͺ api-ios βœ… πŸ§ͺ mac-wk2   πŸ§ͺ api-gtk
βœ… πŸ›  πŸ§ͺ jsc-arm64 βœ… πŸ›  tv βœ… πŸ§ͺ mac-AS-debug-wk2 βœ… πŸ›  jsc-armv7
βœ… πŸ›  tv-sim ❌ πŸ§ͺ jsc-armv7-tests
βœ… πŸ›  πŸ§ͺ merge βœ… πŸ›  watch βœ… πŸ›  jsc-mips
βœ… πŸ›  watch-sim βœ… πŸ§ͺ jsc-mips-tests

@kmiller68 kmiller68 requested review from cdumez and a team as code owners July 12, 2023 16:53
@kmiller68 kmiller68 self-assigned this Jul 12, 2023
@kmiller68 kmiller68 added the WebCore JavaScript Bugs in the JavaScript support that WebCore layers on top of JavaScriptCore. label Jul 12, 2023
@kmiller68
Copy link
Contributor Author

Methinks this patch works but maybe I misunderstood how base64 works. AFAICT, it should only use the following characters ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/= so it's already Latin1?

@cdumez cdumez requested review from weinig and darinadler July 12, 2023 17:03
@@ -267,4 +268,18 @@ std::optional<Vector<uint8_t>> base64Decode(StringView input, Base64DecodeMode m
return base64DecodeInternal(std::span(input.characters16(), length), mode);
}

std::optional<String> base64DecodeToString(StringView input, Base64DecodeMode mode)\
{
auto toString = [&] (auto optionalBuffer) -> std::optional<String> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this PR really shows that we need to introduce a NonNullableString in the codebase at some point or at least a Markable<String>.

In the meantime, I guess we should use the isNull() function instead of using an std::optional<String>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, you're right. I was thinking for some reason that String::adopt would return the nullString if given an empty vector but that's not the case. Will change the API.

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jul 12, 2023
@kmiller68 kmiller68 removed the merging-blocked Applied to prevent a change from being merged label Jul 12, 2023
@kmiller68 kmiller68 force-pushed the eng/atob-clones-the-output-more-than-necessary branch from e9e97e8 to 65fba56 Compare July 12, 2023 18:59
@kmiller68 kmiller68 force-pushed the eng/atob-clones-the-output-more-than-necessary branch from 65fba56 to f64e896 Compare July 12, 2023 19:12
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jul 12, 2023
@kmiller68 kmiller68 removed the merging-blocked Applied to prevent a change from being merged label Jul 12, 2023
@kmiller68 kmiller68 force-pushed the eng/atob-clones-the-output-more-than-necessary branch from f64e896 to bd645f8 Compare July 12, 2023 19:46
@ddkilzer
Copy link
Contributor

Methinks this patch works but maybe I misunderstood how base64 works. AFAICT, it should only use the following characters ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/= so it's already Latin1?

Base64 was created so that binary data could be sent as plain text (ASCII/Latin1).

https://en.wikipedia.org/wiki/Base64

Copy link
Contributor

@ddkilzer ddkilzer left a comment

Choose a reason for hiding this comment

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

r=me

@ddkilzer ddkilzer requested a review from mdubet July 12, 2023 20:41
@kmiller68 kmiller68 added the merge-queue Applied to send a pull request to merge-queue label Jul 12, 2023
https://bugs.webkit.org/show_bug.cgi?id=258789
rdar://111993365

Reviewed by David Kilzer.

Right now several users of `base64Decode` take the `Vector` returned and immediately convert it to a `String`. This
conversion requires a copy as the `Vector` returned by `base64Decode` uses `VectorBufferMalloc` but `String` requires
that the Vector backing store is malloced with `StringImplMalloc`. This patch simply adds a `base64DecodeToString` that
calls `base64DecodeInternal` with a new template parameter telling `base64DecodeInternal` to use `StringImplMalloc`. Thus,
avoiding the extra string copy.

* Source/JavaScriptCore/jsc.cpp:
(JSC_DEFINE_HOST_FUNCTION):
* Source/WTF/wtf/text/Base64.cpp:
(WTF::base64DecodeInternal):
(WTF::base64DecodeToString):
* Source/WTF/wtf/text/Base64.h:
* Source/WTF/wtf/text/WTFString.h:
* Source/WebCore/page/Base64Utilities.cpp:
(WebCore::Base64Utilities::atob):
* Source/WebCore/page/Page.cpp:
(WebCore::Page::userStyleSheetLocationChanged):

Canonical link: https://commits.webkit.org/266016@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/atob-clones-the-output-more-than-necessary branch from bd645f8 to 985e100 Compare July 13, 2023 00:17
@webkit-commit-queue
Copy link
Collaborator

Committed 266016@main (985e100): https://commits.webkit.org/266016@main

Reviewed commits have been landed. Closing PR #15781 and removing active labels.

@webkit-commit-queue webkit-commit-queue merged commit 985e100 into WebKit:main Jul 13, 2023
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WebCore JavaScript Bugs in the JavaScript support that WebCore layers on top of JavaScriptCore.
Projects
None yet
6 participants