Skip to content

Commit

Permalink
The width printf specifier may be negative; fix #3498
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Mar 11, 2017
1 parent f30ec2d commit 94395e8
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions mrbgems/mruby-sprintf/src/sprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ mrb_fix2binstr(mrb_state *mrb, mrb_value x, int base)

#define CHECK(l) do {\
/* int cr = ENC_CODERANGE(result);*/\
if ((l) < 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "illegal specifier"); \
while ((l) >= bsiz - blen) {\
bsiz*=2;\
if (bsiz < 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "too big specifier"); \
Expand Down Expand Up @@ -766,7 +767,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
width -= (int)slen;
if (!(flags&FMINUS)) {
CHECK(width);
while (width--) {
while (width-- > 0) {
buf[blen++] = ' ';
}
}
Expand All @@ -775,7 +776,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
blen += len;
if (flags&FMINUS) {
CHECK(width);
while (width--) {
while (width-- > 0) {
buf[blen++] = ' ';
}
}
Expand Down Expand Up @@ -982,7 +983,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
width -= prec;
}

if (!(flags&FMINUS)) {
if (!(flags&FMINUS) && width > 0) {
CHECK(width);
while (width-- > 0) {
buf[blen++] = ' ';
Expand Down Expand Up @@ -1012,9 +1013,11 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
}

PUSH(s, len);
CHECK(width);
while (width-- > 0) {
buf[blen++] = ' ';
if (width > 0) {
CHECK(width);
while (width-- > 0) {
buf[blen++] = ' ';
}
}
}
break;
Expand Down

0 comments on commit 94395e8

Please sign in to comment.