2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 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
24 #include "adt/strutil.h"
26 #include "format_check.h"
30 #include "diagnostic.h"
35 #include "lang_features.h"
37 typedef enum format_flag_t {
39 FMT_FLAG_HASH = 1U << 0,
40 FMT_FLAG_ZERO = 1U << 1,
41 FMT_FLAG_MINUS = 1U << 2,
42 FMT_FLAG_SPACE = 1U << 3,
43 FMT_FLAG_PLUS = 1U << 4,
44 FMT_FLAG_TICK = 1U << 5
47 typedef unsigned format_flags_t;
49 typedef enum format_length_modifier_t {
60 /* only in microsoft mode */
65 } format_length_modifier_t;
67 typedef struct format_spec_t {
68 const char *name; /**< name of the function */
69 format_kind_t fmt_kind; /**< kind */
70 unsigned fmt_idx; /**< index of the format string */
71 unsigned arg_idx; /**< index of the first argument */
74 static const char* get_length_modifier_name(const format_length_modifier_t mod)
76 static const char* const names[] = {
87 /* only in microsoft mode */
90 [FMT_MOD_I32] = "I32",
93 assert((size_t)mod < lengthof(names));
97 static void warn_invalid_length_modifier(const source_position_t *pos,
98 const format_length_modifier_t mod,
99 const utf32 conversion)
101 char const *const lmod = get_length_modifier_name(mod);
102 warningf(WARN_FORMAT, pos, "invalid length modifier '%s' for conversion specifier '%%%c'", lmod, conversion);
106 * Check printf-style format. Returns number of expected arguments.
108 static int internal_check_printf_format(const expression_t *fmt_expr,
109 const call_argument_t *arg,
110 const format_spec_t *spec)
112 while (fmt_expr->kind == EXPR_UNARY_CAST) {
113 fmt_expr = fmt_expr->unary.value;
117 * gettext results in expressions like (X ? "format_string" : Y)
118 * we assume the left part is the format string
120 if (fmt_expr->kind == EXPR_CONDITIONAL) {
121 conditional_expression_t const *const c = &fmt_expr->conditional;
122 expression_t const * t = c->true_expression;
125 int const nt = internal_check_printf_format(t, arg, spec);
126 int const nf = internal_check_printf_format(c->false_expression, arg, spec);
127 return nt > nf ? nt : nf;
130 if (fmt_expr->kind != EXPR_STRING_LITERAL)
133 const char *string = fmt_expr->string_literal.value.begin;
134 size_t size = fmt_expr->string_literal.value.size;
135 const char *c = string;
137 const source_position_t *pos = &fmt_expr->base.source_position;
138 unsigned num_fmt = 0;
139 unsigned num_args = 0;
141 for (fmt = *c; fmt != '\0'; fmt = *(++c)) {
152 format_flags_t fmt_flags = FMT_FLAG_NONE;
155 fmt_flags |= FMT_FLAG_ZERO;
158 /* argument selector or minimum field width */
162 } while (isdigit(fmt));
164 /* digit string was ... */
166 /* ... argument selector */
167 fmt_flags = FMT_FLAG_NONE; /* reset possibly set 0-flag */
171 /* ... minimum field width */
177 case '#': flag = FMT_FLAG_HASH; break;
178 case '0': flag = FMT_FLAG_ZERO; break;
179 case '-': flag = FMT_FLAG_MINUS; break;
180 case '\'': flag = FMT_FLAG_TICK; break;
183 if (fmt_flags & FMT_FLAG_PLUS) {
184 warningf(WARN_FORMAT, pos, "' ' is overridden by prior '+' in conversion specification %u", num_fmt);
186 flag = FMT_FLAG_SPACE;
190 if (fmt_flags & FMT_FLAG_SPACE) {
191 warningf(WARN_FORMAT, pos, "'+' overrides prior ' ' in conversion specification %u", num_fmt);
193 flag = FMT_FLAG_PLUS;
196 default: goto break_fmt_flags;
198 if (fmt_flags & flag) {
199 warningf(WARN_FORMAT, pos, "repeated flag '%c' in conversion specification %u", (char)fmt, num_fmt);
206 /* minimum field width */
211 warningf(WARN_FORMAT, pos, "missing argument for '*' field width in conversion specification %u", num_fmt);
214 const type_t *const arg_type = arg->expression->base.type;
215 if (arg_type != type_int) {
216 warningf(WARN_FORMAT, pos, "argument for '*' field width in conversion specification %u is not an 'int', but an '%T'", num_fmt, arg_type);
220 while (isdigit(fmt)) {
228 if (fmt_flags & FMT_FLAG_ZERO) {
229 warningf(WARN_FORMAT, pos, "'0' flag ignored with precision in conversion specification %u", num_fmt);
237 warningf(WARN_FORMAT, pos, "missing argument for '*' precision in conversion specification %u", num_fmt);
240 const type_t *const arg_type = arg->expression->base.type;
241 if (arg_type != type_int) {
242 warningf(WARN_FORMAT, pos, "argument for '*' precision in conversion specification %u is not an 'int', but an '%T'", num_fmt, arg_type);
246 /* digit string may be omitted */
247 while (isdigit(fmt)) {
253 /* length modifier */
254 format_length_modifier_t fmt_mod;
260 fmt_mod = FMT_MOD_hh;
270 fmt_mod = FMT_MOD_ll;
276 case 'L': fmt = *(++c); fmt_mod = FMT_MOD_L; break;
277 case 'j': fmt = *(++c); fmt_mod = FMT_MOD_j; break;
278 case 't': fmt = *(++c); fmt_mod = FMT_MOD_t; break;
279 case 'z': fmt = *(++c); fmt_mod = FMT_MOD_z; break;
280 case 'q': fmt = *(++c); fmt_mod = FMT_MOD_q; break;
284 fmt = *(++c); fmt_mod = FMT_MOD_w;
286 fmt_mod = FMT_MOD_NONE;
291 fmt = *(++c); fmt_mod = FMT_MOD_I;
296 fmt_mod = FMT_MOD_I32;
301 } else if (fmt == '6') {
305 fmt_mod = FMT_MOD_I64;
312 fmt_mod = FMT_MOD_NONE;
316 fmt_mod = FMT_MOD_NONE;
321 warningf(WARN_FORMAT, pos, "dangling %% in format string");
325 type_t *expected_type;
326 type_qualifiers_t expected_qual = TYPE_QUALIFIER_NONE;
327 format_flags_t allowed_flags;
332 case FMT_MOD_NONE: expected_type = type_int; break;
333 case FMT_MOD_hh: expected_type = type_signed_char; break;
334 case FMT_MOD_h: expected_type = type_short; break;
335 case FMT_MOD_l: expected_type = type_long; break;
336 case FMT_MOD_ll: expected_type = type_long_long; break;
337 case FMT_MOD_j: expected_type = type_intmax_t; break;
338 case FMT_MOD_z: expected_type = type_ssize_t; break;
339 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
340 case FMT_MOD_I: expected_type = type_ptrdiff_t; break;
341 case FMT_MOD_I32: expected_type = type_int32; break;
342 case FMT_MOD_I64: expected_type = type_int64; break;
345 warn_invalid_length_modifier(pos, fmt_mod, fmt);
348 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
354 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
355 goto eval_fmt_mod_unsigned;
358 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
359 eval_fmt_mod_unsigned:
361 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
362 case FMT_MOD_hh: expected_type = type_unsigned_char; break;
363 case FMT_MOD_h: expected_type = type_unsigned_short; break;
364 case FMT_MOD_l: expected_type = type_unsigned_long; break;
365 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
366 case FMT_MOD_j: expected_type = type_uintmax_t; break;
367 case FMT_MOD_z: expected_type = type_size_t; break;
368 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
369 case FMT_MOD_I: expected_type = type_size_t; break;
370 case FMT_MOD_I32: expected_type = type_unsigned_int32; break;
371 case FMT_MOD_I64: expected_type = type_unsigned_int64; break;
374 warn_invalid_length_modifier(pos, fmt_mod, fmt);
388 case FMT_MOD_l: /* l modifier is ignored */
389 case FMT_MOD_NONE: expected_type = type_double; break;
390 case FMT_MOD_L: expected_type = type_long_double; break;
393 warn_invalid_length_modifier(pos, fmt_mod, fmt);
396 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
400 if (fmt_mod != FMT_MOD_NONE) {
401 warn_invalid_length_modifier(pos, fmt_mod, fmt);
404 expected_type = type_wchar_t;
405 allowed_flags = FMT_FLAG_NONE;
409 expected_type = type_int;
411 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
412 case FMT_MOD_l: expected_type = type_wint_t; break;
413 case FMT_MOD_w: expected_type = type_wchar_t; break;
416 warn_invalid_length_modifier(pos, fmt_mod, fmt);
419 allowed_flags = FMT_FLAG_NONE;
423 if (fmt_mod != FMT_MOD_NONE) {
424 warn_invalid_length_modifier(pos, fmt_mod, fmt);
427 expected_type = type_wchar_t_ptr;
428 expected_qual = TYPE_QUALIFIER_CONST;
429 allowed_flags = FMT_FLAG_MINUS;
434 case FMT_MOD_NONE: expected_type = type_char_ptr; break;
435 case FMT_MOD_l: expected_type = type_wchar_t_ptr; break;
436 case FMT_MOD_w: expected_type = type_wchar_t_ptr; break;
439 warn_invalid_length_modifier(pos, fmt_mod, fmt);
442 expected_qual = TYPE_QUALIFIER_CONST;
443 allowed_flags = FMT_FLAG_MINUS;
447 if (fmt_mod != FMT_MOD_NONE) {
448 warn_invalid_length_modifier(pos, fmt_mod, fmt);
451 expected_type = type_void_ptr;
452 allowed_flags = FMT_FLAG_NONE;
457 case FMT_MOD_NONE: expected_type = type_int_ptr; break;
458 case FMT_MOD_hh: expected_type = type_signed_char_ptr; break;
459 case FMT_MOD_h: expected_type = type_short_ptr; break;
460 case FMT_MOD_l: expected_type = type_long_ptr; break;
461 case FMT_MOD_ll: expected_type = type_long_long_ptr; break;
462 case FMT_MOD_j: expected_type = type_intmax_t_ptr; break;
463 case FMT_MOD_z: expected_type = type_ssize_t_ptr; break;
464 case FMT_MOD_t: expected_type = type_ptrdiff_t_ptr; break;
467 warn_invalid_length_modifier(pos, fmt_mod, fmt);
470 allowed_flags = FMT_FLAG_NONE;
474 warningf(WARN_FORMAT, pos, "encountered unknown conversion specifier '%%%c' at position %u", fmt, num_fmt);
481 format_flags_t wrong_flags = fmt_flags & ~allowed_flags;
482 if (wrong_flags != 0) {
485 if (wrong_flags & FMT_FLAG_HASH) *p++ = '#';
486 if (wrong_flags & FMT_FLAG_ZERO) *p++ = '0';
487 if (wrong_flags & FMT_FLAG_MINUS) *p++ = '-';
488 if (wrong_flags & FMT_FLAG_SPACE) *p++ = ' ';
489 if (wrong_flags & FMT_FLAG_PLUS) *p++ = '+';
490 if (wrong_flags & FMT_FLAG_TICK) *p++ = '\'';
493 warningf(WARN_FORMAT, pos, "invalid format flags \"%s\" in conversion specification %%%c at position %u", wrong, fmt, num_fmt);
498 warningf(WARN_FORMAT, pos, "too few arguments for format string");
502 { /* create a scope here to prevent warning about the jump to next_arg */
503 type_t *const arg_type = arg->expression->base.type;
504 type_t *const arg_skip = skip_typeref(arg_type);
505 type_t *const expected_type_skip = skip_typeref(expected_type);
508 /* allow any pointer type for %p, not just void */
509 if (is_type_pointer(arg_skip))
513 if (is_type_pointer(expected_type_skip)) {
514 if (is_type_pointer(arg_skip)) {
515 type_t *const exp_to = skip_typeref(expected_type_skip->pointer.points_to);
516 type_t *const arg_to = skip_typeref(arg_skip->pointer.points_to);
517 if ((arg_to->base.qualifiers & ~expected_qual) == 0 &&
518 get_unqualified_type(arg_to) == exp_to) {
522 } else if (get_unqualified_type(arg_skip) == expected_type_skip) {
524 } else if (arg->expression->kind == EXPR_UNARY_CAST) {
525 expression_t const *const expr = arg->expression->unary.value;
526 type_t *const unprom_type = skip_typeref(expr->base.type);
527 if (get_unqualified_type(unprom_type) == expected_type_skip) {
530 if (expected_type_skip == type_unsigned_int && !is_type_signed(unprom_type)) {
534 if (is_type_valid(arg_skip)) {
535 source_position_t const *const apos = &arg->expression->base.source_position;
536 char const *const mod = get_length_modifier_name(fmt_mod);
537 warningf(WARN_FORMAT, apos, "conversion '%%%s%c' at position %u specifies type '%T' but the argument has type '%T'", mod, (char)fmt, num_fmt, expected_type, arg_type);
544 if (c+1 < string + size) {
545 warningf(WARN_FORMAT, pos, "format string contains '\\0'");
551 * Check printf-style format.
553 static void check_printf_format(call_argument_t const *arg,
554 format_spec_t const *const spec)
556 /* find format arg */
558 for (; idx < spec->fmt_idx; ++idx) {
564 expression_t const *const fmt_expr = arg->expression;
566 /* find the real args */
567 for (; idx < spec->arg_idx && arg != NULL; ++idx)
570 int const num_fmt = internal_check_printf_format(fmt_expr, arg, spec);
575 for (; arg != NULL; arg = arg->next)
577 if (num_args > (size_t)num_fmt) {
578 source_position_t const *const pos = &fmt_expr->base.source_position;
579 warningf(WARN_FORMAT, pos, "%u argument%s but only %u format specifier%s", num_args, num_args != 1 ? "s" : "", num_fmt, num_fmt != 1 ? "s" : "");
584 * Check scanf-style format.
586 static void check_scanf_format(const call_argument_t *arg,
587 const format_spec_t *spec)
589 /* find format arg */
591 for (; idx < spec->fmt_idx; ++idx) {
597 const expression_t *fmt_expr = arg->expression;
598 if (fmt_expr->kind == EXPR_UNARY_CAST) {
599 fmt_expr = fmt_expr->unary.value;
602 if (fmt_expr->kind != EXPR_STRING_LITERAL)
605 const char *string = fmt_expr->string_literal.value.begin;
606 size_t size = fmt_expr->string_literal.value.size;
607 const char *c = string;
609 /* find the real args */
610 for (; idx < spec->arg_idx && arg != NULL; ++idx)
613 const source_position_t *pos = &fmt_expr->base.source_position;
614 unsigned num_fmt = 0;
616 for (fmt = *c; fmt != '\0'; fmt = *(++c)) {
625 bool suppress_assignment = false;
628 suppress_assignment = true;
632 if ('0' <= fmt && fmt <= '9') {
634 width = width * 10 + (fmt - '0');
636 } while ('0' <= fmt && fmt <= '9');
638 warningf(WARN_FORMAT, pos, "field width is zero at format %u", num_fmt);
642 /* look for length modifiers */
643 format_length_modifier_t fmt_mod = FMT_MOD_NONE;
649 fmt_mod = FMT_MOD_hh;
659 fmt_mod = FMT_MOD_ll;
665 case 'L': fmt = *(++c); fmt_mod = FMT_MOD_L; break;
666 case 'j': fmt = *(++c); fmt_mod = FMT_MOD_j; break;
667 case 't': fmt = *(++c); fmt_mod = FMT_MOD_t; break;
668 case 'z': fmt = *(++c); fmt_mod = FMT_MOD_z; break;
684 fmt_mod = FMT_MOD_I32;
689 } else if (fmt == '6') {
693 fmt_mod = FMT_MOD_I64;
704 warningf(WARN_FORMAT, pos, "dangling %% with conversion specififer in format string");
708 type_t *expected_type;
713 case FMT_MOD_NONE: expected_type = type_int; break;
714 case FMT_MOD_hh: expected_type = type_signed_char; break;
715 case FMT_MOD_h: expected_type = type_short; break;
716 case FMT_MOD_l: expected_type = type_long; break;
717 case FMT_MOD_ll: expected_type = type_long_long; break;
718 case FMT_MOD_j: expected_type = type_intmax_t; break;
719 case FMT_MOD_z: expected_type = type_ssize_t; break;
720 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
721 case FMT_MOD_I: expected_type = type_ptrdiff_t; break;
722 case FMT_MOD_I32: expected_type = type_int32; break;
723 case FMT_MOD_I64: expected_type = type_int64; break;
726 warn_invalid_length_modifier(pos, fmt_mod, fmt);
736 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
737 case FMT_MOD_hh: expected_type = type_unsigned_char; break;
738 case FMT_MOD_h: expected_type = type_unsigned_short; break;
739 case FMT_MOD_l: expected_type = type_unsigned_long; break;
740 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
741 case FMT_MOD_j: expected_type = type_uintmax_t; break;
742 case FMT_MOD_z: expected_type = type_size_t; break;
743 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
744 case FMT_MOD_I: expected_type = type_size_t; break;
745 case FMT_MOD_I32: expected_type = type_unsigned_int32; break;
746 case FMT_MOD_I64: expected_type = type_unsigned_int64; break;
749 warn_invalid_length_modifier(pos, fmt_mod, fmt);
763 case FMT_MOD_l: expected_type = type_double; break;
764 case FMT_MOD_NONE: expected_type = type_float; break;
765 case FMT_MOD_L: expected_type = type_long_double; break;
768 warn_invalid_length_modifier(pos, fmt_mod, fmt);
774 if (fmt_mod != FMT_MOD_NONE) {
775 warn_invalid_length_modifier(pos, fmt_mod, fmt);
778 expected_type = type_wchar_t;
783 case FMT_MOD_NONE: expected_type = type_char; break;
784 case FMT_MOD_l: expected_type = type_wchar_t; break;
785 case FMT_MOD_w: expected_type = type_wchar_t; break;
788 warn_invalid_length_modifier(pos, fmt_mod, fmt);
795 if (!suppress_assignment && arg != NULL) {
796 type_t *const type = skip_typeref(revert_automatic_type_conversion(arg->expression));
797 if (is_type_array(type) &&
798 type->array.size_constant &&
799 width > type->array.size) {
800 warningf(WARN_FORMAT, pos, "target buffer '%T' is too small for %u characters at format %u", type, width, num_fmt);
807 if (fmt_mod != FMT_MOD_NONE) {
808 warn_invalid_length_modifier(pos, fmt_mod, fmt);
811 expected_type = type_wchar_t;
817 case FMT_MOD_NONE: expected_type = type_char; break;
818 case FMT_MOD_l: expected_type = type_wchar_t; break;
819 case FMT_MOD_w: expected_type = type_wchar_t; break;
822 warn_invalid_length_modifier(pos, fmt_mod, fmt);
826 if (!suppress_assignment &&
829 type_t *const type = skip_typeref(revert_automatic_type_conversion(arg->expression));
830 if (is_type_array(type) &&
831 type->array.size_constant &&
832 width >= type->array.size) {
833 warningf(WARN_FORMAT, pos, "target buffer '%T' is too small for %u characters and \\0 at format %u", type, width, num_fmt);
840 if (fmt_mod != FMT_MOD_NONE) {
841 warn_invalid_length_modifier(pos, fmt_mod, fmt);
844 expected_type = type_void;
848 if (suppress_assignment) {
849 warningf(WARN_FORMAT, pos, "conversion '%n' cannot be suppressed with '*' at format %u", num_fmt);
853 case FMT_MOD_NONE: expected_type = type_int; break;
854 case FMT_MOD_hh: expected_type = type_signed_char; break;
855 case FMT_MOD_h: expected_type = type_short; break;
856 case FMT_MOD_l: expected_type = type_long; break;
857 case FMT_MOD_ll: expected_type = type_long_long; break;
858 case FMT_MOD_j: expected_type = type_intmax_t; break;
859 case FMT_MOD_z: expected_type = type_ssize_t; break;
860 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
863 warn_invalid_length_modifier(pos, fmt_mod, fmt);
870 warningf(WARN_FORMAT, pos, "encountered unknown conversion specifier '%%%c' at format %u", fmt, num_fmt);
871 if (suppress_assignment)
878 if (suppress_assignment)
883 warningf(WARN_FORMAT, pos, "too few arguments for format string");
887 { /* create a scope here to prevent warning about the jump to next_arg */
888 type_t *const arg_type = arg->expression->base.type;
889 type_t *const arg_skip = skip_typeref(arg_type);
890 type_t *const expected_type_skip = skip_typeref(expected_type);
892 if (! is_type_pointer(arg_skip))
894 type_t *const ptr_skip = skip_typeref(arg_skip->pointer.points_to);
897 /* allow any pointer type for %p, not just void */
898 if (is_type_pointer(ptr_skip))
902 /* do NOT allow const or restrict, all other should be ok */
903 if (ptr_skip->base.qualifiers & (TYPE_QUALIFIER_CONST | TYPE_QUALIFIER_VOLATILE))
905 type_t *const unqual_ptr = get_unqualified_type(ptr_skip);
906 if (unqual_ptr == expected_type_skip) {
908 } else if (expected_type_skip == type_char) {
909 /* char matches with unsigned char AND signed char */
910 if (unqual_ptr == type_signed_char || unqual_ptr == type_unsigned_char)
914 if (is_type_valid(arg_skip)) {
915 source_position_t const *const apos = &arg->expression->base.source_position;
916 char const *const mod = get_length_modifier_name(fmt_mod);
917 warningf(WARN_FORMAT, apos, "conversion '%%%s%c' at position %u specifies type '%T*' but the argument has type '%T'", mod, (char)fmt, num_fmt, expected_type, arg_type);
924 if (c+1 < string + size) {
925 warningf(WARN_FORMAT, pos, "format string contains '\\0'");
928 unsigned num_args = num_fmt;
929 while (arg != NULL) {
933 warningf(WARN_FORMAT, pos, "%u argument%s but only %u format specifier%s", num_args, num_args != 1 ? "s" : "", num_fmt, num_fmt != 1 ? "s" : "");
937 static const format_spec_t builtin_table[] = {
938 { "printf", FORMAT_PRINTF, 0, 1 },
939 { "wprintf", FORMAT_PRINTF, 0, 1 },
940 { "sprintf", FORMAT_PRINTF, 1, 2 },
941 { "swprintf", FORMAT_PRINTF, 1, 2 },
942 { "snprintf", FORMAT_PRINTF, 2, 3 },
943 { "snwprintf", FORMAT_PRINTF, 2, 3 },
944 { "fprintf", FORMAT_PRINTF, 1, 2 },
945 { "fwprintf", FORMAT_PRINTF, 1, 2 },
946 { "snwprintf", FORMAT_PRINTF, 2, 3 },
947 { "snwprintf", FORMAT_PRINTF, 2, 3 },
949 { "scanf", FORMAT_SCANF, 0, 1 },
950 { "wscanf", FORMAT_SCANF, 0, 1 },
951 { "sscanf", FORMAT_SCANF, 1, 2 },
952 { "swscanf", FORMAT_SCANF, 1, 2 },
953 { "fscanf", FORMAT_SCANF, 1, 2 },
954 { "fwscanf", FORMAT_SCANF, 1, 2 },
956 { "strftime", FORMAT_STRFTIME, 3, 4 },
957 { "wcstrftime", FORMAT_STRFTIME, 3, 4 },
959 { "strfmon", FORMAT_STRFMON, 3, 4 },
962 { "_snprintf", FORMAT_PRINTF, 2, 3 },
963 { "_snwprintf", FORMAT_PRINTF, 2, 3 },
964 { "_scrintf", FORMAT_PRINTF, 0, 1 },
965 { "_scwprintf", FORMAT_PRINTF, 0, 1 },
966 { "printf_s", FORMAT_PRINTF, 0, 1 },
967 { "wprintf_s", FORMAT_PRINTF, 0, 1 },
968 { "sprintf_s", FORMAT_PRINTF, 3, 4 },
969 { "swprintf_s", FORMAT_PRINTF, 3, 4 },
970 { "fprintf_s", FORMAT_PRINTF, 1, 2 },
971 { "fwprintf_s", FORMAT_PRINTF, 1, 2 },
972 { "_sprintf_l", FORMAT_PRINTF, 1, 3 },
973 { "_swprintf_l", FORMAT_PRINTF, 1, 3 },
974 { "_printf_l", FORMAT_PRINTF, 0, 2 },
975 { "_wprintf_l", FORMAT_PRINTF, 0, 2 },
976 { "_fprintf_l", FORMAT_PRINTF, 1, 3 },
977 { "_fwprintf_l", FORMAT_PRINTF, 1, 3 },
978 { "_printf_s_l", FORMAT_PRINTF, 0, 2 },
979 { "_wprintf_s_l", FORMAT_PRINTF, 0, 2 },
980 { "_sprintf_s_l", FORMAT_PRINTF, 3, 5 },
981 { "_swprintf_s_l", FORMAT_PRINTF, 3, 5 },
982 { "_fprintf_s_l", FORMAT_PRINTF, 1, 3 },
983 { "_fwprintf_s_l", FORMAT_PRINTF, 1, 3 },
986 void check_format(const call_expression_t *const call)
988 if (!is_warn_on(WARN_FORMAT))
991 const expression_t *const func_expr = call->function;
992 if (func_expr->kind != EXPR_REFERENCE)
995 const entity_t *const entity = func_expr->reference.entity;
996 const call_argument_t * arg = call->arguments;
999 * For some functions we always check the format, even if it was not
1000 * specified. This allows to check format even in MS mode or without
1003 const char *const name = entity->base.symbol->string;
1004 for (size_t i = 0; i < lengthof(builtin_table); ++i) {
1005 if (streq(name, builtin_table[i].name)) {
1006 switch (builtin_table[i].fmt_kind) {
1008 check_printf_format(arg, &builtin_table[i]);
1011 check_scanf_format(arg, &builtin_table[i]);
1013 case FORMAT_STRFTIME:
1014 case FORMAT_STRFMON:
1015 /* TODO: implement other cases */