From 0701236dacd1c108d79f9cc90665fb4069d43c55 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 1 Dec 2015 16:11:47 +0900 Subject: mrb_str_to_inum(): should treat null byte in strings properly; fix #3040 --- src/string.c | 69 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 34 deletions(-) (limited to 'src/string.c') diff --git a/src/string.c b/src/string.c index 6ea139c90..329010e59 100644 --- a/src/string.c +++ b/src/string.c @@ -2030,11 +2030,12 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str) } MRB_API mrb_value -mrb_cstr_to_inum(mrb_state *mrb, const char *str, int base, int badcheck) +mrb_str_len_to_inum(mrb_state *mrb, const char *str, size_t len, int base, int badcheck) { - const char *p; + const char *p = str; + const char *pend = str + len; char sign = 1; - int c, uscore; + int c; uint64_t n = 0; mrb_int val; @@ -2048,7 +2049,8 @@ mrb_cstr_to_inum(mrb_state *mrb, const char *str, int base, int badcheck) if (badcheck) goto bad; return mrb_fixnum_value(0); } - while (ISSPACE(*str)) str++; + while (str= 2) + if (*str == '_') { + if (badcheck) goto bad; break; + } } - else - uscore = 0; } if (!(c = *str) || ISSPACE(c)) --str; } c = *str; + if (badcheck && c == '\0') { + goto nullbyte; + } c = conv_digit(c); if (c < 0 || c >= base) { if (badcheck) goto bad; return mrb_fixnum_value(0); } - uscore = 0; - for (p=str;*p;p++) { + for (p=str;p= base) { break; @@ -2161,17 +2162,26 @@ mrb_cstr_to_inum(mrb_state *mrb, const char *str, int base, int badcheck) val = n; if (badcheck) { if (p == str) goto bad; /* no number */ - while (*p && ISSPACE(*p)) p++; - if (*p) goto bad; /* trailing garbage */ + while (p