#include <ctype.h>
#include <wctype.h>
+#include "adt/util.h"
#include "format_check.h"
#include "symbol_t.h"
#include "ast_t.h"
[FMT_MOD_I32] = "I32",
[FMT_MOD_I64] = "I64"
};
- assert(mod < sizeof(names) / sizeof(*names));
+ assert(mod < lengthof(names));
return names[mod];
}
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;
}
{
/* 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) {
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;
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);
}
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;
++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" : "");
}
{
/* 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) {
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;
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;
}
goto next_arg;
}
- if (ptr_skip == expected_type_skip) {
+ /* do NOT allow const or restrict, all other should be ok */
+ if (ptr_skip->base.qualifiers & (TYPE_QUALIFIER_CONST | TYPE_QUALIFIER_VOLATILE))
+ goto error_arg_type;
+ type_t *const unqual_ptr = get_unqualified_type(ptr_skip);
+ if (unqual_ptr == expected_type_skip) {
goto next_arg;
} else if (expected_type_skip == type_char) {
/* char matches with unsigned char AND signed char */
- if (ptr_skip == type_signed_char || ptr_skip == type_unsigned_char)
+ if (unqual_ptr == type_signed_char || unqual_ptr == type_unsigned_char)
goto next_arg;
}
error_arg_type:
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;
++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" : "");
}
* 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:
case FORMAT_SCANF:
check_scanf_format(arg, &builtin_table[i]);
break;
- default:
+ case FORMAT_STRFTIME:
+ case FORMAT_STRFMON:
/* TODO: implement other cases */
break;
}