From d6ec400148f58061118917fb5ed3977425244f0d Mon Sep 17 00:00:00 2001 From: tadf Date: Thu, 22 Nov 2012 11:32:52 +0000 Subject: * complex.c (string_to_c_strict, string_to_c): check NUL. * rational.c (string_to_r_strict, string_to_r): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37804 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- complex.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'complex.c') diff --git a/complex.c b/complex.c index 1d61fda950..386f2ddd18 100644 --- a/complex.c +++ b/complex.c @@ -1776,16 +1776,25 @@ parse_comp(const char *s, int strict, static VALUE string_to_c_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_comp(s, 1, &num)) { VALUE ins = f_inspect(self); rb_raise(rb_eArgError, "invalid value for convert(): %s", @@ -1819,13 +1828,22 @@ string_to_c_strict(VALUE self) static VALUE string_to_c(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_comp(s, 0, &num); return num; -- cgit v1.2.3