-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
LibWeb/CSS: Fix crash resolving calc
with only percentages
#3007
LibWeb/CSS: Fix crash resolving calc
with only percentages
#3007
Conversation
Hi @Jaycadox, thanks for looking into this! Could you also add a test to expose the original issue? |
if (result.type().has_value() && (result.type()->matches_length() || result.type()->matches_percentage())) | ||
return Length::make_px(CSSPixels { result.value() }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't correct. If a percentage gets returned (eg, 35%
) it'll return that number as a length (35px
).
A more correct thing to do would be to return the percentage applied to the percentage_basis
in that case.
The question is why this isn't getting converted to a length earlier on. NumericCalculationNode::resolve()
should be doing that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my analysis, I believe it would actually return the correct resolved pixel value (as percentage_basis
was being called on it in NumericCalculationNode::resolve
), the issue was purely that it was being reported as the wrong type, and I do think that my original patch, did fix the specific issue, but agree that it didn't fix the underlying problem.
233cdb3
to
b307b43
Compare
I had a look again at the issue, it seems that In my new patch, I visit all the variants of the percentage basis and set the As the original code (which I put in my original message) only handles the case of |
64566e9
to
9cf2b6c
Compare
static Optional<CSSNumericType> numeric_type_from_calculated_style_value(CalculatedStyleValue::CalculationResult::Value const& value) | ||
{ | ||
return value.visit( | ||
[](Number const&) -> Optional<CSSNumericType> { return {}; }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what CSSNumericType
should occur in the case of Number
, I assume there isn't a set one and it can change. But this code should probably be corrected if there is one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a duplicate of what's done in NumericCalculationNode::create()
, maybe that code can be pulled out and reused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion 👍
6ad0bfd
to
3798b2a
Compare
My bad, I believe I have now fixed the linting errors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is looking good. (And I finally understand what I messed up originally!) Just a couple of comments:
- The commit message should be more descriptive of what the commit does. It's good to mention that it fixes a bug somewhere in the message, but the title itself should be what the code change is. Here, that'd be something like "Recalculate calc() numeric type when resolving percentages" (though you can probably word that better).
- There are a few VERIFY()s sprinkled about, which seem like they might be from your debugging and testing? It's fine to leave them in but I just want to check they're supposed to be permanent. :^)
So really just the commit message needs adjusting, then this should be good to merge. (And then you won't have to keep waiting for someone to manually run CI. 😅 )
Oh also, once a comment is resolved, press the |
3798b2a
to
50f7857
Compare
Hello! One or more of the commit messages in this PR do not match the Ladybird code submission policy, please check the |
Previously, `percentage_of` would be called on the previous value, potentially changing its numeric type, yet this potential change was never reflected as the old numeric type was always used. Now, the numeric type will be re-calculated every time after the percentage is resolved. As well, VERIFY checks have been placed to uphold the requirements for the numeric types to match what the actual values are.
50f7857
to
8ba8166
Compare
Thanks for the feedback, I believe that I have implemented your suggestions. In this case, I left the VERIFY's on purpose, as I believe they express the intent, that is that the numeric type member should have to match the actual numeric type of the value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent! Thanks for that.
test(() => { | ||
println(`PASS! (Didn't crash)`); | ||
}); | ||
</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file doesn't end with a new line. I'm surprised the CI didn't pick it up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't worry about that for tests. Back in the day I think we manually removed the trailing newlines for layout tests just so they wouldn't add the extra text node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ok, I'm always fighting the pre-commit hook when I import WPT tests that don't have one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's odd, maybe your commit hook is outdated? I don't think I've had that issue but maybe I've been importing tests that don't lack the newline.
Fixes #3006 , (relates to) #648
From some analysis, it seems as if this issue only occurs with a only percentages in a
calc
.calc(10%)
crashes.calc(10% + 10%)
crashes.calc(10% + 0px)
doesn't crash.calc(10vw)
doesn't crash.The
resolve
function returns aLength
in the general case, but returns aPercentage
if only percentages are provided.As a sanity check, I looked at the code before it was changed (commit before change was here), and it only handled the case of percentages and lengths, as I do in this commit.
The old code being:
Not sure if this is perfect, very open to constructive criticism.