Get rid of warnings/exceptions at cleanup

After the encoding index instance variable is removed when all
instance variables are removed in `obj_free`, then `rb_str_free`
causes uninitialized instance variable warning and nil-to-integer
conversion exception.  Both cases result in object allocation
during GC, and crashes.
This commit is contained in:
Nobuyoshi Nakada 2020-02-13 09:34:49 +09:00
parent b1b155ff03
commit fce667ed08
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
3 changed files with 14 additions and 0 deletions

View file

@ -779,8 +779,18 @@ enc_get_index_str(VALUE str)
if (i == ENCODING_INLINE_MAX) {
VALUE iv;
#if 0
iv = rb_ivar_get(str, rb_id_encoding());
i = NUM2INT(iv);
#else
/*
* Tentatively, assume ASCII-8BIT, if encoding index instance
* variable is not found. This can happen when freeing after
* all instance variables are removed in `obj_free`.
*/
iv = rb_attr_get(str, rb_id_encoding());
i = NIL_P(iv) ? ENCINDEX_ASCII : NUM2INT(iv);
#endif
}
return i;
}