diff options
| -rw-r--r-- | mrbgems/mruby-sprintf/src/sprintf.c | 26 | ||||
| -rw-r--r-- | mrbgems/mruby-struct/src/struct.c | 36 | ||||
| -rw-r--r-- | src/class.c | 24 | ||||
| -rw-r--r-- | src/error.c | 12 | ||||
| -rw-r--r-- | src/error.h | 1 | ||||
| -rw-r--r-- | src/kernel.c | 4 | ||||
| -rw-r--r-- | src/load.c | 18 | ||||
| -rw-r--r-- | src/object.c | 2 | ||||
| -rw-r--r-- | src/vm.c | 15 |
9 files changed, 72 insertions, 66 deletions
diff --git a/mrbgems/mruby-sprintf/src/sprintf.c b/mrbgems/mruby-sprintf/src/sprintf.c index 8aff03cbf..1b41eb171 100644 --- a/mrbgems/mruby-sprintf/src/sprintf.c +++ b/mrbgems/mruby-sprintf/src/sprintf.c @@ -83,7 +83,7 @@ mrb_fix2binstr(mrb_state *mrb, mrb_value x, int base) char d; if (base != 2) { - mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid radix %d", base); + mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid radix %S", mrb_fixnum_value(base)); } if (val >= (1 << 10)) @@ -148,17 +148,17 @@ mrb_fix2binstr(mrb_state *mrb, mrb_value x, int base) #define GETARG() (!mrb_undef_p(nextvalue) ? nextvalue : \ posarg == -1 ? \ - (mrb_raisef(mrb, E_ARGUMENT_ERROR, "unnumbered(%d) mixed with numbered", nextarg), mrb_undef_value()) : \ + (mrb_raisef(mrb, E_ARGUMENT_ERROR, "unnumbered(%S) mixed with numbered", mrb_fixnum_value(nextarg)), mrb_undef_value()) : \ posarg == -2 ? \ - (mrb_raisef(mrb, E_ARGUMENT_ERROR, "unnumbered(%d) mixed with named", nextarg), mrb_undef_value()) : \ + (mrb_raisef(mrb, E_ARGUMENT_ERROR, "unnumbered(%S) mixed with named", mrb_fixnum_value(nextarg)), mrb_undef_value()) : \ (posarg = nextarg++, GETNTHARG(posarg))) #define GETPOSARG(n) (posarg > 0 ? \ - (mrb_raisef(mrb, E_ARGUMENT_ERROR, "numbered(%d) after unnumbered(%d)", n, posarg), mrb_undef_value()) : \ + (mrb_raisef(mrb, E_ARGUMENT_ERROR, "numbered(%S) after unnumbered(%S)", mrb_fixnum_value(n), mrb_fixnum_value(posarg)), mrb_undef_value()) : \ posarg == -2 ? \ - (mrb_raisef(mrb, E_ARGUMENT_ERROR, "numbered(%d) after named", n), mrb_undef_value()) : \ + (mrb_raisef(mrb, E_ARGUMENT_ERROR, "numbered(%S) after named", mrb_fixnum_value(n)), mrb_undef_value()) : \ ((n < 1) ? \ - (mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid index - %d$", n), mrb_undef_value()) : \ + (mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid index - %S$", mrb_fixnum_value(n)), mrb_undef_value()) : \ (posarg = -1, GETNTHARG(n)))) #define GETNTHARG(nth) \ @@ -166,9 +166,9 @@ mrb_fix2binstr(mrb_state *mrb, mrb_value x, int base) #define GETNAMEARG(id, name, len) ( \ posarg > 0 ? \ - (mrb_raisef(mrb, E_ARGUMENT_ERROR, "named%.*s after unnumbered(%d)", (len), (name), posarg), mrb_undef_value()) : \ + (mrb_raisef(mrb, E_ARGUMENT_ERROR, "named%S after unnumbered(%S)", mrb_str_new(mrb, (name), (len)), mrb_fixnum_value(posarg)), mrb_undef_value()) : \ posarg == -1 ? \ - (mrb_raisef(mrb, E_ARGUMENT_ERROR, "named%.*s after numbered", (len), (name)), mrb_undef_value()) : \ + (mrb_raisef(mrb, E_ARGUMENT_ERROR, "named%S after numbered", mrb_str_new(mrb, (name), (len))), mrb_undef_value()) : \ (posarg = -2, mrb_hash_fetch(mrb, get_hash(mrb, &hash, argc, argv), id, mrb_undef_value()))) #define GETNUM(n, val) \ @@ -553,7 +553,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt) retry: switch (*p) { default: - mrb_raisef(mrb, E_ARGUMENT_ERROR, "malformed format string - %%%c", *p); + mrb_raisef(mrb, E_ARGUMENT_ERROR, "malformed format string - \\%%S", mrb_str_new(mrb, p, 1)); break; case ' ': @@ -592,7 +592,7 @@ retry: GETNUM(n, width); if (*p == '$') { if (!mrb_undef_p(nextvalue)) { - mrb_raisef(mrb, E_ARGUMENT_ERROR, "value given twice - %d$", n); + mrb_raisef(mrb, E_ARGUMENT_ERROR, "value given twice - %S$", mrb_fixnum_value(n)); } nextvalue = GETPOSARG(n); p++; @@ -612,14 +612,14 @@ retry: for (; p < end && *p != term; ) p++; if (id) { - mrb_raisef(mrb, E_ARGUMENT_ERROR, "name%.*s after <%s>", - (int)(p - start + 1), start, mrb_sym2name(mrb, id)); + mrb_raisef(mrb, E_ARGUMENT_ERROR, "name%S after <%S>", + mrb_str_new(mrb, start, p - start + 1), mrb_sym2str(mrb, id)); } symname = mrb_str_new(mrb, start + 1, p - start - 1); id = mrb_intern_str(mrb, symname); nextvalue = GETNAMEARG(mrb_symbol_value(id), start, (int)(p - start + 1)); if (mrb_undef_p(nextvalue)) { - mrb_raisef(mrb, E_KEY_ERROR, "key%.*s not found", (int)(p - start + 1), start); + mrb_raisef(mrb, E_KEY_ERROR, "key%S not found", mrb_str_new(mrb, start, p - start + 1)); } if (term == '}') goto format_s; p++; diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c index da3200fb9..20ea8b5c5 100644 --- a/mrbgems/mruby-struct/src/struct.c +++ b/mrbgems/mruby-struct/src/struct.c @@ -67,8 +67,8 @@ mrb_struct_members(mrb_state *mrb, mrb_value s) if (!strcmp(mrb_class_name(mrb, mrb_obj_class(mrb, s)), "Struct")) { if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) { mrb_raisef(mrb, E_TYPE_ERROR, - "struct size differs (%" PRIdMRB_INT " required %" PRIdMRB_INT " given)", - RARRAY_LEN(members), RSTRUCT_LEN(s)); + "struct size differs (%S required %S given)", + mrb_fixnum_value(RARRAY_LEN(members)), mrb_fixnum_value(RSTRUCT_LEN(s))); } } return members; @@ -125,7 +125,7 @@ mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id) return ptr[i]; } } - mrb_raisef(mrb, E_NAME_ERROR, "%s is not struct member", mrb_sym2name(mrb, id)); + mrb_raisef(mrb, E_NAME_ERROR, "%S is not struct member", mrb_sym2str(mrb, id)); return mrb_nil_value(); /* not reached */ } @@ -204,8 +204,8 @@ mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val) return ptr[i] = val; } } - mrb_raisef(mrb, E_NAME_ERROR, "`%s' is not a struct member", - mrb_sym2name(mrb, mid)); + mrb_raisef(mrb, E_NAME_ERROR, "`%S' is not a struct member", + mrb_sym2str(mrb, mid)); return mrb_nil_value(); /* not reached */ } @@ -249,7 +249,7 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * k name = mrb_str_to_str(mrb, name); id = mrb_to_id(mrb, name); if (!mrb_is_const_id(id)) { - mrb_raisef(mrb, E_NAME_ERROR, "identifier %s needs to be constant", mrb_string_value_ptr(mrb, name)); + mrb_raisef(mrb, E_NAME_ERROR, "identifier %S needs to be constant", name); } if (mrb_const_defined_at(mrb, klass, id)) { mrb_warn("redefining constant Struct::%s", mrb_string_value_ptr(mrb, name)); @@ -544,7 +544,7 @@ mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id) return ptr[i]; } } - mrb_raisef(mrb, E_NAME_ERROR, "no member '%s' in struct", mrb_sym2name(mrb, id)); + mrb_raisef(mrb, E_NAME_ERROR, "no member '%S' in struct", mrb_sym2str(mrb, id)); return mrb_nil_value(); /* not reached */ } @@ -580,12 +580,12 @@ mrb_struct_aref_n(mrb_state *mrb, mrb_value s, mrb_value idx) if (i < 0) i = RSTRUCT_LEN(s) + i; if (i < 0) mrb_raisef(mrb, E_INDEX_ERROR, - "offset %" PRIdMRB_INT " too small for struct(size:%" PRIdMRB_INT ")", - i, RSTRUCT_LEN(s)); + "offset %S too small for struct(size:%S)", + mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); if (RSTRUCT_LEN(s) <= i) mrb_raisef(mrb, E_INDEX_ERROR, - "offset %" PRIdMRB_INT " too large for struct(size:%" PRIdMRB_INT ")", - i, RSTRUCT_LEN(s)); + "offset %S too large for struct(size:%S)", + mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); return RSTRUCT_PTR(s)[i]; } @@ -608,8 +608,8 @@ mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val) len = RARRAY_LEN(members); if (RSTRUCT_LEN(s) != len) { mrb_raisef(mrb, E_TYPE_ERROR, - "struct size differs (%" PRIdMRB_INT " required %" PRIdMRB_INT " given)", - len, RSTRUCT_LEN(s)); + "struct size differs (%S required %S given)", + mrb_fixnum_value(len), mrb_fixnum_value(RSTRUCT_LEN(s))); } ptr = RSTRUCT_PTR(s); ptr_members = RARRAY_PTR(members); @@ -619,7 +619,7 @@ mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val) return val; } } - mrb_raisef(mrb, E_NAME_ERROR, "no member '%s' in struct", mrb_sym2name(mrb, id)); + mrb_raisef(mrb, E_NAME_ERROR, "no member '%S' in struct", mrb_sym2str(mrb, id)); return val; /* not reach */ } @@ -662,13 +662,13 @@ mrb_struct_aset(mrb_state *mrb, mrb_value s) if (i < 0) i = RSTRUCT_LEN(s) + i; if (i < 0) { mrb_raisef(mrb, E_INDEX_ERROR, - "offset %" PRIdMRB_INT " too small for struct(size:%" PRIdMRB_INT ")", - i, RSTRUCT_LEN(s)); + "offset %S too small for struct(size:%S)", + mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); } if (RSTRUCT_LEN(s) <= i) { mrb_raisef(mrb, E_INDEX_ERROR, - "offset %" PRIdMRB_INT " too large for struct(size:%" PRIdMRB_INT ")", - i, RSTRUCT_LEN(s)); + "offset %S too large for struct(size:%S)", + mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); } return RSTRUCT_PTR(s)[i] = val; } diff --git a/src/class.c b/src/class.c index 4b19ba87e..030d018bc 100644 --- a/src/class.c +++ b/src/class.c @@ -1473,7 +1473,7 @@ check_cv_name(mrb_state *mrb, mrb_sym id) s = mrb_sym2name_len(mrb, id, &len); if (len < 3 || !(s[0] == '@' && s[1] == '@')) { - mrb_name_error(mrb, id, "`%s' is not allowed as a class variable name", s); + mrb_name_error(mrb, id, "`%S' is not allowed as a class variable name", mrb_sym2str(mrb, id)); } } @@ -1595,12 +1595,12 @@ mrb_mod_remove_cvar(mrb_state *mrb, mrb_value mod) if (!mrb_undef_p(val)) return val; if (mrb_cv_defined(mrb, mod, id)){ - mrb_name_error(mrb, id, "cannot remove %s for %s", - mrb_sym2name(mrb, id), mrb_class_name(mrb, mrb_class_ptr(mod))); + mrb_name_error(mrb, id, "cannot remove %S for %S", + mrb_sym2str(mrb, id), mod); } - mrb_name_error(mrb, id, "class variable %s not defined for %s", - mrb_sym2name(mrb, id), mrb_class_name(mrb, mrb_class_ptr(mod))); + mrb_name_error(mrb, id, "class variable %S not defined for %S", + mrb_sym2str(mrb, id), mod); /* not reached */ return mrb_nil_value(); @@ -1645,8 +1645,9 @@ mrb_mod_method_defined(mrb_state *mrb, mrb_value mod) } static void -remove_method(mrb_state *mrb, struct RClass *c, mrb_sym mid) +remove_method(mrb_state *mrb, mrb_value mod, mrb_sym mid) { + struct RClass *c = mrb_class_ptr(mod); khash_t(mt) *h = c->mt; khiter_t k; @@ -1658,8 +1659,8 @@ remove_method(mrb_state *mrb, struct RClass *c, mrb_sym mid) } } - mrb_name_error(mrb, mid, "method `%s' not defined in %s", - mrb_sym2name(mrb, mid), mrb_class_name(mrb, c)); + mrb_name_error(mrb, mid, "method `%S' not defined in %S", + mrb_sym2str(mrb, mid), mod); } /* 15.2.2.4.41 */ @@ -1674,13 +1675,12 @@ remove_method(mrb_state *mrb, struct RClass *c, mrb_sym mid) mrb_value mrb_mod_remove_method(mrb_state *mrb, mrb_value mod) { - struct RClass *c = mrb_class_ptr(mod); int argc; mrb_value *argv; mrb_get_args(mrb, "*", &argv, &argc); while (argc--) { - remove_method(mrb, c, mrb_symbol(*argv)); + remove_method(mrb, mod, mrb_symbol(*argv)); argv++; } return mod; @@ -1694,7 +1694,7 @@ check_const_name(mrb_state *mrb, mrb_sym id) s = mrb_sym2name_len(mrb, id, &len); if (len < 1 || !ISUPPER(*s)) { - mrb_name_error(mrb, id, "wrong constant name %s", s); + mrb_name_error(mrb, id, "wrong constant name %S", mrb_sym2str(mrb, id)); } } @@ -1743,7 +1743,7 @@ mrb_mod_remove_const(mrb_state *mrb, mrb_value mod) check_const_name(mrb, id); val = mrb_iv_remove(mrb, mod, id); if (mrb_undef_p(val)) { - mrb_name_error(mrb, id, "constant %s not defined", mrb_sym2name(mrb, id)); + mrb_name_error(mrb, id, "constant %S not defined", mrb_sym2str(mrb, id)); } return val; } diff --git a/src/error.c b/src/error.c index fd2d1c277..8b8b8bd35 100644 --- a/src/error.c +++ b/src/error.c @@ -298,18 +298,14 @@ mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...) void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...) { - mrb_value exc, argv[2]; + mrb_value exc; + mrb_value argv[2]; va_list args; - char buf[256]; - int n; va_start(args, fmt); - n = vsnprintf(buf, sizeof(buf), fmt, args); + argv[0] = mrb_vformat(mrb, fmt, args); va_end(args); - if (n < 0) { - n = 0; - } - argv[0] = mrb_str_new(mrb, buf, n); + argv[1] = mrb_symbol_value(id); /* ignore now */ exc = mrb_class_new_instance(mrb, 1, argv, E_NAME_ERROR); mrb_exc_raise(mrb, exc); diff --git a/src/error.h b/src/error.h index 53ee2b206..83d0938aa 100644 --- a/src/error.h +++ b/src/error.h @@ -12,6 +12,7 @@ int sysexit_status(mrb_state *mrb, mrb_value err); mrb_value mrb_exc_new3(mrb_state *mrb, struct RClass* c, mrb_value str); mrb_value make_exception(mrb_state *mrb, int argc, mrb_value *argv, int isstr); mrb_value mrb_make_exception(mrb_state *mrb, int argc, mrb_value *argv); +mrb_value mrb_format(mrb_state *mrb, const char *format, ...); void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...); void mrb_exc_print(mrb_state *mrb, struct RObject *exc); diff --git a/src/kernel.c b/src/kernel.c index 23b0153ab..ec6408701 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -556,7 +556,7 @@ check_iv_name(mrb_state *mrb, mrb_sym id) s = mrb_sym2name_len(mrb, id, &len); if (len < 2 || !(s[0] == '@' && s[1] != '@')) { - mrb_name_error(mrb, id, "`%s' is not allowed as an instance variable name", s); + mrb_name_error(mrb, id, "`%S' is not allowed as an instance variable name", mrb_sym2str(mrb, id)); } } @@ -964,7 +964,7 @@ mrb_obj_remove_instance_variable(mrb_state *mrb, mrb_value self) check_iv_name(mrb, sym); val = mrb_iv_remove(mrb, self, sym); if (mrb_undef_p(val)) { - mrb_name_error(mrb, sym, "instance variable %s not defined", mrb_sym2name(mrb, sym)); + mrb_name_error(mrb, sym, "instance variable %S not defined", mrb_sym2str(mrb, sym)); } return val; } diff --git a/src/load.c b/src/load.c index 23b23af5c..cb36c9914 100644 --- a/src/load.c +++ b/src/load.c @@ -233,6 +233,7 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3 char *fname; short *lines; + ret = MRB_DUMP_OK; bin += sizeof(uint32_t); // record size fname_len = bin_to_uint16(bin); bin += sizeof(uint16_t); @@ -258,7 +259,7 @@ read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint3 mrb->irep[irepno]->lines = lines; error_exit: - return MRB_DUMP_OK; + return ret; } static int @@ -271,6 +272,7 @@ read_rite_section_lineno(mrb_state *mrb, const uint8_t *bin, size_t sirep) uint16_t n; const struct rite_section_lineno_header *header; + len = 0; header = (const struct rite_section_lineno_header*)bin; bin += sizeof(struct rite_section_lineno_header); @@ -397,7 +399,9 @@ read_rite_section_lineno_file(mrb_state *mrb, FILE *fp, size_t sirep) const size_t record_header_size = 4; struct rite_section_lineno_header header; - fread(&header, sizeof(struct rite_section_lineno_header), 1, fp); + if (fread(&header, sizeof(struct rite_section_lineno_header), 1, fp) == 0) { + return MRB_DUMP_READ_FAULT; + } nirep = bin_to_uint16(header.nirep); @@ -406,11 +410,17 @@ read_rite_section_lineno_file(mrb_state *mrb, FILE *fp, size_t sirep) //Read Binary Data Section for (n = 0, i = sirep; n < nirep; n++, i++) { - fread(buf, record_header_size, 1, fp); + if (fread(buf, record_header_size, 1, fp) == 0) { + result = MRB_DUMP_READ_FAULT; + goto error_exit; + } buf_size = bin_to_uint32(&buf[0]); buf = (uint8_t *)mrb_realloc(mrb, buf, buf_size); - fread(&buf[record_header_size], buf_size - record_header_size, 1, fp); + if (fread(&buf[record_header_size], buf_size - record_header_size, 1, fp) == 0) { + result = MRB_DUMP_READ_FAULT; + goto error_exit; + } result = read_rite_lineno_record(mrb, buf, i, &len); if (result != MRB_DUMP_OK) goto error_exit; diff --git a/src/object.c b/src/object.c index c3a398db8..a77903e19 100644 --- a/src/object.c +++ b/src/object.c @@ -503,7 +503,7 @@ mrb_to_integer(mrb_state *mrb, mrb_value val, const char *method) if (mrb_fixnum_p(val)) return val; v = convert_type(mrb, val, "Integer", method, TRUE); if (!mrb_obj_is_kind_of(mrb, v, mrb->fixnum_class)) { - mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %s to Integer (%S#%S gives %S)", + mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S to Integer (%S#%S gives %S)", val, val, mrb_str_new_cstr(mrb, method), v); } return v; @@ -464,20 +464,19 @@ localjump_error(mrb_state *mrb, localjump_error_kind kind) static void argnum_error(mrb_state *mrb, int num) { - char buf[256]; - size_t len; mrb_value exc; + mrb_value str; if (mrb->ci->mid) { - len = snprintf(buf, sizeof(buf), "'%s': wrong number of arguments (%d for %d)", - mrb_sym2name(mrb, mrb->ci->mid), - mrb->ci->argc, num); + str = mrb_format(mrb, "'%S': wrong number of arguments (%S for %S)", + mrb_sym2str(mrb, mrb->ci->mid), + mrb_fixnum_value(mrb->ci->argc), mrb_fixnum_value(num)); } else { - len = snprintf(buf, sizeof(buf), "wrong number of arguments (%d for %d)", - mrb->ci->argc, num); + str = mrb_format(mrb, "wrong number of arguments (%S for %S)", + mrb_fixnum_value(mrb->ci->argc), mrb_fixnum_value(num)); } - exc = mrb_exc_new(mrb, E_ARGUMENT_ERROR, buf, len); + exc = mrb_exc_new3(mrb, E_ARGUMENT_ERROR, str); mrb->exc = mrb_obj_ptr(exc); } |
