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
25 #include "format_check.h"
29 #include "diagnostic.h"
33 #include "lang_features.h"
35 typedef enum format_flag_t {
37 FMT_FLAG_HASH = 1U << 0,
38 FMT_FLAG_ZERO = 1U << 1,
39 FMT_FLAG_MINUS = 1U << 2,
40 FMT_FLAG_SPACE = 1U << 3,
41 FMT_FLAG_PLUS = 1U << 4,
42 FMT_FLAG_TICK = 1U << 5
45 typedef unsigned format_flags_t;
47 typedef enum format_length_modifier_t {
58 /* only in microsoft mode */
63 } format_length_modifier_t;
65 typedef struct format_spec_t {
66 const char *name; /**< name of the function */
67 format_kind_t fmt_kind; /**< kind */
68 unsigned fmt_idx; /**< index of the format string */
69 unsigned arg_idx; /**< index of the first argument */
72 static const char* get_length_modifier_name(const format_length_modifier_t mod)
74 static const char* const names[] = {
85 /* only in microsoft mode */
88 [FMT_MOD_I32] = "I32",
91 assert((size_t)mod < lengthof(names));
95 static void warn_invalid_length_modifier(const source_position_t *pos,
96 const format_length_modifier_t mod,
97 const utf32 conversion)
99 char const *const lmod = get_length_modifier_name(mod);
100 warningf(WARN_FORMAT, pos, "invalid length modifier '%s' for conversion specifier '%%%c'", lmod, conversion);
104 * Check printf-style format. Returns number of expected arguments.
106 static int internal_check_printf_format(const expression_t *fmt_expr,
107 const call_argument_t *arg,
108 const format_spec_t *spec)
110 while (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
111 fmt_expr = fmt_expr->unary.value;
115 * gettext results in expressions like (X ? "format_string" : Y)
116 * we assume the left part is the format string
118 if (fmt_expr->kind == EXPR_CONDITIONAL) {
119 conditional_expression_t const *const c = &fmt_expr->conditional;
120 expression_t const * t = c->true_expression;
123 int const nt = internal_check_printf_format(t, arg, spec);
124 int const nf = internal_check_printf_format(c->false_expression, arg, spec);
125 return nt > nf ? nt : nf;
128 if (fmt_expr->kind != EXPR_STRING_LITERAL
129 && fmt_expr->kind != EXPR_WIDE_STRING_LITERAL)
132 const char *string = fmt_expr->literal.value.begin;
133 size_t size = fmt_expr->literal.value.size;
134 const char *c = string;
136 const source_position_t *pos = &fmt_expr->base.source_position;
137 unsigned num_fmt = 0;
138 unsigned num_args = 0;
140 for (fmt = *c; fmt != '\0'; fmt = *(++c)) {
146 warningf(WARN_FORMAT, pos, "dangling %% in format string");
155 format_flags_t fmt_flags = FMT_FLAG_NONE;
158 fmt_flags |= FMT_FLAG_ZERO;
161 /* argument selector or minimum field width */
165 } while (isdigit(fmt));
167 /* digit string was ... */
169 /* ... argument selector */
170 fmt_flags = FMT_FLAG_NONE; /* reset possibly set 0-flag */
174 /* ... minimum field width */
180 case '#': flag = FMT_FLAG_HASH; break;
181 case '0': flag = FMT_FLAG_ZERO; break;
182 case '-': flag = FMT_FLAG_MINUS; break;
183 case '\'': flag = FMT_FLAG_TICK; break;
186 if (fmt_flags & FMT_FLAG_PLUS) {
187 warningf(WARN_FORMAT, pos, "' ' is overridden by prior '+' in conversion specification %u", num_fmt);
189 flag = FMT_FLAG_SPACE;
193 if (fmt_flags & FMT_FLAG_SPACE) {
194 warningf(WARN_FORMAT, pos, "'+' overrides prior ' ' in conversion specification %u", num_fmt);
196 flag = FMT_FLAG_PLUS;
199 default: goto break_fmt_flags;
201 if (fmt_flags & flag) {
202 warningf(WARN_FORMAT, pos, "repeated flag '%c' in conversion specification %u", (char)fmt, num_fmt);
209 /* minimum field width */
214 warningf(WARN_FORMAT, pos, "missing argument for '*' field width in conversion specification %u", num_fmt);
217 const type_t *const arg_type = arg->expression->base.type;
218 if (arg_type != type_int) {
219 warningf(WARN_FORMAT, pos, "argument for '*' field width in conversion specification %u is not an 'int', but an '%T'", num_fmt, arg_type);
223 while (isdigit(fmt)) {
231 if (fmt_flags & FMT_FLAG_ZERO) {
232 warningf(WARN_FORMAT, pos, "'0' flag ignored with precision in conversion specification %u", num_fmt);
240 warningf(WARN_FORMAT, pos, "missing argument for '*' precision in conversion specification %u", num_fmt);
243 const type_t *const arg_type = arg->expression->base.type;
244 if (arg_type != type_int) {
245 warningf(WARN_FORMAT, pos, "argument for '*' precision in conversion specification %u is not an 'int', but an '%T'", num_fmt, arg_type);
249 /* digit string may be omitted */
250 while (isdigit(fmt)) {
256 /* length modifier */
257 format_length_modifier_t fmt_mod;
263 fmt_mod = FMT_MOD_hh;
273 fmt_mod = FMT_MOD_ll;
279 case 'L': fmt = *(++c); fmt_mod = FMT_MOD_L; break;
280 case 'j': fmt = *(++c); fmt_mod = FMT_MOD_j; break;
281 case 't': fmt = *(++c); fmt_mod = FMT_MOD_t; break;
282 case 'z': fmt = *(++c); fmt_mod = FMT_MOD_z; break;
283 case 'q': fmt = *(++c); fmt_mod = FMT_MOD_q; break;
287 fmt = *(++c); fmt_mod = FMT_MOD_w;
289 fmt_mod = FMT_MOD_NONE;
294 fmt = *(++c); fmt_mod = FMT_MOD_I;
299 fmt_mod = FMT_MOD_I32;
304 } else if (fmt == '6') {
308 fmt_mod = FMT_MOD_I64;
315 fmt_mod = FMT_MOD_NONE;
319 fmt_mod = FMT_MOD_NONE;
324 type_t *expected_type;
325 type_qualifiers_t expected_qual = TYPE_QUALIFIER_NONE;
326 format_flags_t allowed_flags;
331 case FMT_MOD_NONE: expected_type = type_int; break;
332 case FMT_MOD_hh: expected_type = type_signed_char; break;
333 case FMT_MOD_h: expected_type = type_short; break;
334 case FMT_MOD_l: expected_type = type_long; break;
335 case FMT_MOD_ll: expected_type = type_long_long; break;
336 case FMT_MOD_j: expected_type = type_intmax_t; break;
337 case FMT_MOD_z: expected_type = type_ssize_t; break;
338 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
339 case FMT_MOD_I: expected_type = type_ptrdiff_t; break;
340 case FMT_MOD_I32: expected_type = type_int32; break;
341 case FMT_MOD_I64: expected_type = type_int64; break;
344 warn_invalid_length_modifier(pos, fmt_mod, fmt);
347 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_ZERO;
353 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
354 goto eval_fmt_mod_unsigned;
357 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_ZERO;
358 eval_fmt_mod_unsigned:
360 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
361 case FMT_MOD_hh: expected_type = type_unsigned_char; break;
362 case FMT_MOD_h: expected_type = type_unsigned_short; break;
363 case FMT_MOD_l: expected_type = type_unsigned_long; break;
364 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
365 case FMT_MOD_j: expected_type = type_uintmax_t; break;
366 case FMT_MOD_z: expected_type = type_size_t; break;
367 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
368 case FMT_MOD_I: expected_type = type_size_t; break;
369 case FMT_MOD_I32: expected_type = type_unsigned_int32; break;
370 case FMT_MOD_I64: expected_type = type_unsigned_int64; break;
373 warn_invalid_length_modifier(pos, fmt_mod, fmt);
387 case FMT_MOD_l: /* l modifier is ignored */
388 case FMT_MOD_NONE: expected_type = type_double; break;
389 case FMT_MOD_L: expected_type = type_long_double; break;
392 warn_invalid_length_modifier(pos, fmt_mod, fmt);
395 allowed_flags = FMT_FLAG_MINUS | FMT_FLAG_SPACE | FMT_FLAG_PLUS | FMT_FLAG_HASH | FMT_FLAG_ZERO;
399 if (fmt_mod != FMT_MOD_NONE) {
400 warn_invalid_length_modifier(pos, fmt_mod, fmt);
403 expected_type = type_wchar_t;
404 allowed_flags = FMT_FLAG_NONE;
408 expected_type = type_int;
410 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
411 case FMT_MOD_l: expected_type = type_wint_t; break;
412 case FMT_MOD_w: expected_type = type_wchar_t; break;
415 warn_invalid_length_modifier(pos, fmt_mod, fmt);
418 allowed_flags = FMT_FLAG_NONE;
422 if (fmt_mod != FMT_MOD_NONE) {
423 warn_invalid_length_modifier(pos, fmt_mod, fmt);
426 expected_type = type_wchar_t_ptr;
427 expected_qual = TYPE_QUALIFIER_CONST;
428 allowed_flags = FMT_FLAG_MINUS;
433 case FMT_MOD_NONE: expected_type = type_char_ptr; break;
434 case FMT_MOD_l: expected_type = type_wchar_t_ptr; break;
435 case FMT_MOD_w: expected_type = type_wchar_t_ptr; break;
438 warn_invalid_length_modifier(pos, fmt_mod, fmt);
441 expected_qual = TYPE_QUALIFIER_CONST;
442 allowed_flags = FMT_FLAG_MINUS;
446 if (fmt_mod != FMT_MOD_NONE) {
447 warn_invalid_length_modifier(pos, fmt_mod, fmt);
450 expected_type = type_void_ptr;
451 allowed_flags = FMT_FLAG_NONE;
456 case FMT_MOD_NONE: expected_type = type_int_ptr; break;
457 case FMT_MOD_hh: expected_type = type_signed_char_ptr; break;
458 case FMT_MOD_h: expected_type = type_short_ptr; break;
459 case FMT_MOD_l: expected_type = type_long_ptr; break;
460 case FMT_MOD_ll: expected_type = type_long_long_ptr; break;
461 case FMT_MOD_j: expected_type = type_intmax_t_ptr; break;
462 case FMT_MOD_z: expected_type = type_ssize_t_ptr; break;
463 case FMT_MOD_t: expected_type = type_ptrdiff_t_ptr; break;
466 warn_invalid_length_modifier(pos, fmt_mod, fmt);
469 allowed_flags = FMT_FLAG_NONE;
473 warningf(WARN_FORMAT, pos, "encountered unknown conversion specifier '%%%c' at position %u", fmt, num_fmt);
480 format_flags_t wrong_flags = fmt_flags & ~allowed_flags;
481 if (wrong_flags != 0) {
484 if (wrong_flags & FMT_FLAG_HASH) *p++ = '#';
485 if (wrong_flags & FMT_FLAG_ZERO) *p++ = '0';
486 if (wrong_flags & FMT_FLAG_MINUS) *p++ = '-';
487 if (wrong_flags & FMT_FLAG_SPACE) *p++ = ' ';
488 if (wrong_flags & FMT_FLAG_PLUS) *p++ = '+';
489 if (wrong_flags & FMT_FLAG_TICK) *p++ = '\'';
492 warningf(WARN_FORMAT, pos, "invalid format flags \"%s\" in conversion specification %%%c at position %u", wrong, fmt, num_fmt);
497 warningf(WARN_FORMAT, pos, "too few arguments for format string");
501 { /* create a scope here to prevent warning about the jump to next_arg */
502 type_t *const arg_type = arg->expression->base.type;
503 type_t *const arg_skip = skip_typeref(arg_type);
504 type_t *const expected_type_skip = skip_typeref(expected_type);
507 /* allow any pointer type for %p, not just void */
508 if (is_type_pointer(arg_skip))
512 if (is_type_pointer(expected_type_skip)) {
513 if (is_type_pointer(arg_skip)) {
514 type_t *const exp_to = skip_typeref(expected_type_skip->pointer.points_to);
515 type_t *const arg_to = skip_typeref(arg_skip->pointer.points_to);
516 if ((arg_to->base.qualifiers & ~expected_qual) == 0 &&
517 get_unqualified_type(arg_to) == exp_to) {
521 } else if (get_unqualified_type(arg_skip) == expected_type_skip) {
523 } else if (arg->expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
524 expression_t const *const expr = arg->expression->unary.value;
525 type_t *const unprom_type = skip_typeref(expr->base.type);
526 if (get_unqualified_type(unprom_type) == expected_type_skip) {
529 if (expected_type_skip == type_unsigned_int && !is_type_signed(unprom_type)) {
533 if (is_type_valid(arg_skip)) {
534 source_position_t const *const apos = &arg->expression->base.source_position;
535 char const *const mod = get_length_modifier_name(fmt_mod);
536 warningf(WARN_FORMAT, apos, "argument type '%T' does not match conversion specifier '%%%s%c' at position %u", arg_type, mod, (char)fmt, num_fmt);
543 if (c+1 < string + size) {
544 warningf(WARN_FORMAT, pos, "format string contains '\\0'");
550 * Check printf-style format.
552 static void check_printf_format(call_argument_t const *arg,
553 format_spec_t const *const spec)
555 /* find format arg */
557 for (; idx < spec->fmt_idx; ++idx) {
563 expression_t const *const fmt_expr = arg->expression;
565 /* find the real args */
566 for (; idx < spec->arg_idx && arg != NULL; ++idx)
569 int const num_fmt = internal_check_printf_format(fmt_expr, arg, spec);
574 for (; arg != NULL; arg = arg->next)
576 if (num_args > (size_t)num_fmt) {
577 source_position_t const *const pos = &fmt_expr->base.source_position;
578 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" : "");
583 * Check scanf-style format.
585 static void check_scanf_format(const call_argument_t *arg,
586 const format_spec_t *spec)
588 /* find format arg */
590 for (; idx < spec->fmt_idx; ++idx) {
596 const expression_t *fmt_expr = arg->expression;
597 if (fmt_expr->kind == EXPR_UNARY_CAST_IMPLICIT) {
598 fmt_expr = fmt_expr->unary.value;
601 if (fmt_expr->kind != EXPR_STRING_LITERAL
602 && fmt_expr->kind != EXPR_WIDE_STRING_LITERAL)
605 const char *string = fmt_expr->literal.value.begin;
606 size_t size = fmt_expr->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)) {
621 warningf(WARN_FORMAT, pos, "dangling '%%' in format string");
629 /* look for length modifiers */
630 format_length_modifier_t fmt_mod = FMT_MOD_NONE;
636 fmt_mod = FMT_MOD_hh;
646 fmt_mod = FMT_MOD_ll;
652 case 'L': fmt = *(++c); fmt_mod = FMT_MOD_L; break;
653 case 'j': fmt = *(++c); fmt_mod = FMT_MOD_j; break;
654 case 't': fmt = *(++c); fmt_mod = FMT_MOD_t; break;
655 case 'z': fmt = *(++c); fmt_mod = FMT_MOD_z; break;
671 fmt_mod = FMT_MOD_I32;
676 } else if (fmt == '6') {
680 fmt_mod = FMT_MOD_I64;
691 warningf(WARN_FORMAT, pos, "dangling %% with conversion specififer in format string");
695 type_t *expected_type;
700 case FMT_MOD_NONE: expected_type = type_int; break;
701 case FMT_MOD_hh: expected_type = type_signed_char; break;
702 case FMT_MOD_h: expected_type = type_short; break;
703 case FMT_MOD_l: expected_type = type_long; break;
704 case FMT_MOD_ll: expected_type = type_long_long; break;
705 case FMT_MOD_j: expected_type = type_intmax_t; break;
706 case FMT_MOD_z: expected_type = type_ssize_t; break;
707 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
708 case FMT_MOD_I: expected_type = type_ptrdiff_t; break;
709 case FMT_MOD_I32: expected_type = type_int32; break;
710 case FMT_MOD_I64: expected_type = type_int64; break;
713 warn_invalid_length_modifier(pos, fmt_mod, fmt);
723 case FMT_MOD_NONE: expected_type = type_unsigned_int; break;
724 case FMT_MOD_hh: expected_type = type_unsigned_char; break;
725 case FMT_MOD_h: expected_type = type_unsigned_short; break;
726 case FMT_MOD_l: expected_type = type_unsigned_long; break;
727 case FMT_MOD_ll: expected_type = type_unsigned_long_long; break;
728 case FMT_MOD_j: expected_type = type_uintmax_t; break;
729 case FMT_MOD_z: expected_type = type_size_t; break;
730 case FMT_MOD_t: expected_type = type_uptrdiff_t; break;
731 case FMT_MOD_I: expected_type = type_size_t; break;
732 case FMT_MOD_I32: expected_type = type_unsigned_int32; break;
733 case FMT_MOD_I64: expected_type = type_unsigned_int64; break;
736 warn_invalid_length_modifier(pos, fmt_mod, fmt);
750 case FMT_MOD_l: expected_type = type_double; break;
751 case FMT_MOD_NONE: expected_type = type_float; break;
752 case FMT_MOD_L: expected_type = type_long_double; break;
755 warn_invalid_length_modifier(pos, fmt_mod, fmt);
761 if (fmt_mod != FMT_MOD_NONE) {
762 warn_invalid_length_modifier(pos, fmt_mod, fmt);
765 expected_type = type_wchar_t;
769 expected_type = type_int;
771 case FMT_MOD_NONE: expected_type = type_int; break; /* TODO promoted char */
772 case FMT_MOD_l: expected_type = type_wint_t; break;
773 case FMT_MOD_w: expected_type = type_wchar_t; break;
776 warn_invalid_length_modifier(pos, fmt_mod, fmt);
782 if (fmt_mod != FMT_MOD_NONE) {
783 warn_invalid_length_modifier(pos, fmt_mod, fmt);
786 expected_type = type_wchar_t;
792 case FMT_MOD_NONE: expected_type = type_char; break;
793 case FMT_MOD_l: expected_type = type_wchar_t; break;
794 case FMT_MOD_w: expected_type = type_wchar_t; break;
797 warn_invalid_length_modifier(pos, fmt_mod, fmt);
803 if (fmt_mod != FMT_MOD_NONE) {
804 warn_invalid_length_modifier(pos, fmt_mod, fmt);
807 expected_type = type_void_ptr;
812 case FMT_MOD_NONE: expected_type = type_int; break;
813 case FMT_MOD_hh: expected_type = type_signed_char; break;
814 case FMT_MOD_h: expected_type = type_short; break;
815 case FMT_MOD_l: expected_type = type_long; break;
816 case FMT_MOD_ll: expected_type = type_long_long; break;
817 case FMT_MOD_j: expected_type = type_intmax_t; break;
818 case FMT_MOD_z: expected_type = type_ssize_t; break;
819 case FMT_MOD_t: expected_type = type_ptrdiff_t; break;
822 warn_invalid_length_modifier(pos, fmt_mod, fmt);
828 warningf(WARN_FORMAT, pos, "encountered unknown conversion specifier '%%%c' at format %u", fmt, num_fmt);
836 warningf(WARN_FORMAT, pos, "too few arguments for format string");
840 { /* create a scope here to prevent warning about the jump to next_arg */
841 type_t *const arg_type = arg->expression->base.type;
842 type_t *const arg_skip = skip_typeref(arg_type);
843 type_t *const expected_type_skip = skip_typeref(expected_type);
845 if (! is_type_pointer(arg_skip))
847 type_t *const ptr_skip = skip_typeref(arg_skip->pointer.points_to);
850 /* allow any pointer type for %p, not just void */
851 if (is_type_pointer(ptr_skip))
855 /* do NOT allow const or restrict, all other should be ok */
856 if (ptr_skip->base.qualifiers & (TYPE_QUALIFIER_CONST | TYPE_QUALIFIER_VOLATILE))
858 type_t *const unqual_ptr = get_unqualified_type(ptr_skip);
859 if (unqual_ptr == expected_type_skip) {
861 } else if (expected_type_skip == type_char) {
862 /* char matches with unsigned char AND signed char */
863 if (unqual_ptr == type_signed_char || unqual_ptr == type_unsigned_char)
867 if (is_type_valid(arg_skip)) {
868 source_position_t const *const apos = &arg->expression->base.source_position;
869 char const *const mod = get_length_modifier_name(fmt_mod);
870 warningf(WARN_FORMAT, apos, "argument type '%T' does not match conversion specifier '%%%s%c' at position %u", arg_type, mod, (char)fmt, num_fmt);
877 if (c+1 < string + size) {
878 warningf(WARN_FORMAT, pos, "format string contains '\\0'");
881 unsigned num_args = num_fmt;
882 while (arg != NULL) {
886 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" : "");
890 static const format_spec_t builtin_table[] = {
891 { "printf", FORMAT_PRINTF, 0, 1 },
892 { "wprintf", FORMAT_PRINTF, 0, 1 },
893 { "sprintf", FORMAT_PRINTF, 1, 2 },
894 { "swprintf", FORMAT_PRINTF, 1, 2 },
895 { "snprintf", FORMAT_PRINTF, 2, 3 },
896 { "snwprintf", FORMAT_PRINTF, 2, 3 },
897 { "fprintf", FORMAT_PRINTF, 1, 2 },
898 { "fwprintf", FORMAT_PRINTF, 1, 2 },
899 { "snwprintf", FORMAT_PRINTF, 2, 3 },
900 { "snwprintf", FORMAT_PRINTF, 2, 3 },
902 { "scanf", FORMAT_SCANF, 0, 1 },
903 { "wscanf", FORMAT_SCANF, 0, 1 },
904 { "sscanf", FORMAT_SCANF, 1, 2 },
905 { "swscanf", FORMAT_SCANF, 1, 2 },
906 { "fscanf", FORMAT_SCANF, 1, 2 },
907 { "fwscanf", FORMAT_SCANF, 1, 2 },
909 { "strftime", FORMAT_STRFTIME, 3, 4 },
910 { "wcstrftime", FORMAT_STRFTIME, 3, 4 },
912 { "strfmon", FORMAT_STRFMON, 3, 4 },
915 { "_snprintf", FORMAT_PRINTF, 2, 3 },
916 { "_snwprintf", FORMAT_PRINTF, 2, 3 },
917 { "_scrintf", FORMAT_PRINTF, 0, 1 },
918 { "_scwprintf", FORMAT_PRINTF, 0, 1 },
919 { "printf_s", FORMAT_PRINTF, 0, 1 },
920 { "wprintf_s", FORMAT_PRINTF, 0, 1 },
921 { "sprintf_s", FORMAT_PRINTF, 3, 4 },
922 { "swprintf_s", FORMAT_PRINTF, 3, 4 },
923 { "fprintf_s", FORMAT_PRINTF, 1, 2 },
924 { "fwprintf_s", FORMAT_PRINTF, 1, 2 },
925 { "_sprintf_l", FORMAT_PRINTF, 1, 3 },
926 { "_swprintf_l", FORMAT_PRINTF, 1, 3 },
927 { "_printf_l", FORMAT_PRINTF, 0, 2 },
928 { "_wprintf_l", FORMAT_PRINTF, 0, 2 },
929 { "_fprintf_l", FORMAT_PRINTF, 1, 3 },
930 { "_fwprintf_l", FORMAT_PRINTF, 1, 3 },
931 { "_printf_s_l", FORMAT_PRINTF, 0, 2 },
932 { "_wprintf_s_l", FORMAT_PRINTF, 0, 2 },
933 { "_sprintf_s_l", FORMAT_PRINTF, 3, 5 },
934 { "_swprintf_s_l", FORMAT_PRINTF, 3, 5 },
935 { "_fprintf_s_l", FORMAT_PRINTF, 1, 3 },
936 { "_fwprintf_s_l", FORMAT_PRINTF, 1, 3 },
939 void check_format(const call_expression_t *const call)
941 if (!is_warn_on(WARN_FORMAT))
944 const expression_t *const func_expr = call->function;
945 if (func_expr->kind != EXPR_REFERENCE)
948 const entity_t *const entity = func_expr->reference.entity;
949 const call_argument_t * arg = call->arguments;
952 * For some functions we always check the format, even if it was not
953 * specified. This allows to check format even in MS mode or without
956 const char *const name = entity->base.symbol->string;
957 for (size_t i = 0; i < lengthof(builtin_table); ++i) {
958 if (strcmp(name, builtin_table[i].name) == 0) {
959 switch (builtin_table[i].fmt_kind) {
961 check_printf_format(arg, &builtin_table[i]);
964 check_scanf_format(arg, &builtin_table[i]);
966 case FORMAT_STRFTIME:
968 /* TODO: implement other cases */