Skip to content

Conversation

peterzhu2118
Copy link
Member

We sometimes pass in a fake string to sym_check_asciionly. This can crash if sym_check_asciionly raises because it creates a CFP with the fake string as the receiver which will crash if GC tries to mark the CFP.

For example, the following script crashes:

GC.stress = true
Object.const_defined?("\xC3")

@peterzhu2118 peterzhu2118 changed the title [Bug #20244] Fix crash when checking symbol encoding [Bug #20245] Fix crash when checking symbol encoding Feb 7, 2024
[Bug #20245]

We sometimes pass in a fake string to sym_check_asciionly. This can crash
if sym_check_asciionly raises because it creates a CFP with the fake
string as the receiver which will crash if GC tries to mark the CFP.

For example, the following script crashes:

    GC.stress = true
    Object.const_defined?("\xC3")
{
if (!rb_enc_asciicompat(rb_enc_get(str))) return FALSE;
switch (rb_enc_str_coderange(str)) {
case ENC_CODERANGE_BROKEN:
if (fake_str) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of passing a boolean, couldn't you check for the STR_FAKESTR user flag?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess we could, but we'll have to make STR_FAKESTR available outside of string.c. I'm not sure if I want to encourage more usage of the STR_FAKESTR flag.

Copy link
Member

Choose a reason for hiding this comment

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

Agree with @peterzhu2118.
And the explicit flag makes inlining this function easier when it is false.

{
if (!rb_enc_asciicompat(rb_enc_get(str))) return FALSE;
switch (rb_enc_str_coderange(str)) {
case ENC_CODERANGE_BROKEN:
if (fake_str) {
Copy link
Member

Choose a reason for hiding this comment

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

Agree with @peterzhu2118.
And the explicit flag makes inlining this function easier when it is false.

@peterzhu2118 peterzhu2118 merged commit 01fd262 into ruby:master Feb 8, 2024
@peterzhu2118 peterzhu2118 deleted the invalid-symbol-crash branch February 8, 2024 15:12
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.

3 participants