Commit 7c64a2be authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #329 from monaka/pr-reduce-magic-number-in-snprintf

Use sizeof to get char array sizes.
parents cf8b6ec8 2042684b
......@@ -1149,11 +1149,11 @@ mrb_mod_to_s(mrb_state *mrb, mrb_value klass)
switch (mrb_type(klass)) {
case MRB_TT_CLASS:
snprintf(buf, 256, "#<Class:%p>", c);
snprintf(buf, sizeof(buf), "#<Class:%p>", c);
break;
case MRB_TT_MODULE:
snprintf(buf, 256, "#<Module:%p>", c);
snprintf(buf, sizeof(buf), "#<Module:%p>", c);
break;
default:
......
......@@ -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();
......
......@@ -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;
......
......@@ -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);
}
......@@ -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);
}
}
......
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment