Commit dc18042e authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #606 from masamitsu-murase/fix_memory_leak_in_string_to_i

Fix memory leak in String#to_i and String#to_f.
parents 453980d7 a7182c20
...@@ -2544,12 +2544,8 @@ mrb_str_to_inum(mrb_state *mrb, mrb_value str, int base, int badcheck) ...@@ -2544,12 +2544,8 @@ mrb_str_to_inum(mrb_state *mrb, mrb_value str, int base, int badcheck)
if (s) { if (s) {
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
if (s[len]) { /* no sentinel somehow */ if (s[len]) { /* no sentinel somehow */
char *p = (char *)mrb_malloc(mrb, len+1); struct RString *temp_str = str_new(mrb, s, len);
s = temp_str->ptr;
//MEMCPY(p, s, char, len);
memcpy(p, s, len);
p[len] = '\0';
s = p;
} }
} }
return mrb_cstr_to_inum(mrb, s, base, badcheck); return mrb_cstr_to_inum(mrb, s, base, badcheck);
...@@ -2681,11 +2677,8 @@ mrb_str_to_dbl(mrb_state *mrb, mrb_value str, int badcheck) ...@@ -2681,11 +2677,8 @@ mrb_str_to_dbl(mrb_state *mrb, mrb_value str, int badcheck)
mrb_raise(mrb, E_ARGUMENT_ERROR, "string for Float contains null byte"); mrb_raise(mrb, E_ARGUMENT_ERROR, "string for Float contains null byte");
} }
if (s[len]) { /* no sentinel somehow */ if (s[len]) { /* no sentinel somehow */
char *p = (char *)mrb_malloc(mrb, len+1); struct RString *temp_str = str_new(mrb, s, len);
s = temp_str->ptr;
memcpy(p, s, len);
p[len] = '\0';
s = p;
} }
} }
return mrb_cstr_to_dbl(mrb, s, badcheck); return mrb_cstr_to_dbl(mrb, s, badcheck);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment