forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfrr-format.c
4475 lines (3917 loc) · 136 KB
/
frr-format.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Check calls to formatted I/O functions (-Wformat).
Copyright (C) 1992-2019 Free Software Foundation, Inc.
Extended for FRR's printfrr() with Linux kernel style extensions
Copyright (C) 2019-2020 David Lamparter, for NetDEF, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.GPLv3. If not see
<http://www.gnu.org/licenses/>. */
#include "gcc-common.h"
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
//include "c-target.h"
#include "c-common.h"
#include "alloc-pool.h"
#include "stringpool.h"
#include "c-tree.h"
#include "c-objc.h"
#include "intl.h"
#include "langhooks.h"
#include "frr-format.h"
#include "diagnostic.h"
#include "substring-locations.h"
#include "selftest.h"
#include "selftest-diagnostic.h"
#ifndef FIRST_PSEUDO_REGISTER
#define FIRST_PSEUDO_REGISTER 0
#endif
#include "builtins.h"
#include "attribs.h"
#include "gcc-rich-location.h"
#include "c-pretty-print.h"
#include "c-pragma.h"
extern struct cpp_reader *parse_in;
#pragma GCC visibility push(hidden)
/* Handle attributes associated with format checking. */
/* This must be in the same order as format_types, except for
format_type_error. Target-specific format types do not have
matching enum values. */
enum format_type { frr_printf_format_type,
format_type_error = -1};
struct function_format_info
{
int format_type; /* type of format (printf, scanf, etc.) */
unsigned HOST_WIDE_INT format_num; /* number of format argument */
unsigned HOST_WIDE_INT first_arg_num; /* number of first arg (zero for varargs) */
};
static GTY(()) tree local_uint64_t_node;
static GTY(()) tree local_int64_t_node;
static GTY(()) tree local_size_t_node;
static GTY(()) tree local_ssize_t_node;
static GTY(()) tree local_atomic_size_t_node;
static GTY(()) tree local_atomic_ssize_t_node;
static GTY(()) tree local_ptrdiff_t_node;
static GTY(()) tree local_pid_t_node;
static GTY(()) tree local_uid_t_node;
static GTY(()) tree local_gid_t_node;
static GTY(()) tree local_time_t_node;
static GTY(()) tree local_socklen_t_node;
static GTY(()) tree local_in_addr_t_node;
static struct type_special {
tree *match;
tree *replace;
tree *cousin;
} special_types[] = {
{ &local_atomic_size_t_node, &local_size_t_node, &local_ssize_t_node, },
{ &local_atomic_ssize_t_node, &local_ssize_t_node, &local_size_t_node, },
{ &local_size_t_node, NULL, &local_ssize_t_node, },
{ &local_ssize_t_node, NULL, &local_size_t_node, },
{ &local_uint64_t_node, NULL, &local_int64_t_node, },
{ &local_int64_t_node, NULL, &local_uint64_t_node, },
{ &local_pid_t_node, NULL, &local_pid_t_node, },
{ &local_uid_t_node, NULL, &local_uid_t_node, },
{ &local_gid_t_node, NULL, &local_gid_t_node, },
{ &local_time_t_node, NULL, &local_time_t_node, },
{ NULL, NULL, NULL, }
};
static bool decode_format_attr (tree, function_format_info *, int);
static int decode_format_type (const char *);
static bool check_format_string (tree argument,
unsigned HOST_WIDE_INT format_num,
int flags, bool *no_add_attrs,
int expected_format_type);
static bool get_constant (tree expr, unsigned HOST_WIDE_INT *value,
int validated_p);
static const char *convert_format_name_to_system_name (const char *attr_name);
static int first_target_format_type;
static const char *format_name (int format_num);
static int format_flags (int format_num);
/* Emit a warning as per format_warning_va, but construct the substring_loc
for the character at offset (CHAR_IDX - 1) within a string constant
FORMAT_STRING_CST at FMT_STRING_LOC. */
ATTRIBUTE_GCC_DIAG (5,6)
static bool
format_warning_at_char (location_t fmt_string_loc, tree format_string_cst,
int char_idx, int opt, const char *gmsgid, ...)
{
va_list ap;
va_start (ap, gmsgid);
tree string_type = TREE_TYPE (format_string_cst);
/* The callers are of the form:
format_warning (format_string_loc, format_string_cst,
format_chars - orig_format_chars,
where format_chars has already been incremented, so that
CHAR_IDX is one character beyond where the warning should
be emitted. Fix it. */
char_idx -= 1;
substring_loc fmt_loc (fmt_string_loc, string_type, char_idx, char_idx,
char_idx);
#if BUILDING_GCC_VERSION >= 9000
format_string_diagnostic_t diag (fmt_loc, NULL, UNKNOWN_LOCATION, NULL,
NULL);
bool warned = diag.emit_warning_va (opt, gmsgid, &ap);
#else
bool warned = format_warning_va (fmt_loc, UNKNOWN_LOCATION, NULL,
opt, gmsgid, &ap);
#endif
va_end (ap);
return warned;
}
/* Check that we have a pointer to a string suitable for use as a format.
The default is to check for a char type.
For objective-c dialects, this is extended to include references to string
objects validated by objc_string_ref_type_p ().
Targets may also provide a string object type that can be used within c and
c++ and shared with their respective objective-c dialects. In this case the
reference to a format string is checked for validity via a hook.
The function returns true if strref points to any string type valid for the
language dialect and target. */
static bool
valid_stringptr_type_p (tree strref)
{
return (strref != NULL
&& TREE_CODE (strref) == POINTER_TYPE
&& (TYPE_MAIN_VARIANT (TREE_TYPE (strref)) == char_type_node
|| objc_string_ref_type_p (strref)));
// || (*targetcm.string_object_ref_type_p) ((const_tree) strref)));
}
/* Handle a "format_arg" attribute; arguments as in
struct attribute_spec.handler. */
tree
handle_frr_format_arg_attribute (tree *node, tree ARG_UNUSED (name),
tree args, int flags, bool *no_add_attrs)
{
tree type = *node;
tree format_num_expr = TREE_VALUE (args);
unsigned HOST_WIDE_INT format_num = 0;
if (!get_constant (format_num_expr, &format_num, 0))
{
error ("format string has invalid operand number");
*no_add_attrs = true;
return NULL_TREE;
}
if (prototype_p (type))
{
/* The format arg can be any string reference valid for the language and
target. We cannot be more specific in this case. */
if (!check_format_string (type, format_num, flags, no_add_attrs, -1))
return NULL_TREE;
}
if (!valid_stringptr_type_p (TREE_TYPE (type)))
{
if (!(flags & (int) ATTR_FLAG_BUILT_IN))
error ("function does not return string type");
*no_add_attrs = true;
return NULL_TREE;
}
return NULL_TREE;
}
/* Verify that the format_num argument is actually a string reference suitable,
for the language dialect and target (in case the format attribute is in
error). When we know the specific reference type expected, this is also
checked. */
static bool
check_format_string (tree fntype, unsigned HOST_WIDE_INT format_num,
int flags, bool *no_add_attrs, int expected_format_type)
{
unsigned HOST_WIDE_INT i;
bool is_target_sref, is_char_ref;
tree ref;
int fmt_flags;
function_args_iterator iter;
i = 1;
FOREACH_FUNCTION_ARGS (fntype, ref, iter)
{
if (i == format_num)
break;
i++;
}
if (!ref
|| !valid_stringptr_type_p (ref))
{
if (!(flags & (int) ATTR_FLAG_BUILT_IN))
error ("format string argument is not a string type");
*no_add_attrs = true;
return false;
}
/* We only know that we want a suitable string reference. */
if (expected_format_type < 0)
return true;
/* Now check that the arg matches the expected type. */
is_char_ref =
(TYPE_MAIN_VARIANT (TREE_TYPE (ref)) == char_type_node);
fmt_flags = format_flags (expected_format_type);
is_target_sref = false;
if (!(fmt_flags & FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL))
{
if (is_char_ref)
return true; /* OK, we expected a char and found one. */
else
{
error ("found a %qT but the format argument should be a string",
ref);
*no_add_attrs = true;
return false;
}
}
/* We expect a string object type as the format arg. */
if (is_char_ref)
{
error ("format argument should be a %qs reference but a string was found", format_name (expected_format_type));
*no_add_attrs = true;
return false;
}
/* We will allow a target string ref to match only itself. */
if (first_target_format_type
&& expected_format_type >= first_target_format_type
&& is_target_sref)
return true;
else
{
error ("format argument should be a %qs reference",
format_name (expected_format_type));
*no_add_attrs = true;
return false;
}
gcc_unreachable ();
}
/* Verify EXPR is a constant, and store its value.
If validated_p is true there should be no errors.
Returns true on success, false otherwise. */
static bool
get_constant (tree expr, unsigned HOST_WIDE_INT *value, int validated_p)
{
if (!tree_fits_uhwi_p (expr))
{
gcc_assert (!validated_p);
return false;
}
*value = TREE_INT_CST_LOW (expr);
return true;
}
/* Decode the arguments to a "format" attribute into a
function_format_info structure. It is already known that the list
is of the right length. If VALIDATED_P is true, then these
attributes have already been validated and must not be erroneous;
if false, it will give an error message. Returns true if the
attributes are successfully decoded, false otherwise. */
static bool
decode_format_attr (tree args, function_format_info *info, int validated_p)
{
tree format_type_id = TREE_VALUE (args);
tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
tree first_arg_num_expr
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
if (TREE_CODE (format_type_id) != STRING_CST)
{
gcc_assert (!validated_p);
error ("unrecognized format specifier");
return false;
}
else
{
const char *p = TREE_STRING_POINTER (format_type_id);
p = convert_format_name_to_system_name (p);
info->format_type = decode_format_type (p);
if (info->format_type == format_type_error)
{
gcc_assert (!validated_p);
warning (OPT_Wformat_, "%qE is an unrecognized format function type",
format_type_id);
return false;
}
}
if (!get_constant (format_num_expr, &info->format_num, validated_p))
{
error ("format string has invalid operand number");
return false;
}
if (!get_constant (first_arg_num_expr, &info->first_arg_num, validated_p))
{
error ("%<...%> has invalid operand number");
return false;
}
if (info->first_arg_num != 0 && info->first_arg_num <= info->format_num)
{
gcc_assert (!validated_p);
error ("format string argument follows the arguments to be formatted");
return false;
}
return true;
}
/* Check a call to a format function against a parameter list. */
/* The C standard version C++ is treated as equivalent to
or inheriting from, for the purpose of format features supported. */
#define CPLUSPLUS_STD_VER (cxx_dialect < cxx11 ? STD_C94 : STD_C99)
/* The C standard version we are checking formats against when pedantic. */
#define C_STD_VER ((int) (c_dialect_cxx () \
? CPLUSPLUS_STD_VER \
: (flag_isoc99 \
? STD_C99 \
: (flag_isoc94 ? STD_C94 : STD_C89))))
/* The name to give to the standard version we are warning about when
pedantic. FEATURE_VER is the version in which the feature warned out
appeared, which is higher than C_STD_VER. */
#define C_STD_NAME(FEATURE_VER) (c_dialect_cxx () \
? (cxx_dialect < cxx11 ? "ISO C++98" \
: "ISO C++11") \
: ((FEATURE_VER) == STD_EXT \
? "ISO C" \
: "ISO C90"))
/* Adjust a C standard version, which may be STD_C9L, to account for
-Wno-long-long. Returns other standard versions unchanged. */
#define ADJ_STD(VER) ((int) ((VER) == STD_C9L \
? (warn_long_long ? STD_C99 : STD_C89) \
: (VER)))
/* Enum describing the kind of specifiers present in the format and
requiring an argument. */
enum format_specifier_kind {
CF_KIND_FORMAT,
CF_KIND_FIELD_WIDTH,
CF_KIND_FIELD_PRECISION
};
static const char *kind_descriptions[] = {
N_("format"),
N_("field width specifier"),
N_("field precision specifier")
};
/* Structure describing details of a type expected in format checking,
and the type to check against it. */
struct format_wanted_type
{
/* The type wanted. */
tree wanted_type;
/* The name of this type to use in diagnostics. */
const char *wanted_type_name;
/* Should be type checked just for scalar width identity. */
int scalar_identity_flag;
/* The level of indirection through pointers at which this type occurs. */
int pointer_count;
/* Whether, when pointer_count is 1, to allow any character type when
pedantic, rather than just the character or void type specified. */
int char_lenient_flag;
/* Whether the argument, dereferenced once, is written into and so the
argument must not be a pointer to a const-qualified type. */
int writing_in_flag;
/* Whether the argument, dereferenced once, is read from and so
must not be a NULL pointer. */
int reading_from_flag;
/* The kind of specifier that this type is used for. */
enum format_specifier_kind kind;
/* The starting character of the specifier. This never includes the
initial percent sign. */
const char *format_start;
/* The length of the specifier. */
int format_length;
/* The actual parameter to check against the wanted type. */
tree param;
/* The argument number of that parameter. */
int arg_num;
/* The offset location of this argument with respect to the format
string location. */
unsigned int offset_loc;
/* The next type to check for this format conversion, or NULL if none. */
struct format_wanted_type *next;
};
/* Convenience macro for format_length_info meaning unused. */
#define NO_FMT NULL, FMT_LEN_none, STD_C89
static const format_length_info printf_length_specs[] =
{
{ "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99, 0 },
{ "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L, 0 },
{ "q", FMT_LEN_ll, STD_EXT, NO_FMT, 0 },
{ "L", FMT_LEN_L, STD_C89, NO_FMT, 0 },
{ "z", FMT_LEN_z, STD_C99, NO_FMT, 0 },
{ "Z", FMT_LEN_z, STD_EXT, NO_FMT, 0 },
{ "t", FMT_LEN_t, STD_C99, NO_FMT, 0 },
{ "j", FMT_LEN_j, STD_C99, NO_FMT, 0 },
{ "H", FMT_LEN_H, STD_EXT, NO_FMT, 0 },
{ "D", FMT_LEN_D, STD_EXT, "DD", FMT_LEN_DD, STD_EXT, 0 },
{ NO_FMT, NO_FMT, 0 }
};
static const format_flag_spec printf_flag_specs[] =
{
{ ' ', 0, 0, 0, N_("' ' flag"), N_("the ' ' printf flag"), STD_C89 },
{ '+', 0, 0, 0, N_("'+' flag"), N_("the '+' printf flag"), STD_C89 },
{ '#', 0, 0, 0, N_("'#' flag"), N_("the '#' printf flag"), STD_C89 },
{ '0', 0, 0, 0, N_("'0' flag"), N_("the '0' printf flag"), STD_C89 },
{ '-', 0, 0, 0, N_("'-' flag"), N_("the '-' printf flag"), STD_C89 },
{ '\'', 0, 0, 0, N_("''' flag"), N_("the ''' printf flag"), STD_EXT },
{ 'I', 0, 0, 0, N_("'I' flag"), N_("the 'I' printf flag"), STD_EXT },
{ 'w', 0, 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 },
{ 'p', 0, 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 },
{ 'L', 0, 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
{ 0, 0, 0, 0, NULL, NULL, STD_C89 }
};
static const format_flag_pair printf_flag_pairs[] =
{
{ ' ', '+', 1, 0 },
{ '0', '-', 1, 0 },
{ '0', 'p', 1, 'i' },
{ 0, 0, 0, 0 }
};
#define ETAB_SZ 128
static kernel_ext_fmt ext_p[ETAB_SZ] = {
{ }
};
static kernel_ext_fmt ext_d[ETAB_SZ] = {
{ }
};
static const format_char_info print_char_table[] =
{
/* C89 conversion specifiers. */
/* none, hh, h, l, ll, L, z, t, j, H, D, DD */
{ "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_S64, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "-wp0 +'I", "i", NULL, ext_d },
{ "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_U64, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0#", "i", NULL, NULL },
{ "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_U64, T99_ST, T99_UPD, T99_UIM, BADLEN, BADLEN, BADLEN }, "-wp0'I", "i", NULL, NULL },
{ "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL, NULL },
{ "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#I", "", NULL, NULL },
{ "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL, NULL },
{ "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR", NULL, NULL },
{ "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "c", NULL, ext_p },
{ "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM, BADLEN, BADLEN, BADLEN }, "", "W", NULL, NULL },
/* C99 conversion specifiers. */
{ "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, TEX_D32, TEX_D64, TEX_D128 }, "-wp0 +#'I", "", NULL, NULL },
{ "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "", NULL, NULL },
/* X/Open conversion specifiers. */
{ "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "", NULL, NULL },
{ "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R", NULL, NULL },
/* GNU conversion specifiers. */
{ "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "", NULL, NULL },
{ NULL, 0, STD_C89, NOLENGTHS, NULL, NULL, NULL, NULL }
};
/* This must be in the same order as enum format_type. */
static const format_kind_info format_types_orig[] =
{
{ "frr_printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
printf_flag_specs, printf_flag_pairs,
FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK,
'w', 0, 'p', 0, 'L', 0,
&integer_type_node, &integer_type_node
},
};
/* This layer of indirection allows GCC to reassign format_types with
new data if necessary, while still allowing the original data to be
const. */
static const format_kind_info *format_types = format_types_orig;
static int n_format_types = ARRAY_SIZE (format_types_orig);
/* Structure detailing the results of checking a format function call
where the format expression may be a conditional expression with
many leaves resulting from nested conditional expressions. */
struct format_check_results
{
/* Number of leaves of the format argument that could not be checked
as they were not string literals. */
int number_non_literal;
/* Number of leaves of the format argument that were null pointers or
string literals, but had extra format arguments. */
int number_extra_args;
location_t extra_arg_loc;
/* Number of leaves of the format argument that were null pointers or
string literals, but had extra format arguments and used $ operand
numbers. */
int number_dollar_extra_args;
/* Number of leaves of the format argument that were wide string
literals. */
int number_wide;
/* Number of leaves of the format argument that are not array of "char". */
int number_non_char;
/* Number of leaves of the format argument that were empty strings. */
int number_empty;
/* Number of leaves of the format argument that were unterminated
strings. */
int number_unterminated;
/* Number of leaves of the format argument that were not counted above. */
int number_other;
/* Location of the format string. */
location_t format_string_loc;
};
struct format_check_context
{
format_check_results *res;
function_format_info *info;
tree params;
vec<location_t> *arglocs;
};
/* Return the format name (as specified in the original table) for the format
type indicated by format_num. */
static const char *
format_name (int format_num)
{
if (format_num >= 0 && format_num < n_format_types)
return format_types[format_num].name;
gcc_unreachable ();
}
/* Return the format flags (as specified in the original table) for the format
type indicated by format_num. */
static int
format_flags (int format_num)
{
if (format_num >= 0 && format_num < n_format_types)
return format_types[format_num].flags;
gcc_unreachable ();
}
static void check_format_info (function_format_info *, tree,
vec<location_t> *);
static void check_format_arg (void *, tree, unsigned HOST_WIDE_INT);
static void check_format_info_main (format_check_results *,
function_format_info *, const char *,
location_t, tree,
int, tree,
unsigned HOST_WIDE_INT,
object_allocator<format_wanted_type> &,
vec<location_t> *);
static void init_dollar_format_checking (int, tree);
static int maybe_read_dollar_number (const char **, int,
tree, tree *, const format_kind_info *);
static bool avoid_dollar_number (const char *);
static void finish_dollar_format_checking (format_check_results *, int);
static const format_flag_spec *get_flag_spec (const format_flag_spec *,
int, const char *);
static void check_format_types (const substring_loc &fmt_loc,
format_wanted_type *,
const format_kind_info *fki,
int offset_to_type_start,
char conversion_char,
vec<location_t> *arglocs);
static void format_type_warning (const substring_loc &fmt_loc,
location_t param_loc,
format_wanted_type *, tree,
tree,
const format_kind_info *fki,
int offset_to_type_start,
char conversion_char,
const char *extra = NULL);
static bool check_kef_type (const substring_loc &fmt_loc,
const struct kernel_ext_fmt *kef,
unsigned arg_num,
tree cur_param,
tree wanted_type,
const format_kind_info *fki,
int offset_to_type_start,
char conversion_char,
vec<location_t> *arglocs);
/* Decode a format type from a string, returning the type, or
format_type_error if not valid, in which case the caller should print an
error message. */
static int
decode_format_type (const char *s)
{
int i;
int slen;
s = convert_format_name_to_system_name (s);
slen = strlen (s);
for (i = 0; i < n_format_types; i++)
{
int alen;
if (!strcmp (s, format_types[i].name))
return i;
alen = strlen (format_types[i].name);
if (slen == alen + 4 && s[0] == '_' && s[1] == '_'
&& s[slen - 1] == '_' && s[slen - 2] == '_'
&& !strncmp (s + 2, format_types[i].name, alen))
return i;
}
return format_type_error;
}
/* Check the argument list of a call to printf, scanf, etc.
ATTRS are the attributes on the function type. There are NARGS argument
values in the array ARGARRAY.
Also, if -Wsuggest-attribute=format,
warn for calls to vprintf or vscanf in functions with no such format
attribute themselves. */
void
check_function_format (tree attrs, int nargs, tree *argarray,
vec<location_t> *arglocs)
{
tree a;
/* See if this function has any format attributes. */
for (a = attrs; a; a = TREE_CHAIN (a))
{
if (is_attribute_p ("frr_format", TREE_PURPOSE (a)))
{
/* Yup; check it. */
function_format_info info;
decode_format_attr (TREE_VALUE (a), &info, /*validated=*/true);
if (warn_format)
{
/* FIXME: Rewrite all the internal functions in this file
to use the ARGARRAY directly instead of constructing this
temporary list. */
tree params = NULL_TREE;
int i;
for (i = nargs - 1; i >= 0; i--)
params = tree_cons (NULL_TREE, argarray[i], params);
check_format_info (&info, params, arglocs);
}
/* Attempt to detect whether the current function might benefit
from the format attribute if the called function is decorated
with it. Avoid using calls with string literal formats for
guidance since those are unlikely to be viable candidates. */
if (warn_suggest_attribute_format
&& current_function_decl != NULL_TREE
&& info.first_arg_num == 0
&& (format_types[info.format_type].flags
& (int) FMT_FLAG_ARG_CONVERT)
/* c_strlen will fail for a function parameter but succeed
for a literal or constant array. */
&& !c_strlen (argarray[info.format_num - 1], 1))
{
tree c;
for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
c;
c = TREE_CHAIN (c))
if (is_attribute_p ("frr_format", TREE_PURPOSE (c))
&& (decode_format_type (IDENTIFIER_POINTER
(TREE_VALUE (TREE_VALUE (c))))
== info.format_type))
break;
if (c == NULL_TREE)
{
/* Check if the current function has a parameter to which
the format attribute could be attached; if not, it
can't be a candidate for a format attribute, despite
the vprintf-like or vscanf-like call. */
tree args;
for (args = DECL_ARGUMENTS (current_function_decl);
args != 0;
args = DECL_CHAIN (args))
{
if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
&& (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args)))
== char_type_node))
break;
}
if (args != 0)
warning (OPT_Wsuggest_attribute_format,
"function %qD might be a candidate for %qs %<frr_format%> attribute",
current_function_decl,
format_types[info.format_type].name);
}
}
}
}
}
/* Variables used by the checking of $ operand number formats. */
static char *dollar_arguments_used = NULL;
static char *dollar_arguments_pointer_p = NULL;
static int dollar_arguments_alloc = 0;
static int dollar_arguments_count;
static int dollar_first_arg_num;
static int dollar_max_arg_used;
static int dollar_format_warned;
/* Initialize the checking for a format string that may contain $
parameter number specifications; we will need to keep track of whether
each parameter has been used. FIRST_ARG_NUM is the number of the first
argument that is a parameter to the format, or 0 for a vprintf-style
function; PARAMS is the list of arguments starting at this argument. */
static void
init_dollar_format_checking (int first_arg_num, tree params)
{
tree oparams = params;
dollar_first_arg_num = first_arg_num;
dollar_arguments_count = 0;
dollar_max_arg_used = 0;
dollar_format_warned = 0;
if (first_arg_num > 0)
{
while (params)
{
dollar_arguments_count++;
params = TREE_CHAIN (params);
}
}
if (dollar_arguments_alloc < dollar_arguments_count)
{
free (dollar_arguments_used);
free (dollar_arguments_pointer_p);
dollar_arguments_alloc = dollar_arguments_count;
dollar_arguments_used = XNEWVEC (char, dollar_arguments_alloc);
dollar_arguments_pointer_p = XNEWVEC (char, dollar_arguments_alloc);
}
if (dollar_arguments_alloc)
{
memset (dollar_arguments_used, 0, dollar_arguments_alloc);
if (first_arg_num > 0)
{
int i = 0;
params = oparams;
while (params)
{
dollar_arguments_pointer_p[i] = (TREE_CODE (TREE_TYPE (TREE_VALUE (params)))
== POINTER_TYPE);
params = TREE_CHAIN (params);
i++;
}
}
}
}
/* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
is set, it is an error if one is not found; otherwise, it is OK. If
such a number is found, check whether it is within range and mark that
numbered operand as being used for later checking. Returns the operand
number if found and within range, zero if no such number was found and
this is OK, or -1 on error. PARAMS points to the first operand of the
format; PARAM_PTR is made to point to the parameter referred to. If
a $ format is found, *FORMAT is updated to point just after it. */
static int
maybe_read_dollar_number (const char **format,
int dollar_needed, tree params, tree *param_ptr,
const format_kind_info *fki)
{
int argnum;
int overflow_flag;
const char *fcp = *format;
if (!ISDIGIT (*fcp))
{
if (dollar_needed)
{
warning (OPT_Wformat_, "missing $ operand number in format");
return -1;
}
else
return 0;
}
argnum = 0;
overflow_flag = 0;
while (ISDIGIT (*fcp))
{
int nargnum;
nargnum = 10 * argnum + (*fcp - '0');
if (nargnum < 0 || nargnum / 10 != argnum)
overflow_flag = 1;
argnum = nargnum;
fcp++;
}
if (*fcp != '$')
{
if (dollar_needed)
{
warning (OPT_Wformat_, "missing $ operand number in format");
return -1;
}
else
return 0;
}
*format = fcp + 1;
if (pedantic && !dollar_format_warned)
{
warning (OPT_Wformat_, "%s does not support %%n$ operand number formats",
C_STD_NAME (STD_EXT));
dollar_format_warned = 1;
}
if (overflow_flag || argnum == 0
|| (dollar_first_arg_num && argnum > dollar_arguments_count))
{
warning (OPT_Wformat_, "operand number out of range in format");
return -1;
}
if (argnum > dollar_max_arg_used)
dollar_max_arg_used = argnum;
/* For vprintf-style functions we may need to allocate more memory to
track which arguments are used. */
while (dollar_arguments_alloc < dollar_max_arg_used)
{
int nalloc;
nalloc = 2 * dollar_arguments_alloc + 16;
dollar_arguments_used = XRESIZEVEC (char, dollar_arguments_used,
nalloc);
dollar_arguments_pointer_p = XRESIZEVEC (char, dollar_arguments_pointer_p,
nalloc);
memset (dollar_arguments_used + dollar_arguments_alloc, 0,
nalloc - dollar_arguments_alloc);
dollar_arguments_alloc = nalloc;
}
if (!(fki->flags & (int) FMT_FLAG_DOLLAR_MULTIPLE)
&& dollar_arguments_used[argnum - 1] == 1)
{
dollar_arguments_used[argnum - 1] = 2;
warning (OPT_Wformat_, "format argument %d used more than once in %s format",
argnum, fki->name);
}
else
dollar_arguments_used[argnum - 1] = 1;
if (dollar_first_arg_num)
{
int i;
*param_ptr = params;
for (i = 1; i < argnum && *param_ptr != 0; i++)
*param_ptr = TREE_CHAIN (*param_ptr);
/* This case shouldn't be caught here. */
gcc_assert (*param_ptr);
}
else
*param_ptr = 0;
return argnum;
}
/* Ensure that FORMAT does not start with a decimal number followed by
a $; give a diagnostic and return true if it does, false otherwise. */
static bool
avoid_dollar_number (const char *format)
{
if (!ISDIGIT (*format))
return false;
while (ISDIGIT (*format))
format++;
if (*format == '$')
{
warning (OPT_Wformat_, "%<$%> operand number used after format without operand number");
return true;
}
return false;
}
/* Finish the checking for a format string that used $ operand number formats
instead of non-$ formats. We check for unused operands before used ones
(a serious error, since the implementation of the format function
can't know what types to pass to va_arg to find the later arguments).
and for unused operands at the end of the format (if we know how many
arguments the format had, so not for vprintf). If there were operand
numbers out of range on a non-vprintf-style format, we won't have reached
here. If POINTER_GAP_OK, unused arguments are OK if all arguments are
pointers. */
static void
finish_dollar_format_checking (format_check_results *res, int pointer_gap_ok)
{
int i;
bool found_pointer_gap = false;
for (i = 0; i < dollar_max_arg_used; i++)
{
if (!dollar_arguments_used[i])
{
if (pointer_gap_ok && (dollar_first_arg_num == 0
|| dollar_arguments_pointer_p[i]))
found_pointer_gap = true;
else
warning_at (res->format_string_loc, OPT_Wformat_,
"format argument %d unused before used argument %d in %<$%>-style format",
i + 1, dollar_max_arg_used);
}
}
if (found_pointer_gap
|| (dollar_first_arg_num
&& dollar_max_arg_used < dollar_arguments_count))
{
res->number_other--;
res->number_dollar_extra_args++;
}
}
/* Retrieve the specification for a format flag. SPEC contains the
specifications for format flags for the applicable kind of format.
FLAG is the flag in question. If PREDICATES is NULL, the basic
spec for that flag must be retrieved and must exist. If
PREDICATES is not NULL, it is a string listing possible predicates
for the spec entry; if an entry predicated on any of these is
found, it is returned, otherwise NULL is returned. */
static const format_flag_spec *
get_flag_spec (const format_flag_spec *spec, int flag, const char *predicates)
{
int i;
for (i = 0; spec[i].flag_char != 0; i++)
{
if (spec[i].flag_char != flag)
continue;
if (predicates != NULL)
{
if (spec[i].predicate != 0
&& strchr (predicates, spec[i].predicate) != 0)
return &spec[i];
}
else if (spec[i].predicate == 0)
return &spec[i];
}
gcc_assert (predicates);
return NULL;
}