diff options
| -rw-r--r-- | src/class.c | 10 | ||||
| -rw-r--r-- | src/codegen.c | 4 | ||||
| -rw-r--r-- | src/error.c | 6 | ||||
| -rw-r--r-- | src/hash.c | 4 | ||||
| -rw-r--r-- | src/parse.y | 22 | ||||
| -rw-r--r-- | src/re.c | 18 | ||||
| -rw-r--r-- | src/sprintf.c | 22 | ||||
| -rw-r--r-- | src/string.c | 6 | ||||
| -rw-r--r-- | src/time.c | 2 | ||||
| -rw-r--r-- | src/vm.c | 6 |
10 files changed, 49 insertions, 51 deletions
diff --git a/src/class.c b/src/class.c index 071d7aebe..b53c18590 100644 --- a/src/class.c +++ b/src/class.c @@ -1141,25 +1141,25 @@ mrb_mod_to_s(mrb_state *mrb, mrb_value klass) } else { struct RClass *c = mrb_class_ptr(klass); - const char *cn = mrb_class_name(mrb, c); + const char *cn = mrb_class_name(mrb, c); if (!cn) { char buf[256]; - + int n = 0; switch (mrb_type(klass)) { case MRB_TT_CLASS: - snprintf(buf, 256, "#<Class:%p>", c); + n = snprintf(buf, sizeof(buf), "#<Class:%p>", c); break; case MRB_TT_MODULE: - snprintf(buf, 256, "#<Module:%p>", c); + n = snprintf(buf, sizeof(buf), "#<Module:%p>", c); break; default: break; } - return mrb_str_dup(mrb, mrb_str_new_cstr(mrb, buf)); + return mrb_str_dup(mrb, mrb_str_new(mrb, buf, n)); } else { return mrb_str_dup(mrb, mrb_str_new_cstr(mrb, cn)); diff --git a/src/codegen.c b/src/codegen.c index cbf401a1f..fb0006625 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1537,7 +1537,7 @@ codegen(codegen_scope *s, node *tree, int val) int len; int sym; - len = snprintf(buf, 3, "$%c", (int)(intptr_t)tree); + len = snprintf(buf, sizeof(buf), "$%c", (int)(intptr_t)tree); sym = new_sym(s, mrb_intern2(s->mrb, buf, len)); genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym)); push(); @@ -1550,7 +1550,7 @@ codegen(codegen_scope *s, node *tree, int val) int len; int sym; - len = snprintf(buf, 3, "$%d", (int)(intptr_t)tree); + len = snprintf(buf, sizeof(buf), "$%d", (int)(intptr_t)tree); sym = new_sym(s, mrb_intern2(s->mrb, buf, len)); genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym)); push(); diff --git a/src/error.c b/src/error.c index 1183d8643..e46e37cbb 100644 --- a/src/error.c +++ b/src/error.c @@ -180,7 +180,7 @@ mrb_raise(mrb_state *mrb, struct RClass *c, const char *fmt, ...) int n; va_start(args, fmt); - n = vsnprintf(buf, 256, fmt, args); + n = vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); if (n < 0) { n = 0; @@ -197,7 +197,7 @@ mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...) int n; va_start(args, fmt); - n = vsnprintf(buf, 256, fmt, args); + n = vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); if (n < 0) { n = 0; @@ -216,7 +216,7 @@ mrb_sprintf(mrb_state *mrb, const char *fmt, ...) int n; va_start(args, fmt); - n = vsnprintf(buf, 256, fmt, args); + n = vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); if (n < 0) { n = 0; diff --git a/src/hash.c b/src/hash.c index a87ca97d4..888a1917e 100644 --- a/src/hash.c +++ b/src/hash.c @@ -904,14 +904,14 @@ inspect_hash(mrb_state *mrb, mrb_value hash, int recur) str2 = mrb_inspect(mrb, kh_key(h,k)); mrb_str_append(mrb, str, str2); - mrb_str_buf_cat(mrb, str, "=>", sizeof("=>")); + mrb_str_buf_cat(mrb, str, "=>", 2); str2 = mrb_inspect(mrb, kh_value(h,k)); mrb_str_append(mrb, str, str2); mrb_gc_arena_restore(mrb, ai); } } - mrb_str_buf_cat(mrb, str, "}", sizeof("}")); + mrb_str_buf_cat(mrb, str, "}", 1); return str; } diff --git a/src/parse.y b/src/parse.y index 4c9f88889..d243941e8 100644 --- a/src/parse.y +++ b/src/parse.y @@ -2587,7 +2587,7 @@ var_ref : variable { char buf[16]; - snprintf(buf, 16, "%d", p->lineno); + snprintf(buf, sizeof(buf), "%d", p->lineno); $$ = new_int(p, buf, 10); } ; @@ -2955,7 +2955,7 @@ yyerror_i(parser_state *p, const char *fmt, int i) { char buf[256]; - snprintf(buf, 256, fmt, i); + snprintf(buf, sizeof(buf), fmt, i); yyerror(p, buf); } @@ -2995,7 +2995,7 @@ yywarning_s(parser_state *p, const char *fmt, const char *s) { char buf[256]; - snprintf(buf, 256, fmt, s); + snprintf(buf, sizeof(buf), fmt, s); yywarning(p, buf); } @@ -3201,9 +3201,9 @@ toklen(parser_state *p) #define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1)) static unsigned long -scan_oct(const char *start, int len, int *retlen) +scan_oct(const int *start, int len, int *retlen) { - const char *s = start; + const int *s = start; unsigned long retval = 0; while (len-- && *s >= '0' && *s <= '7') { @@ -3215,10 +3215,10 @@ scan_oct(const char *start, int len, int *retlen) } static unsigned long -scan_hex(const char *start, int len, int *retlen) +scan_hex(const int *start, int len, int *retlen) { static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF"; - register const char *s = start; + register const int *s = start; register unsigned long retval = 0; char *tmp; @@ -3264,7 +3264,7 @@ read_escape(parser_state *p) case '0': case '1': case '2': case '3': /* octal constant */ case '4': case '5': case '6': case '7': { - char buf[3]; + int buf[3]; int i; for (i=0; i<3; i++) { @@ -3281,7 +3281,7 @@ read_escape(parser_state *p) case 'x': /* hex constant */ { - char buf[2]; + int buf[2]; int i; for (i=0; i<2; i++) { @@ -3702,7 +3702,7 @@ parser_yylex(parser_state *p) } if (c2) { char buf[256]; - snprintf(buf, 256, "invalid character syntax; use ?\\%c", c2); + snprintf(buf, sizeof(buf), "invalid character syntax; use ?\\%c", c2); yyerror(p, buf); } } @@ -4542,7 +4542,7 @@ parser_yylex(parser_state *p) pushback(p, c); } } - if (result == 0 && isupper(tok(p)[0])) { + if (result == 0 && isupper((int)tok(p)[0])) { result = tCONSTANT; } else { @@ -364,7 +364,7 @@ mrb_reg_desc(mrb_state *mrb, const char *s, long len, mrb_value re) if (*option_to_str(opts, RREGEXP(re)->ptr->options)) mrb_str_buf_cat(mrb, str, opts, strlen(opts));//mrb_str_buf_cat2(str, opts); if (RBASIC(re)->flags & REG_ENCODING_NONE) - mrb_str_buf_cat(mrb, str, "n", sizeof("n")); + mrb_str_buf_cat(mrb, str, "n", 1); } return str; @@ -1797,9 +1797,9 @@ again: mrb_str_buf_cat(mrb, str, optbuf, strlen(optbuf)); } - mrb_str_buf_cat(mrb, str, ":", sizeof(":")); + mrb_str_buf_cat(mrb, str, ":", 1); mrb_reg_expr_str(mrb, str, (char*)ptr, len); - mrb_str_buf_cat(mrb, str, ")", sizeof(")")); + mrb_str_buf_cat(mrb, str, ")", 1); return str; } @@ -1925,23 +1925,23 @@ mrb_match_inspect(mrb_state *mrb, mrb_value match) for (i = 0; i < num_regs; i++) { char buf[sizeof(num_regs)*3+1]; mrb_value v; - mrb_str_buf_cat(mrb, str, " ", sizeof(" ")); + mrb_str_buf_cat(mrb, str, " ", 1); if (0 < i) { if (names[i].name) mrb_str_buf_cat(mrb, str, (const char*)names[i].name, names[i].len); else { - sprintf(buf, "%d", i); - mrb_str_buf_cat(mrb, str, (const char*)buf, strlen(buf)); + int n = sprintf(buf, "%d", i); + mrb_str_buf_cat(mrb, str, (const char*)buf, n); } - mrb_str_buf_cat(mrb, str, ":", sizeof(":")); + mrb_str_buf_cat(mrb, str, ":", 1); } v = mrb_reg_nth_match(mrb, i, match); if (mrb_nil_p(v)) - mrb_str_buf_cat(mrb, str, "nil", sizeof("nil")); + mrb_str_buf_cat(mrb, str, "nil", 3); else mrb_str_buf_append(mrb, str, mrb_str_inspect(mrb, v)); } - mrb_str_buf_cat(mrb, str, ">", sizeof(">")); + mrb_str_buf_cat(mrb, str, ">", 1); return str; } diff --git a/src/sprintf.c b/src/sprintf.c index 68addef89..d6104ad48 100644 --- a/src/sprintf.c +++ b/src/sprintf.c @@ -496,7 +496,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt) char *buf; long blen, bsiz; mrb_value result; - + int n; int width, prec, flags = FNONE; int nextarg = 1; int posarg = 0; @@ -533,7 +533,6 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt) for (; p < end; p++) { const char *t; - int n; mrb_sym id = 0; for (t = p; t < end && *t != '%'; t++) ; @@ -669,7 +668,6 @@ retry: mrb_value val = GETARG(); mrb_value tmp; unsigned int c; - int n; tmp = mrb_check_string_type(mrb, val); if (!mrb_nil_p(tmp)) { @@ -984,6 +982,7 @@ bin_retry: fval = mrb_float(mrb_Float(mrb, val)); if (isnan(fval) || isinf(fval)) { const char *expr; + const int elen = 3; if (isnan(fval)) { expr = "NaN"; @@ -991,14 +990,14 @@ bin_retry: else { expr = "Inf"; } - need = (int)strlen(expr); + need = elen; if ((!isnan(fval) && fval < 0.0) || (flags & FPLUS)) need++; if ((flags & FWIDTH) && need < width) need = width; CHECK(need + 1); - snprintf(&buf[blen], need + 1, "%*s", need, ""); + n = snprintf(&buf[blen], need + 1, "%*s", need, ""); if (flags & FMINUS) { if (!isnan(fval) && fval < 0.0) buf[blen++] = '-'; @@ -1006,17 +1005,16 @@ bin_retry: buf[blen++] = '+'; else if (flags & FSPACE) blen++; - memcpy(&buf[blen], expr, strlen(expr)); + memcpy(&buf[blen], expr, elen); } else { if (!isnan(fval) && fval < 0.0) - buf[blen + need - strlen(expr) - 1] = '-'; + buf[blen + need - elen - 1] = '-'; else if (flags & FPLUS) - buf[blen + need - strlen(expr) - 1] = '+'; + buf[blen + need - elen - 1] = '+'; else if ((flags & FSPACE) && need > width) blen++; - memcpy(&buf[blen + need - strlen(expr)], expr, - strlen(expr)); + memcpy(&buf[blen + need - elen], expr, elen); } blen += strlen(&buf[blen]); break; @@ -1036,8 +1034,8 @@ bin_retry: need += 20; CHECK(need); - snprintf(&buf[blen], need, fbuf, fval); - blen += strlen(&buf[blen]); + n = snprintf(&buf[blen], need, fbuf, fval); + blen += n; } break; } diff --git a/src/string.c b/src/string.c index c74dcedde..8e61e6bb9 100644 --- a/src/string.c +++ b/src/string.c @@ -2983,12 +2983,12 @@ mrb_str_inspect(mrb_state *mrb, mrb_value str) continue; } else { - sprintf(buf, "\\%03o", c & 0377); - mrb_str_buf_cat(mrb, result, buf, strlen(buf)); + int n = sprintf(buf, "\\%03o", c & 0377); + mrb_str_buf_cat(mrb, result, buf, n); continue; } } - mrb_str_buf_cat(mrb, result, "\"", sizeof("\"")); + mrb_str_buf_cat(mrb, result, "\"", 1); return result; } diff --git a/src/time.c b/src/time.c index 28cc3c1a3..3da824e82 100644 --- a/src/time.c +++ b/src/time.c @@ -435,7 +435,7 @@ mrb_time_asctime(mrb_state *mrb, mrb_value self) tm = mrb_get_datatype(mrb, self, &mrb_time_type); if (!tm) return mrb_nil_value(); d = &tm->datetime; - len = snprintf(buf, 256, "%s %s %02d %02d:%02d:%02d %s%d", + len = snprintf(buf, sizeof(buf), "%s %s %02d %02d:%02d:%02d %s%d", wday_names[d->tm_wday], mon_names[d->tm_mon], d->tm_mday, d->tm_hour, d->tm_min, d->tm_sec, tm->timezone == MRB_TIMEZONE_UTC ? "UTC " : "", @@ -295,7 +295,7 @@ localjump_error(mrb_state *mrb, const char *kind) int len; mrb_value exc; - len = snprintf(buf, 256, "unexpected %s", kind); + len = snprintf(buf, sizeof(buf), "unexpected %s", kind); exc = mrb_exc_new(mrb, E_LOCALJUMP_ERROR, buf, len); mrb->exc = (struct RObject*)mrb_object(exc); } @@ -308,12 +308,12 @@ argnum_error(mrb_state *mrb, int num) mrb_value exc; if (mrb->ci->mid) { - len = snprintf(buf, 256, "'%s': wrong number of arguments (%d for %d)", + len = snprintf(buf, sizeof(buf), "'%s': wrong number of arguments (%d for %d)", mrb_sym2name(mrb, mrb->ci->mid), mrb->ci->argc, num); } else { - len = snprintf(buf, 256, "wrong number of arguments (%d for %d)", + len = snprintf(buf, sizeof(buf), "wrong number of arguments (%d for %d)", mrb->ci->argc, num); } exc = mrb_exc_new(mrb, E_ARGUMENT_ERROR, buf, len); |
