From 615e7e44e3bd277bd0f73410dc253aa6d4e2bd34 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 20 Jun 2017 09:39:33 +0900 Subject: Avoid using `snprintf(3)` in case `MRB_DISABLE_STDIO`; fix #3714 --- mrbgems/mruby-string-ext/src/string.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'mrbgems/mruby-string-ext') diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c index 3b288f1db..0d834b6ef 100644 --- a/mrbgems/mruby-string-ext/src/string.c +++ b/mrbgems/mruby-string-ext/src/string.c @@ -602,17 +602,25 @@ mrb_str_upto(mrb_state *mrb, mrb_value beg) all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg)) && all_digits_p(RSTRING_PTR(end), RSTRING_LEN(end))) { mrb_int min_width = RSTRING_LEN(beg); - mrb_int max_width = RSTRING_LEN(end); mrb_int bi = mrb_int(mrb, mrb_str_to_inum(mrb, beg, 10, FALSE)); mrb_int ei = mrb_int(mrb, mrb_str_to_inum(mrb, end, 10, FALSE)); - mrb_value str = mrb_str_new(mrb, NULL, max_width); int ai = mrb_gc_arena_save(mrb); - char *buf = RSTRING_PTR(str); while (bi <= ei) { + mrb_value ns, str; + if (excl && bi == ei) break; - snprintf(buf, max_width+1, "%.*" MRB_PRId, (int)min_width, bi); - mrb_yield(mrb, block, mrb_str_new(mrb, buf, strlen(buf))); + ns = mrb_format(mrb, "%S", mrb_fixnum_value(bi)); + if (min_width > RSTRING_LEN(ns)) { + str = mrb_str_new(mrb, NULL, min_width); + memset(RSTRING_PTR(str), '0', min_width-RSTRING_LEN(ns)); + memcpy(RSTRING_PTR(str)+min_width-RSTRING_LEN(ns), + RSTRING_PTR(ns), RSTRING_LEN(ns)); + } + else { + str = ns; + } + mrb_yield(mrb, block, str); mrb_gc_arena_restore(mrb, ai); bi++; } -- cgit v1.2.3