summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2024-09-07 16:34:28 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2024-09-07 16:34:28 +0900
commitf97332a3a3647b79a19a7ad81ab8e58bc8608399 (patch)
tree85f62c6953a80169f3acb3ae7044a1b7b74811d2 /object.c
parentc1862cbb89a6bf42dcd07d92fe4f4bfeebca5775 (diff)
Preserve encoding in exception message of `Float`
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/11564
Diffstat (limited to 'object.c')
-rw-r--r--object.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/object.c b/object.c
index bbc607ea98..ae6ec6ea54 100644
--- a/object.c
+++ b/object.c
@@ -3400,7 +3400,7 @@ rb_f_integer(rb_execution_context_t *ec, VALUE obj, VALUE arg, VALUE base, VALUE
}
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;
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) ? \
(w = max_width, ellipsis = "...") : \
(w = (int)(end - p), ellipsis = ""))
+ /* p...end has been parsed with strtod, should be ASCII-only */
if (!p) return 0.0;
q = p;
@@ -3506,7 +3507,8 @@ rb_cstr_to_dbl_raise(const char *p, int badcheck, int raise, int *error)
bad:
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(""));
}
else {
@@ -3518,7 +3520,7 @@ rb_cstr_to_dbl_raise(const char *p, int badcheck, int raise, int *error)
double
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
@@ -3549,9 +3551,11 @@ rb_str_to_dbl_raise(VALUE str, int badcheck, int raise, int *error)
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)
ALLOCV_END(v);
+ else
+ RB_GC_GUARD(str);
return ret;
}