summaryrefslogtreecommitdiff
path: root/rational.c
diff options
context:
space:
mode:
Diffstat (limited to 'rational.c')
-rw-r--r--rational.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/rational.c b/rational.c
index b72f851..855b187 100644
--- a/rational.c
+++ b/rational.c
@@ -2143,16 +2143,25 @@ parse_rat(const char *s, int strict,
static VALUE
string_to_r_strict(VALUE self)
{
- const char *s;
+ char *s;
VALUE num;
rb_must_asciicompat(self);
s = RSTRING_PTR(self);
- if (memchr(s, 0, RSTRING_LEN(self)))
+ if (!s || memchr(s, '\0', RSTRING_LEN(self)))
rb_raise(rb_eArgError, "string contains null byte");
+ if (s && s[RSTRING_LEN(self)]) {
+ rb_str_modify(self);
+ s = RSTRING_PTR(self);
+ s[RSTRING_LEN(self)] = '\0';
+ }
+
+ if (!s)
+ s = (char *)"";
+
if (!parse_rat(s, 1, &num)) {
VALUE ins = f_inspect(self);
rb_raise(rb_eArgError, "invalid value for convert(): %s",
@@ -2188,13 +2197,22 @@ string_to_r_strict(VALUE self)
static VALUE
string_to_r(VALUE self)
{
- const char *s;
+ char *s;
VALUE num;
rb_must_asciicompat(self);
s = RSTRING_PTR(self);
+ if (s && s[RSTRING_LEN(self)]) {
+ rb_str_modify(self);
+ s = RSTRING_PTR(self);
+ s[RSTRING_LEN(self)] = '\0';
+ }
+
+ if (!s)
+ s = (char *)"";
+
(void)parse_rat(s, 0, &num);
if (RB_TYPE_P(num, T_FLOAT))