X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=format_check.c;h=5d7f0a1dad7a8abde5277fe7d285427977c24aed;hb=54ae1a7a0e8a1a759de3bb941ed972ea1fc19a54;hp=a399fb24d3623f425fe45696d648fe52a5717102;hpb=c4db1fa784bcac24d6cd298c80ed037da847dc5f;p=cparser diff --git a/format_check.c b/format_check.c index a399fb2..5d7f0a1 100644 --- a/format_check.c +++ b/format_check.c @@ -20,6 +20,7 @@ #include #include +#include "adt/util.h" #include "format_check.h" #include "symbol_t.h" #include "ast_t.h" @@ -86,7 +87,7 @@ static const char* get_length_modifier_name(const format_length_modifier_t mod) [FMT_MOD_I32] = "I32", [FMT_MOD_I64] = "I64" }; - assert(mod < sizeof(names) / sizeof(*names)); + assert(mod < lengthof(names)); return names[mod]; } @@ -114,39 +115,46 @@ struct vchar_t { int (*is_digit)(unsigned vchar); }; -static unsigned string_first(vchar_t *self) { +static unsigned string_first(vchar_t *self) +{ self->position = 0; const string_t *string = self->string; return string->begin[0]; } -static unsigned string_next(vchar_t *self) { +static unsigned string_next(vchar_t *self) +{ ++self->position; const string_t *string = self->string; return string->begin[self->position]; } -static int string_isdigit(unsigned vchar) { +static int string_isdigit(unsigned vchar) +{ return isdigit(vchar); } -static unsigned wstring_first(vchar_t *self) { +static unsigned wstring_first(vchar_t *self) +{ self->position = 0; const wide_string_t *wstring = self->string; return wstring->begin[0]; } -static unsigned wstring_next(vchar_t *self) { +static unsigned wstring_next(vchar_t *self) +{ ++self->position; const wide_string_t *wstring = self->string; return wstring->begin[self->position]; } -static int wstring_isdigit(unsigned vchar) { +static int wstring_isdigit(unsigned vchar) +{ return iswdigit(vchar); } -static bool atend(vchar_t *self) { +static bool atend(vchar_t *self) +{ return self->position + 1 == self->size; } @@ -157,8 +165,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 +193,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,6 +526,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; } @@ -573,7 +588,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; @@ -594,8 +609,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 +637,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 +861,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 +910,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 +918,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" : ""); } @@ -971,7 +993,7 @@ void check_format(const call_expression_t *const call) * This allows to check format even in MS mode or without header included. */ const char *const name = entity->base.symbol->string; - for (size_t i = 0; i < sizeof(builtin_table) / sizeof(builtin_table[0]); ++i) { + for (size_t i = 0; i < lengthof(builtin_table); ++i) { if (strcmp(name, builtin_table[i].name) == 0) { switch (builtin_table[i].fmt_kind) { case FORMAT_PRINTF: