Skip to content

Commit

Permalink
Cherry-pick 259548.8@safari-7615-branch (5a0f792). https://bugs.webki…
Browse files Browse the repository at this point in the history
…t.org/show_bug.cgi?id=251158

    Check color opacity after lossy conversion when blending
    https://bugs.webkit.org/show_bug.cgi?id=251158
    rdar://104553839

    Reviewed by Dean Jackson.

    We check opacity to determine if we should forgo blending,
    however after performing a lossy conversion we can end
    up with alpha values that result in a division by zero.
    Add an additional check after conversion to prevent this
    case.

    * LayoutTests/fast/backgrounds/background-color-lch-crash-expected.txt: Added.
    * LayoutTests/fast/backgrounds/background-color-lch-crash.html: Added.
    * Source/WebCore/platform/graphics/ColorBlending.cpp:
    (WebCore::blendSourceOver):

    Canonical link: https://commits.webkit.org/259548.8@safari-7615-branch
  • Loading branch information
jace0x21 authored and aperezdc committed Apr 5, 2023
1 parent 1cf2bfc commit b7cc754
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
@@ -0,0 +1 @@
This test passes if it does not crash
14 changes: 14 additions & 0 deletions LayoutTests/fast/backgrounds/background-color-lch-crash.html
@@ -0,0 +1,14 @@
<style>
html, body {
background-color: lch(0 0 0 / 0.001);
}
</style>
<script>
internals.settings.setCaretBrowsingEnabled(true);
onload = () => {
document.execCommand('SelectAll');
};
if (window.testRunner)
testRunner.dumpAsText();
</script>
<span>This test passes if it does not crash</span>
6 changes: 6 additions & 0 deletions Source/WebCore/platform/graphics/ColorBlending.cpp
Expand Up @@ -42,6 +42,12 @@ Color blendSourceOver(const Color& backdrop, const Color& source)
auto [backdropR, backdropG, backdropB, backdropA] = backdrop.toColorTypeLossy<SRGBA<uint8_t>>().resolved();
auto [sourceR, sourceG, sourceB, sourceA] = source.toColorTypeLossy<SRGBA<uint8_t>>().resolved();

if (!backdropA || sourceA == 255)
return source;

if (!sourceA)
return backdrop;

int d = 0xFF * (backdropA + sourceA) - backdropA * sourceA;
int a = d / 0xFF;
int r = (backdropR * backdropA * (0xFF - sourceA) + 0xFF * sourceA * sourceR) / d;
Expand Down

0 comments on commit b7cc754

Please sign in to comment.