2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #include "format_check.h"
26 #include "diagnostic.h"
30 #include "lang_features.h"
32 typedef enum format_flag_t {
34 FMT_FLAG_HASH = 1U << 0,
35 FMT_FLAG_ZERO = 1U << 1,
36 FMT_FLAG_MINUS = 1U << 2,
37 FMT_FLAG_SPACE = 1U << 3,
38 FMT_FLAG_PLUS = 1U << 4,
39 FMT_FLAG_TICK = 1U << 5
42 typedef unsigned format_flags_t;
44 typedef enum format_length_modifier_t {
55 /* only in microsoft mode */
60 } format_length_modifier_t;
62 static const char* get_length_modifier_name(const format_length_modifier_t mod)
64 static const char* const names[] = {
75 /* only in microsoft mode */
78 [FMT_MOD_I32] = "I32",
81 assert(mod < sizeof(names) / sizeof(*names));
85 static void warn_invalid_length_modifier(const source_position_t *pos,
86 const format_length_modifier_t mod,
87 const wchar_rep_t conversion)
90 "invalid length modifier '%s' for conversion specifier '%%%c'",
91 get_length_modifier_name(mod), conversion
95 typedef struct vchar_t vchar_t;
97 const void *string; /**< the string */
98 size_t position; /**< current position */
99 size_t size; /**< size of the string */
101 /** return the first character of the string and setthe position to 0. */
102 unsigned (*first)(vchar_t *self);
103 /** return the next character of the string */
104 unsigned (*next)(vchar_t *self);
105 /** return non_zero if the given character is a digit */
106 int (*is_digit)(unsigned vchar);
109 static unsigned string_first(vchar_t *self) {
111 const string_t *string = self->string;
112 return string->begin[0];
115 static unsigned string_next(vchar_t *self) {
117 const string_t *string = self->string;
118 return string->begin[self->position];
121 static int string_isdigit(unsigned vchar) {
122 return isdigit(vchar);
125 static unsigned wstring_first(vchar_t *self) {
127 const wide_string_t *wstring = self->string;
128 return wstring->begin[0];
131 static unsigned wstring_next(vchar_t *self) {
133 const wide_string_t *wstring = self->string;
134 return wstring->begin[self->position];
137 static int wstring_isdigit(unsigned vchar) {
138 return iswdigit(vchar);
141 static bool atend(vchar_t *self) {
142 return self->position + 1 == self->size;
146 * Check printf-style format.
148 static void check_format_arguments(const call_argument_t *arg, unsigned idx_fmt,
151 unsigned num_fmt = 0;
153 /* find format arg */
155 for (; idx < idx_fmt; ++idx)
158 const expression_t *fmt_expr = arg->expression;
159 if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
160 fmt_expr = fmt_expr->unary.value;
164 if (fmt_expr->kind == EXPR_WIDE_STRING_LITERAL) {
165 vchar.string = &fmt_expr->wide_string.value;
166 vchar.size = fmt_expr->wide_string.value.size;
167 vchar.first = wstring_first;
168 vchar.next = wstring_next;
169 vchar.is_digit = wstring_isdigit;
170 } else if (fmt_expr->kind == EXPR_STRING_LITERAL) {
171 vchar.string = &fmt_expr->string.value;
172 vchar.size = fmt_expr->string.value.size;
173 vchar.first = string_first;
174 vchar.next = string_next;
175 vchar.is_digit = string_isdigit;
179 /* find the real args */
180 for(; idx < idx_param; ++idx)
183 const source_position_t *pos = &fmt_expr->base.source_position;
184 unsigned fmt = vchar.first(&vchar);
185 for (; fmt != '\0'; fmt = vchar.next(&vchar)) {
188 fmt = vchar.next(&vchar);
195 format_flags_t fmt_flags = FMT_FLAG_NONE;
197 fmt = vchar.next(&vchar);
198 fmt_flags |= FMT_FLAG_ZERO;
201 /* argument selector or minimum field width */
202 if (vchar.is_digit(fmt)) {
204 fmt = vchar.next(&vchar);
205 } while (vchar.is_digit(fmt));
207 /* digit string was ... */
209 /* ... argument selector */
210 fmt_flags = FMT_FLAG_NONE; /* reset possibly set 0-flag */
214 /* ... minimum field width */
220 case '#': flag = FMT_FLAG_HASH; break;
221 case '0': flag = FMT_FLAG_ZERO; break;
222 case '-': flag = FMT_FLAG_MINUS; break;
223 case '\'': flag = FMT_FLAG_TICK; break;
226 if (fmt_flags & FMT_FLAG_PLUS) {
227 warningf(pos, "' ' is overridden by prior '+' in conversion specification");
229 flag = FMT_FLAG_SPACE;
233 if (fmt_flags & FMT_FLAG_SPACE) {
234 warningf(pos, "'+' overrides prior ' ' in conversion specification");
236 flag = FMT_FLAG_PLUS;
239 default: goto break_fmt_flags;
241 if (fmt_flags & flag) {
242 warningf(pos, "repeated flag '%c' in conversion specification", (char)fmt);
245 fmt = vchar.next(&vchar);
249 /* minimum field width */
251 fmt = vchar.next(&vchar);
253 warningf(pos, "missing argument for '*' field width in conversion specification");
256 const type_t *const arg_type = arg->expression->base.type;
257 if (arg_type != type_int) {
258 warningf(pos, "argument for '*' field width in conversion specification is not an 'int', but an '%T'", arg_type);
262 while (vchar.is_digit(fmt)) {
263 fmt = vchar.next(&vchar);
270 fmt = vchar.next(&vchar);
272 fmt = vchar.next(&vchar);
274 warningf(pos, "missing argument for '*' precision in conversion specification");
277 const type_t *const arg_type = arg->expression->base.type;
278 if (arg_type != type_int) {
279 warningf(pos, "argument for '*' precision in conversion specification is not an 'int', but an '%T'", arg_type);
283 /* digit string may be omitted */
284 while (vchar.is_digit(fmt)) {
285 fmt = vchar.next(&vchar);
290 /* length modifier */
291 format_length_modifier_t fmt_mod;
294 fmt = vchar.next(&vchar);
296 fmt = vchar.next(&vchar);
297 fmt_mod = FMT_MOD_hh;
304 fmt = vchar.next(&vchar);
306 fmt = vchar.next(&vchar);
307 fmt_mod = FMT_MOD_ll;
313 case 'L': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_L; break;
314 case 'j': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_j; break;
315 case 't': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_t; break;
316 case 'z': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_z; break;
317 case 'q': fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_q; break;
321 fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_w;
323 fmt_mod = FMT_MOD_NONE;
328 fmt = vchar.next(&vchar); fmt_mod = FMT_MOD_I;
330 fmt = vchar.next(&vchar);
332 fmt = vchar.next(&vchar);
333 fmt_mod = FMT_MOD_I32;
338 } else if (fmt == '6') {
339 fmt = vchar.next(&vchar);
341 fmt = vchar.next(&vchar);
342 fmt_mod = FMT_MOD_I64;
349 fmt_mod = FMT_MOD_NONE;
353 fmt_mod = FMT_MOD_NONE;
358 warningf(pos, "dangling %% in format string");
362 type_t *expected_type;
363 type_qualifiers_t expected_qual = TYPE_QUALIFIER_NONE;
364 format_flags_t allowed_flags;
369 case FMT_MOD_NONE: expected_type = type_int; break;
370 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted signed char */
371 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted short */
372 case FMT_MOD_l: expected_type = type_long; break;
373 case FMT_MOD_ll: expected_type = type_long_long; break;
374 case FMT_MOD_j: expected_type = type_intmax_t; break;
375 case FMT_MOD_z: expected_type = type_ssize_t; break;
376 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
377 case FMT_MOD_I: expected_type = type_ptrdiff_t; break;
378 case FMT_MOD_I32: expected_type = type_int32; break;
379 case FMT_MOD_I64: expected_type = type_int64; break;
382 warn_invalid_length_modifier(pos, fmt_mod, fmt);
385 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
391 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
392 goto eval_fmt_mod_unsigned;
396 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
397 eval_fmt_mod_unsigned:
399 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
400 case FMT_MOD_hh: expected_type = type_int; break; /* TODO promoted unsigned char */
401 case FMT_MOD_h: expected_type = type_int; break; /* TODO promoted unsigned short */
402 case FMT_MOD_l: expected_type = type_unsigned_long; break;
403 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
404 case FMT_MOD_j: expected_type = type_uintmax_t; break;
405 case FMT_MOD_z: expected_type = type_size_t; break;
406 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
407 case FMT_MOD_I: expected_type = type_size_t; break;
408 case FMT_MOD_I32: expected_type = type_unsigned_int32; break;
409 case FMT_MOD_I64: expected_type = type_unsigned_int64; break;
412 warn_invalid_length_modifier(pos, fmt_mod, fmt);
426 case FMT_MOD_l: /* l modifier is ignored */
427 case FMT_MOD_NONE: expected_type = type_double; break;
428 case FMT_MOD_L: expected_type = type_long_double; break;
431 warn_invalid_length_modifier(pos, fmt_mod, fmt);
434 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
438 if (fmt_mod != FMT_MOD_NONE) {
439 warn_invalid_length_modifier(pos, fmt_mod, fmt);
442 expected_type = type_wchar_t;
443 allowed_flags = FMT_FLAG_NONE;
447 expected_type = type_int;
449 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
450 case FMT_MOD_l: expected_type = type_wint_t; break;
451 case FMT_MOD_w: expected_type = type_wchar_t; break;
454 warn_invalid_length_modifier(pos, fmt_mod, fmt);
457 allowed_flags = FMT_FLAG_NONE;
461 if (fmt_mod != FMT_MOD_NONE) {
462 warn_invalid_length_modifier(pos, fmt_mod, fmt);
465 expected_type = type_wchar_t_ptr;
466 expected_qual = TYPE_QUALIFIER_CONST;
467 allowed_flags = FMT_FLAG_MINUS;
472 case FMT_MOD_NONE: expected_type = type_char_ptr; break;
473 case FMT_MOD_l: expected_type = type_wchar_t_ptr; break;
474 case FMT_MOD_w: expected_type = type_wchar_t_ptr; break;
477 warn_invalid_length_modifier(pos, fmt_mod, fmt);
480 expected_qual = TYPE_QUALIFIER_CONST;
481 allowed_flags = FMT_FLAG_MINUS;
485 if (fmt_mod != FMT_MOD_NONE) {
486 warn_invalid_length_modifier(pos, fmt_mod, fmt);
489 expected_type = type_void_ptr;
490 allowed_flags = FMT_FLAG_NONE;
495 case FMT_MOD_NONE: expected_type = type_int_ptr; break;
496 case FMT_MOD_hh: expected_type = type_signed_char_ptr; break;
497 case FMT_MOD_h: expected_type = type_short_ptr; break;
498 case FMT_MOD_l: expected_type = type_long_ptr; break;
499 case FMT_MOD_ll: expected_type = type_long_long_ptr; break;
500 case FMT_MOD_j: expected_type = type_intmax_t_ptr; break;
501 case FMT_MOD_z: expected_type = type_ssize_t_ptr; break;
502 case FMT_MOD_t: expected_type = type_ptrdiff_t_ptr; break;
505 warn_invalid_length_modifier(pos, fmt_mod, fmt);
508 allowed_flags = FMT_FLAG_NONE;
512 warningf(pos, "encountered unknown conversion specifier '%%%C'", (wint_t)fmt);
516 format_flags_t wrong_flags = fmt_flags & ~allowed_flags;
517 if (wrong_flags != 0) {
520 if (wrong_flags & FMT_FLAG_HASH) wrong[idx++] = '#';
521 if (wrong_flags & FMT_FLAG_ZERO) wrong[idx++] = '0';
522 if (wrong_flags & FMT_FLAG_MINUS) wrong[idx++] = '-';
523 if (wrong_flags & FMT_FLAG_SPACE) wrong[idx++] = ' ';
524 if (wrong_flags & FMT_FLAG_PLUS) wrong[idx++] = '+';
525 if (wrong_flags & FMT_FLAG_TICK) wrong[idx++] = '\'';
528 warningf(pos, "invalid format flags \"%s\" in conversion specification %%%c", wrong, fmt);
532 warningf(pos, "too few arguments for format string");
536 { /* create a scope here to prevent warning about the jump to next_arg */
537 type_t *const arg_type = arg->expression->base.type;
538 type_t *const arg_skip = skip_typeref(arg_type);
539 type_t *const expected_type_skip = skip_typeref(expected_type);
540 if (is_type_pointer(expected_type_skip)) {
541 if (is_type_pointer(arg_skip)) {
542 type_t *const exp_to = skip_typeref(expected_type_skip->pointer.points_to);
543 type_t *const arg_to = skip_typeref(arg_skip->pointer.points_to);
544 if ((arg_to->base.qualifiers & ~expected_qual) == 0 &&
545 get_unqualified_type(arg_to) == exp_to) {
550 if (get_unqualified_type(arg_skip) == expected_type_skip) {
554 if (is_type_valid(arg_skip)) {
556 "argument type '%T' does not match conversion specifier '%%%s%c'",
557 arg_type, get_length_modifier_name(fmt_mod), (char)fmt);
563 if (!atend(&vchar)) {
564 warningf(pos, "format string contains NUL");
567 unsigned num_args = num_fmt;
568 while (arg != NULL) {
572 warningf(pos, "%u argument%s but only %u format string%s",
573 num_args, num_args != 1 ? "s" : "",
574 num_fmt, num_fmt != 1 ? "s" : "");
578 static const struct {
580 format_kind_t fmt_kind;
583 } builtin_table[] = {
584 { "printf", FORMAT_PRINTF, 0, 1 },
585 { "wprintf", FORMAT_PRINTF, 0, 1 },
586 { "sprintf", FORMAT_PRINTF, 1, 2 },
587 { "swprintf", FORMAT_PRINTF, 1, 2 },
588 { "snprintf", FORMAT_PRINTF, 2, 3 },
589 { "snwprintf", FORMAT_PRINTF, 2, 3 },
590 { "fprintf", FORMAT_PRINTF, 1, 2 },
591 { "fwprintf", FORMAT_PRINTF, 1, 2 },
592 { "snwprintf", FORMAT_PRINTF, 2, 3 },
593 { "snwprintf", FORMAT_PRINTF, 2, 3 },
595 { "scanf", FORMAT_SCANF, 0, 1 },
596 { "wscanf", FORMAT_SCANF, 0, 1 },
597 { "sscanf", FORMAT_SCANF, 1, 2 },
598 { "swscanf", FORMAT_SCANF, 1, 2 },
599 { "fscanf", FORMAT_SCANF, 1, 2 },
600 { "fwscanf", FORMAT_SCANF, 1, 2 },
602 { "strftime", FORMAT_STRFTIME, 3, 4 },
603 { "wcstrftime", FORMAT_STRFTIME, 3, 4 },
605 { "strfmon", FORMAT_STRFMON, 3, 4 },
608 { "_snprintf", FORMAT_PRINTF, 2, 3 },
609 { "_snwprintf", FORMAT_PRINTF, 2, 3 },
610 { "_scrintf", FORMAT_PRINTF, 0, 1 },
611 { "_scwprintf", FORMAT_PRINTF, 0, 1 },
612 { "printf_s", FORMAT_PRINTF, 0, 1 },
613 { "wprintf_s", FORMAT_PRINTF, 0, 1 },
614 { "sprintf_s", FORMAT_PRINTF, 3, 4 },
615 { "swprintf_s", FORMAT_PRINTF, 3, 4 },
616 { "fprintf_s", FORMAT_PRINTF, 1, 2 },
617 { "fwprintf_s", FORMAT_PRINTF, 1, 2 },
618 { "_sprintf_l", FORMAT_PRINTF, 1, 3 },
619 { "_swprintf_l", FORMAT_PRINTF, 1, 3 },
620 { "_printf_l", FORMAT_PRINTF, 0, 2 },
621 { "_wprintf_l", FORMAT_PRINTF, 0, 2 },
622 { "_fprintf_l", FORMAT_PRINTF, 1, 3 },
623 { "_fwprintf_l", FORMAT_PRINTF, 1, 3 },
624 { "_printf_s_l", FORMAT_PRINTF, 0, 2 },
625 { "_wprintf_s_l", FORMAT_PRINTF, 0, 2 },
626 { "_sprintf_s_l", FORMAT_PRINTF, 3, 5 },
627 { "_swprintf_s_l", FORMAT_PRINTF, 3, 5 },
628 { "_fprintf_s_l", FORMAT_PRINTF, 1, 3 },
629 { "_fwprintf_s_l", FORMAT_PRINTF, 1, 3 },
632 void check_format(const call_expression_t *const call)
637 const expression_t *const func_expr = call->function;
638 if (func_expr->kind != EXPR_REFERENCE)
641 const declaration_t *const decl = func_expr->reference.declaration;
642 const call_argument_t * arg = call->arguments;
645 /* the declaration has a GNU format attribute, check it */
648 * For some functions we always check the format, even if it was not specified.
649 * This allows to check format even in MS mode or without header included.
651 const char *const name = decl->symbol->string;
652 for(size_t i = 0; i < sizeof(builtin_table) / sizeof(builtin_table[0]); ++i) {
653 if(strcmp(name, builtin_table[i].name) == 0) {
654 if(builtin_table[i].fmt_kind == FORMAT_PRINTF) {
655 check_format_arguments(arg,
656 builtin_table[i].fmt_idx,
657 builtin_table[i].arg_idx);