Preserve encoding in exception message of Float

This commit is contained in:
Nobuyoshi Nakada 2024-09-07 16:34:28 +09:00
parent c1862cbb89
commit f97332a3a3
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2024-09-07 08:29:29 +00:00
2 changed files with 10 additions and 4 deletions

View File

@ -3400,7 +3400,7 @@ rb_f_integer(rb_execution_context_t *ec, VALUE obj, VALUE arg, VALUE base, VALUE
} }
static double static double
rb_cstr_to_dbl_raise(const char *p, int badcheck, int raise, int *error) rb_cstr_to_dbl_raise(const char *p, rb_encoding *enc, int badcheck, int raise, int *error)
{ {
const char *q; const char *q;
char *end; char *end;
@ -3411,6 +3411,7 @@ rb_cstr_to_dbl_raise(const char *p, int badcheck, int raise, int *error)
#define OutOfRange() ((end - p > max_width) ? \ #define OutOfRange() ((end - p > max_width) ? \
(w = max_width, ellipsis = "...") : \ (w = max_width, ellipsis = "...") : \
(w = (int)(end - p), ellipsis = "")) (w = (int)(end - p), ellipsis = ""))
/* p...end has been parsed with strtod, should be ASCII-only */
if (!p) return 0.0; if (!p) return 0.0;
q = p; q = p;
@ -3506,7 +3507,8 @@ rb_cstr_to_dbl_raise(const char *p, int badcheck, int raise, int *error)
bad: bad:
if (raise) { if (raise) {
rb_invalid_str(q, "Float()"); VALUE s = rb_enc_str_new_cstr(q, enc);
rb_raise(rb_eArgError, "invalid value for Float(): %+"PRIsVALUE, s);
UNREACHABLE_RETURN(nan("")); UNREACHABLE_RETURN(nan(""));
} }
else { else {
@ -3518,7 +3520,7 @@ rb_cstr_to_dbl_raise(const char *p, int badcheck, int raise, int *error)
double double
rb_cstr_to_dbl(const char *p, int badcheck) rb_cstr_to_dbl(const char *p, int badcheck)
{ {
return rb_cstr_to_dbl_raise(p, badcheck, TRUE, NULL); return rb_cstr_to_dbl_raise(p, NULL, badcheck, TRUE, NULL);
} }
static double static double
@ -3549,9 +3551,11 @@ rb_str_to_dbl_raise(VALUE str, int badcheck, int raise, int *error)
s = p; s = p;
} }
} }
ret = rb_cstr_to_dbl_raise(s, badcheck, raise, error); ret = rb_cstr_to_dbl_raise(s, rb_enc_get(str), badcheck, raise, error);
if (v) if (v)
ALLOCV_END(v); ALLOCV_END(v);
else
RB_GC_GUARD(str);
return ret; return ret;
} }

View File

@ -856,6 +856,8 @@ class TestFloat < Test::Unit::TestCase
assert_raise(Encoding::CompatibilityError) {Float("0".encode("utf-32be"))} assert_raise(Encoding::CompatibilityError) {Float("0".encode("utf-32be"))}
assert_raise(Encoding::CompatibilityError) {Float("0".encode("utf-32le"))} assert_raise(Encoding::CompatibilityError) {Float("0".encode("utf-32le"))}
assert_raise(Encoding::CompatibilityError) {Float("0".encode("iso-2022-jp"))} assert_raise(Encoding::CompatibilityError) {Float("0".encode("iso-2022-jp"))}
assert_raise_with_message(ArgumentError, /\u{1f4a1}/) {Float("\u{1f4a1}")}
end end
def test_invalid_str def test_invalid_str