From 571d21fd4a2e877f49b4ff918832bda9a5e8f91c Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Wed, 24 Aug 2022 10:31:17 -0700 Subject: Make String#rstrip{,!} raise Encoding::CompatibilityError for broken coderange It's questionable whether we want to allow rstrip to work for strings where the broken coderange occurs before the trailing whitespace and not after, but this approach is probably simpler, and I don't think users should expect string operations like rstrip to work on broken strings. In some cases, this changes rstrip to raise Encoding::CompatibilityError instead of ArgumentError. However, as the problem is related to an encoding issue in the receiver, and due not due to an issue with an argument, I think Encoding::CompatibilityError is the more appropriate error. Fixes [Bug #18931] --- string.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'string.c') diff --git a/string.c b/string.c index 0ae36e5c72..20d92896c0 100644 --- a/string.c +++ b/string.c @@ -9720,6 +9720,9 @@ rstrip_offset(VALUE str, const char *s, const char *e, rb_encoding *enc) const char *t; rb_str_check_dummy_enc(enc); + if (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN) { + rb_raise(rb_eEncCompatError, "invalid byte sequence in %s", rb_enc_name(enc)); + } if (!s || s >= e) return 0; t = e; -- cgit v1.2.1