From 88cd807379152ea3fec5f534e5f4d6ebebd53982 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 25 Apr 2017 10:39:11 +0900 Subject: Avoid use of `snprintf()` when DISABLE_STDIO is set; fix #3632 ref #3492 #3515 #3517 --- src/backtrace.c | 11 ++++------- src/error.c | 25 ++++++++----------------- 2 files changed, 12 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/backtrace.c b/src/backtrace.c index d634123b4..b60c375f3 100644 --- a/src/backtrace.c +++ b/src/backtrace.c @@ -74,14 +74,12 @@ static void get_backtrace_i(mrb_state *mrb, struct backtrace_location *loc, void *data) { mrb_value ary, str; - char buf[32]; int ai = mrb_gc_arena_save(mrb); ary = mrb_obj_value((struct RArray*)data); str = mrb_str_new_cstr(mrb, loc->filename); - snprintf(buf, sizeof(buf), ":%d", loc->lineno); - mrb_str_cat_cstr(mrb, str, buf); + str = mrb_format(mrb, "%S:%S", str, mrb_fixnum_value(loc->lineno)); if (loc->method) { mrb_str_cat_lit(mrb, str, ":in "); @@ -400,14 +398,13 @@ mrb_restore_backtrace(mrb_state *mrb) int ai; mrb_backtrace_entry *entry; mrb_value mrb_entry; - char buf[32]; ai = mrb_gc_arena_save(mrb); entry = &(mrb->backtrace.entries[i]); - mrb_entry = mrb_str_new_cstr(mrb, entry->filename); - snprintf(buf, sizeof(buf), ":%d", entry->lineno); - mrb_str_cat_cstr(mrb, mrb_entry, buf); + mrb_entry = mrb_format(mrb, "%S:%S", + mrb_str_new_cstr(mrb, entry->filename), + mrb_fixnum_value(entry->lineno)); if (entry->method_id != 0) { mrb_str_cat_lit(mrb, mrb_entry, ":in "); diff --git a/src/error.c b/src/error.c index ac11e4d8d..258a8f7df 100644 --- a/src/error.c +++ b/src/error.c @@ -137,6 +137,7 @@ exc_inspect(mrb_state *mrb, mrb_value exc) { mrb_value str, mesg, file, line; mrb_bool append_mesg; + const char *cname; mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg")); file = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "file")); @@ -148,28 +149,18 @@ exc_inspect(mrb_state *mrb, mrb_value exc) append_mesg = RSTRING_LEN(mesg) > 0; } + cname = mrb_obj_classname(mrb, exc); + str = mrb_str_new_cstr(mrb, cname); if (mrb_string_p(file) && mrb_fixnum_p(line)) { - char buf[32]; - - str = mrb_str_dup(mrb, file); - snprintf(buf, sizeof(buf), ":%" MRB_PRId ": ", mrb_fixnum(line)); - mrb_str_cat_cstr(mrb, str, buf); if (append_mesg) { - mrb_str_cat_str(mrb, str, mesg); - mrb_str_cat_lit(mrb, str, " ("); + str = mrb_format(mrb, "%S:%S:%S (%S)", file, line, mesg, str); } - mrb_str_cat_cstr(mrb, str, mrb_obj_classname(mrb, exc)); - if (append_mesg) { - mrb_str_cat_lit(mrb, str, ")"); + else { + str = mrb_format(mrb, "%S:%S:%S", file, line, str); } } - else { - const char *cname = mrb_obj_classname(mrb, exc); - str = mrb_str_new_cstr(mrb, cname); - if (append_mesg) { - mrb_str_cat_lit(mrb, str, ": "); - mrb_str_cat_str(mrb, str, mesg); - } + else if (append_mesg) { + str = mrb_format(mrb, "%S:%S", str, mesg); } return str; } -- cgit v1.2.3