X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=format_check.c;h=59902485ed6924aa50f0ed0049a37395488c7c9b;hb=1789a2fe5989073d07da3b46f7ea95d40a0c46bf;hp=8b9bc6396ea6f73bef619eb5a4d1990730a0b3f8;hpb=1c43487a8633dca3329e08e2c829ec9ceb13c91b;p=cparser diff --git a/format_check.c b/format_check.c index 8b9bc63..5990248 100644 --- a/format_check.c +++ b/format_check.c @@ -157,8 +157,11 @@ static void check_printf_format(const call_argument_t *arg, const format_spec_t { /* find format arg */ unsigned idx = 0; - for (; idx < spec->fmt_idx; ++idx) + for (; idx < spec->fmt_idx; ++idx) { + if (arg == NULL) + return; arg = arg->next; + } const expression_t *fmt_expr = arg->expression; if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) { @@ -182,7 +185,7 @@ static void check_printf_format(const call_argument_t *arg, const format_spec_t return; } /* find the real args */ - for(; idx < spec->arg_idx; ++idx) + for(; idx < spec->arg_idx && arg != NULL; ++idx) arg = arg->next; const source_position_t *pos = &fmt_expr->base.source_position; @@ -515,20 +518,24 @@ eval_fmt_mod_unsigned: default: warningf(pos, "encountered unknown conversion specifier '%%%C' at position %u", (wint_t)fmt, num_fmt); + if (arg == NULL) { + warningf(pos, "too few arguments for format string"); + return; + } goto next_arg; } format_flags_t wrong_flags = fmt_flags & ~allowed_flags; if (wrong_flags != 0) { - char wrong[8]; - int idx = 0; - if (wrong_flags & FMT_FLAG_HASH) wrong[idx++] = '#'; - if (wrong_flags & FMT_FLAG_ZERO) wrong[idx++] = '0'; - if (wrong_flags & FMT_FLAG_MINUS) wrong[idx++] = '-'; - if (wrong_flags & FMT_FLAG_SPACE) wrong[idx++] = ' '; - if (wrong_flags & FMT_FLAG_PLUS) wrong[idx++] = '+'; - if (wrong_flags & FMT_FLAG_TICK) wrong[idx++] = '\''; - wrong[idx] = '\0'; + char wrong[8]; + char *p = wrong; + if (wrong_flags & FMT_FLAG_HASH) *p++ = '#'; + if (wrong_flags & FMT_FLAG_ZERO) *p++ = '0'; + if (wrong_flags & FMT_FLAG_MINUS) *p++ = '-'; + if (wrong_flags & FMT_FLAG_SPACE) *p++ = ' '; + if (wrong_flags & FMT_FLAG_PLUS) *p++ = '+'; + if (wrong_flags & FMT_FLAG_TICK) *p++ = '\''; + *p = '\0'; warningf(pos, "invalid format flags \"%s\" in conversion specification %%%c at position %u", wrong, fmt, num_fmt); } @@ -573,7 +580,7 @@ next_arg: arg = arg->next; } if (!atend(&vchar)) { - warningf(pos, "format string contains NUL"); + warningf(pos, "format string contains '\\0'"); } if (arg != NULL) { unsigned num_args = num_fmt; @@ -581,7 +588,7 @@ next_arg: ++num_args; arg = arg->next; } - warningf(pos, "%u argument%s but only %u format string%s", + warningf(pos, "%u argument%s but only %u format specifier%s", num_args, num_args != 1 ? "s" : "", num_fmt, num_fmt != 1 ? "s" : ""); } @@ -594,8 +601,11 @@ static void check_scanf_format(const call_argument_t *arg, const format_spec_t * { /* find format arg */ unsigned idx = 0; - for (; idx < spec->fmt_idx; ++idx) + for (; idx < spec->fmt_idx; ++idx) { + if (arg == NULL) + return; arg = arg->next; + } const expression_t *fmt_expr = arg->expression; if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) { @@ -619,7 +629,7 @@ static void check_scanf_format(const call_argument_t *arg, const format_spec_t * return; } /* find the real args */ - for (; idx < spec->arg_idx; ++idx) + for (; idx < spec->arg_idx && arg != NULL; ++idx) arg = arg->next; const source_position_t *pos = &fmt_expr->base.source_position; @@ -843,6 +853,10 @@ eval_fmt_mod_unsigned: default: warningf(pos, "encountered unknown conversion specifier '%%%C' at position %u", (wint_t)fmt, num_fmt); + if (arg == NULL) { + warningf(pos, "too few arguments for format string"); + return; + } goto next_arg; } @@ -888,7 +902,7 @@ next_arg: arg = arg->next; } if (!atend(&vchar)) { - warningf(pos, "format string contains NUL"); + warningf(pos, "format string contains '\\0'"); } if (arg != NULL) { unsigned num_args = num_fmt; @@ -896,7 +910,7 @@ next_arg: ++num_args; arg = arg->next; } - warningf(pos, "%u argument%s but only %u format string%s", + warningf(pos, "%u argument%s but only %u format specifier%s", num_args, num_args != 1 ? "s" : "", num_fmt, num_fmt != 1 ? "s" : ""); }