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
|
/* Compiler implementation of the D programming language
* Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
* written by Walter Bright
* http://www.digitalmars.com
* Distributed under the Boost Software License, Version 1.0.
* http://www.boost.org/LICENSE_1_0.txt
*/
// Check the arguments to `printf` and `scanf` against the `format` string.
#include "root/dsystem.h"
#include "root/dcompat.h"
#include "arraytypes.h"
#include "cond.h"
#include "errors.h"
#include "expression.h"
#include "globals.h"
#include "identifier.h"
#include "mtype.h"
#include "target.h"
/* Different kinds of formatting specifications, variations we don't
care about are merged. (Like we don't care about the difference between
f, e, g, a, etc.)
For `scanf`, every format is a pointer.
*/
enum Format
{
Format_d, // int
Format_hhd, // signed char
Format_hd, // short int
Format_ld, // long int
Format_lld, // long long int
Format_jd, // intmax_t
Format_zd, // size_t
Format_td, // ptrdiff_t
Format_u, // unsigned int
Format_hhu, // unsigned char
Format_hu, // unsigned short int
Format_lu, // unsigned long int
Format_llu, // unsigned long long int
Format_ju, // uintmax_t
Format_g, // float (scanf) / double (printf)
Format_lg, // double (scanf)
Format_Lg, // long double (both)
Format_s, // char string (both)
Format_ls, // wchar_t string (both)
Format_c, // char (printf)
Format_lc, // wint_t (printf)
Format_p, // pointer
Format_n, // pointer to int
Format_hhn, // pointer to signed char
Format_hn, // pointer to short
Format_ln, // pointer to long int
Format_lln, // pointer to long long int
Format_jn, // pointer to intmax_t
Format_zn, // pointer to size_t
Format_tn, // pointer to ptrdiff_t
Format_GNU_a, // GNU ext. : address to a string with no maximum size (scanf)
Format_GNU_m, // GNU ext. : string corresponding to the error code in errno (printf) / length modifier (scanf)
Format_percent, // %% (i.e. no argument)
Format_error, // invalid format specification
};
/**************************************
* Parse the *length specifier* and the *specifier* of the following form:
* `[length]specifier`
*
* Params:
* format = format string
* idx = index of of start of format specifier,
* which gets updated to index past the end of it,
* even if `Format_error` is returned
* genSpecifier = Generic specifier. For instance, it will be set to `d` if the
* format is `hdd`.
* Returns:
* Format
*/
static Format parseGenericFormatSpecifier(const char *format,
size_t &idx, char &genSpecifier, bool useGNUExts =
findCondition(global.versionids, Identifier::idPool("CRuntime_Glibc")))
{
genSpecifier = 0;
const size_t length = strlen(format);
/* Read the `length modifier`
*/
const char lm = format[idx];
bool lm1= false; // if jztL
bool lm2= false; // if `hh` or `ll`
if (lm == 'j' ||
lm == 'z' ||
lm == 't' ||
lm == 'L')
{
++idx;
if (idx == length)
return Format_error;
lm1 = true;
}
else if (lm == 'h' || lm == 'l')
{
++idx;
if (idx == length)
return Format_error;
lm2 = lm == format[idx];
if (lm2)
{
++idx;
if (idx == length)
return Format_error;
}
}
/* Read the `specifier`
*/
Format specifier;
const char sc = format[idx];
genSpecifier = sc;
switch (sc)
{
case 'd':
case 'i':
if (lm == 'L')
specifier = Format_error;
else
specifier = lm == 'h' && lm2 ? Format_hhd :
lm == 'h' ? Format_hd :
lm == 'l' && lm2 ? Format_lld :
lm == 'l' ? Format_ld :
lm == 'j' ? Format_jd :
lm == 'z' ? Format_zd :
lm == 't' ? Format_td :
Format_d;
break;
case 'u':
case 'o':
case 'x':
case 'X':
if (lm == 'L')
specifier = Format_error;
else
specifier = lm == 'h' && lm2 ? Format_hhu :
lm == 'h' ? Format_hu :
lm == 'l' && lm2 ? Format_llu :
lm == 'l' ? Format_lu :
lm == 'j' ? Format_ju :
lm == 'z' ? Format_zd :
lm == 't' ? Format_td :
Format_u;
break;
case 'a':
if (useGNUExts)
{
// https://www.gnu.org/software/libc/manual/html_node/Dynamic-String-Input.html
specifier = Format_GNU_a;
break;
}
/* fall through */
case 'f':
case 'F':
case 'e':
case 'E':
case 'g':
case 'G':
case 'A':
if (lm == 'L')
specifier = Format_Lg;
else if (lm1 || lm2 || lm == 'h')
specifier = Format_error;
else
specifier = lm == 'l' ? Format_lg : Format_g;
break;
case 'c':
if (lm1 || lm2 || lm == 'h')
specifier = Format_error;
else
specifier = lm == 'l' ? Format_lc : Format_c;
break;
case 's':
if (lm1 || lm2 || lm == 'h')
specifier = Format_error;
else
specifier = lm == 'l' ? Format_ls : Format_s;
break;
case 'p':
if (lm1 || lm2 || lm == 'h' || lm == 'l')
specifier = Format_error;
else
specifier = Format_p;
break;
case 'n':
if (lm == 'L')
specifier = Format_error;
else
specifier = lm == 'l' && lm2 ? Format_lln :
lm == 'l' ? Format_ln :
lm == 'h' && lm2 ? Format_hhn :
lm == 'h' ? Format_hn :
lm == 'j' ? Format_jn :
lm == 'z' ? Format_zn :
lm == 't' ? Format_tn :
Format_n;
break;
case 'm':
if (useGNUExts)
{
// http://www.gnu.org/software/libc/manual/html_node/Other-Output-Conversions.html
specifier = Format_GNU_m;
break;
}
goto Ldefault;
default:
Ldefault:
specifier = Format_error;
break;
}
++idx;
return specifier; // success
}
Format formatError(size_t &idx, size_t i)
{
idx = i;
return Format_error;
}
/**************************************
* Parse the *format specifier* which is of the form:
*
* `%[*][width][length]specifier`
*
* Params:
* format = format string
* idx = index of `%` of start of format specifier,
* which gets updated to index past the end of it,
* even if `Format_error` is returned
* asterisk = set if there is a `*` sub-specifier
* Returns:
* Format
*/
static Format parseScanfFormatSpecifier(const char *format, size_t &idx,
bool &asterisk)
{
asterisk = false;
size_t i = idx;
assert(format[i] == '%');
const size_t length = strlen(format);
++i;
if (i == length)
return formatError(idx, i);
if (format[i] == '%')
{
idx = i + 1;
return Format_percent;
}
// * sub-specifier
if (format[i] == '*')
{
++i;
if (i == length)
return formatError(idx, i);
asterisk = true;
}
// fieldWidth
while (isdigit(format[i]))
{
i++;
if (i == length)
return formatError(idx, i);
}
/* Read the scanset
* A scanset can be anything, so we just check that it is paired
*/
if (format[i] == '[')
{
while (i < length)
{
if (format[i] == ']')
break;
++i;
}
// no `]` found
if (i == length)
return formatError(idx, i);
++i;
// no specifier after `]`
// it could be mixed with the one above, but then idx won't have the right index
if (i == length)
return formatError(idx, i);
}
/* Read the specifier
*/
char genSpec;
Format specifier = parseGenericFormatSpecifier(format, i, genSpec);
if (specifier == Format_error)
return formatError(idx, i);
idx = i;
return specifier; // success
}
/**************************************
* Parse the *format specifier* which is of the form:
*
* `%[flags][field width][.precision][length modifier]specifier`
*
* Params:
* format = format string
* idx = index of `%` of start of format specifier,
* which gets updated to index past the end of it,
* even if `Format_error` is returned
* widthStar = set if * for width
* precisionStar = set if * for precision
* Returns:
* Format
*/
static Format parsePrintfFormatSpecifier(const char *format, size_t &idx,
bool &widthStar, bool &precisionStar)
{
widthStar = false;
precisionStar = false;
size_t i = idx;
assert(format[i] == '%');
const size_t format_length = strlen(format);
const size_t length = format_length;
bool hash = false;
bool zero = false;
bool flags = false;
bool width = false;
bool precision = false;
++i;
if (i == length)
return formatError(idx, i);
if (format[i] == '%')
{
idx = i + 1;
return Format_percent;
}
/* Read the `flags`
*/
while (1)
{
const char c = format[i];
if (c == '-' ||
c == '+' ||
c == ' ')
{
flags = true;
}
else if (c == '#')
{
hash = true;
}
else if (c == '0')
{
zero = true;
}
else
break;
++i;
if (i == length)
return formatError(idx, i);
}
/* Read the `field width`
*/
{
const char c = format[i];
if (c == '*')
{
width = true;
widthStar = true;
++i;
if (i == length)
return formatError(idx, i);
}
else if ('1' <= c && c <= '9')
{
width = true;
++i;
if (i == length)
return formatError(idx, i);
while ('0' <= format[i] && format[i] <= '9')
{
++i;
if (i == length)
return formatError(idx, i);
}
}
}
/* Read the `precision`
*/
if (format[i] == '.')
{
precision = true;
++i;
if (i == length)
return formatError(idx, i);
const char c = format[i];
if (c == '*')
{
precisionStar = true;
++i;
if (i == length)
return formatError(idx, i);
}
else if ('0' <= c && c <= '9')
{
++i;
if (i == length)
return formatError(idx, i);
while ('0' <= format[i] && format[i] <= '9')
{
++i;
if (i == length)
return formatError(idx, i);
}
}
}
/* Read the specifier
*/
char genSpec;
Format specifier = parseGenericFormatSpecifier(format, i, genSpec);
if (specifier == Format_error)
return formatError(idx, i);
switch (genSpec)
{
case 'c':
case 's':
if (hash || zero)
return formatError(idx, i);
break;
case 'd':
case 'i':
if (hash)
return formatError(idx, i);
break;
case 'n':
if (hash || zero || precision || width || flags)
return formatError(idx, i);
break;
default:
break;
}
idx = i;
return specifier; // success
}
/*******************************************/
static Expression *getNextPrintfArg(const Loc &loc, Expressions &args, size_t &n,
size_t gnu_m_count, bool &skip)
{
if (n == args.length)
{
if (args.length < (n + 1) - gnu_m_count)
deprecation(loc, "more format specifiers than %d arguments", (int)n);
else
skip = true;
return NULL;
}
return args[n++];
}
static void errorPrintfFormat(const char *prefix, DString &slice, Expression *arg,
const char *texpect, Type *tactual)
{
deprecation(arg->loc, "%sargument `%s` for format specification `\"%.*s\"` must be `%s`, not `%s`",
prefix ? prefix : "", arg->toChars(), (int)slice.length, slice.ptr, texpect, tactual->toChars());
}
/******************************************
* Check that arguments to a printf format string are compatible
* with that string. Issue errors for incompatibilities.
*
* Follows the C99 specification for printf.
*
* Takes a generous, rather than strict, view of compatiblity.
* For example, an unsigned value can be formatted with a signed specifier.
*
* Diagnosed incompatibilities are:
*
* 1. incompatible sizes which will cause argument misalignment
* 2. deferencing arguments that are not pointers
* 3. insufficient number of arguments
* 4. struct arguments
* 5. array and slice arguments
* 6. non-pointer arguments to `s` specifier
* 7. non-standard formats
* 8. undefined behavior per C99
*
* Per the C Standard, extra arguments are ignored.
*
* No attempt is made to fix the arguments or the format string.
*
* Params:
* loc = location for error messages
* format = format string
* args = arguments to match with format string
* isVa_list = if a "v" function (format check only)
*
* Returns:
* `true` if errors occurred
* References:
* C99 7.19.6.1
* http://www.cplusplus.com/reference/cstdio/printf/
*/
bool checkPrintfFormat(const Loc &loc, const char *format, Expressions &args, bool isVa_list)
{
//printf("checkPrintFormat('%s')\n", format);
size_t n = 0; // index in args
size_t gnu_m_count = 0; // number of Format_GNU_m
const size_t format_length = strlen(format);
for (size_t i = 0; i < format_length;)
{
if (format[i] != '%')
{
++i;
continue;
}
bool widthStar = false;
bool precisionStar = false;
size_t j = i;
const Format fmt = parsePrintfFormatSpecifier(format, j, widthStar, precisionStar);
DString slice = DString(j - i, format + i);
i = j;
if (fmt == Format_percent)
continue; // "%%", no arguments
if (isVa_list)
{
// format check only
if (fmt == Format_error)
deprecation(loc, "format specifier `\"%.*s\"` is invalid", (int)slice.length, slice.ptr);
continue;
}
if (fmt == Format_GNU_m)
++gnu_m_count;
if (widthStar)
{
bool skip = false;
Expression *e = getNextPrintfArg(loc, args, n, gnu_m_count, skip);
if (skip)
continue;
if (!e)
return true;
Type *t = e->type->toBasetype();
if (t->ty != Tint32 && t->ty != Tuns32)
errorPrintfFormat("width ", slice, e, "int", t);
}
if (precisionStar)
{
bool skip = false;
Expression *e = getNextPrintfArg(loc, args, n, gnu_m_count, skip);
if (skip)
continue;
if (!e)
return true;
Type *t = e->type->toBasetype();
if (t->ty != Tint32 && t->ty != Tuns32)
errorPrintfFormat("precision ", slice, e, "int", t);
}
bool skip = false;
Expression *e = getNextPrintfArg(loc, args, n, gnu_m_count, skip);
if (skip)
continue;
if (!e)
return true;
Type *t = e->type->toBasetype();
Type *tnext = t->nextOf();
const unsigned c_longsize = target.c.longsize;
const unsigned ptrsize = target.ptrsize;
// Types which are promoted to int are allowed.
// Spec: C99 6.5.2.2.7
switch (fmt)
{
case Format_u: // unsigned int
case Format_d: // int
if (t->ty != Tint32 && t->ty != Tuns32)
errorPrintfFormat(NULL, slice, e, fmt == Format_u ? "uint" : "int", t);
break;
case Format_hhu: // unsigned char
case Format_hhd: // signed char
if (t->ty != Tint32 && t->ty != Tuns32 && t->ty != Tint8 && t->ty != Tuns8)
errorPrintfFormat(NULL, slice, e, fmt == Format_hhu ? "ubyte" : "byte", t);
break;
case Format_hu: // unsigned short int
case Format_hd: // short int
if (t->ty != Tint32 && t->ty != Tuns32 && t->ty != Tint16 && t->ty != Tuns16)
errorPrintfFormat(NULL, slice, e, fmt == Format_hu ? "ushort" : "short", t);
break;
case Format_lu: // unsigned long int
case Format_ld: // long int
if (!(t->isintegral() && t->size() == c_longsize))
{
if (fmt == Format_lu)
errorPrintfFormat(NULL, slice, e, (c_longsize == 4 ? "uint" : "ulong"), t);
else
errorPrintfFormat(NULL, slice, e, (c_longsize == 4 ? "int" : "long"), t);
}
break;
case Format_llu: // unsigned long long int
case Format_lld: // long long int
if (t->ty != Tint64 && t->ty != Tuns64)
errorPrintfFormat(NULL, slice, e, fmt == Format_llu ? "ulong" : "long", t);
break;
case Format_ju: // uintmax_t
case Format_jd: // intmax_t
if (t->ty != Tint64 && t->ty != Tuns64)
{
if (fmt == Format_ju)
errorPrintfFormat(NULL, slice, e, "core.stdc.stdint.uintmax_t", t);
else
errorPrintfFormat(NULL, slice, e, "core.stdc.stdint.intmax_t", t);
}
break;
case Format_zd: // size_t
if (!(t->isintegral() && t->size() == ptrsize))
errorPrintfFormat(NULL, slice, e, "size_t", t);
break;
case Format_td: // ptrdiff_t
if (!(t->isintegral() && t->size() == ptrsize))
errorPrintfFormat(NULL, slice, e, "ptrdiff_t", t);
break;
case Format_GNU_a: // Format_GNU_a is only for scanf
case Format_lg:
case Format_g: // double
if (t->ty != Tfloat64 && t->ty != Timaginary64)
errorPrintfFormat(NULL, slice, e, "double", t);
break;
case Format_Lg: // long double
if (t->ty != Tfloat80 && t->ty != Timaginary80)
errorPrintfFormat(NULL, slice, e, "real", t);
break;
case Format_p: // pointer
if (t->ty != Tpointer && t->ty != Tnull && t->ty != Tclass && t->ty != Tdelegate && t->ty != Taarray)
errorPrintfFormat(NULL, slice, e, "void*", t);
break;
case Format_n: // pointer to int
if (!(t->ty == Tpointer && tnext->ty == Tint32))
errorPrintfFormat(NULL, slice, e, "int*", t);
break;
case Format_ln: // pointer to long int
if (!(t->ty == Tpointer && tnext->isintegral() && !tnext->isunsigned() && tnext->size() == c_longsize))
errorPrintfFormat(NULL, slice, e, (c_longsize == 4 ? "int*" : "long*"), t);
break;
case Format_lln: // pointer to long long int
if (!(t->ty == Tpointer && tnext->ty == Tint64))
errorPrintfFormat(NULL, slice, e, "long*", t);
break;
case Format_hn: // pointer to short
if (!(t->ty == Tpointer && tnext->ty == Tint16))
errorPrintfFormat(NULL, slice, e, "short*", t);
break;
case Format_hhn: // pointer to signed char
if (!(t->ty == Tpointer && tnext->ty == Tint16))
errorPrintfFormat(NULL, slice, e, "byte*", t);
break;
case Format_jn: // pointer to intmax_t
if (!(t->ty == Tpointer && tnext->ty == Tint64))
errorPrintfFormat(NULL, slice, e, "core.stdc.stdint.intmax_t*", t);
break;
case Format_zn: // pointer to size_t
if (!(t->ty == Tpointer && tnext->isintegral() && tnext->isunsigned() && tnext->size() == ptrsize))
errorPrintfFormat(NULL, slice, e, "size_t*", t);
break;
case Format_tn: // pointer to ptrdiff_t
if (!(t->ty == Tpointer && tnext->isintegral() && !tnext->isunsigned() && tnext->size() == ptrsize))
errorPrintfFormat(NULL, slice, e, "ptrdiff_t*", t);
break;
case Format_c: // char
if (t->ty != Tint32 && t->ty != Tuns32)
errorPrintfFormat(NULL, slice, e, "char", t);
break;
case Format_lc: // wint_t
if (t->ty != Tint32 && t->ty != Tuns32)
errorPrintfFormat(NULL, slice, e, "wchar_t", t);
break;
case Format_s: // pointer to char string
if (!(t->ty == Tpointer && (tnext->ty == Tchar || tnext->ty == Tint8 || tnext->ty == Tuns8)))
errorPrintfFormat(NULL, slice, e, "char*", t);
break;
case Format_ls: // pointer to wchar_t string
{
if (!(t->ty == Tpointer && tnext == target.c.twchar_t))
errorPrintfFormat(NULL, slice, e, "wchar_t*", t);
break;
}
case Format_error:
deprecation(loc, "format specifier `\"%.*s\"` is invalid", (int)slice.length, slice.ptr);
break;
case Format_GNU_m:
break; // not assert(0) because it may go through it if there are extra arguments
case Format_percent:
default:
assert(0);
}
}
return false;
}
/*******************************************/
static Expression *getNextScanfArg(const Loc &loc, Expressions &args, size_t &n, bool asterisk)
{
if (n == args.length)
{
if (!asterisk)
deprecation(loc, "more format specifiers than %d arguments", (int)n);
return NULL;
}
return args[n++];
}
static void errorScanfFormat(const char *prefix, DString &slice,
Expression *arg, const char *texpect, Type *tactual)
{
deprecation(arg->loc, "%sargument `%s` for format specification `\"%.*s\"` must be `%s`, not `%s`",
prefix ? prefix : "", arg->toChars(), (int)slice.length, slice.ptr, texpect, tactual->toChars());
}
/******************************************
* Check that arguments to a scanf format string are compatible
* with that string. Issue errors for incompatibilities.
*
* Follows the C99 specification for scanf.
*
* Takes a generous, rather than strict, view of compatiblity.
* For example, an unsigned value can be formatted with a signed specifier.
*
* Diagnosed incompatibilities are:
*
* 1. incompatible sizes which will cause argument misalignment
* 2. deferencing arguments that are not pointers
* 3. insufficient number of arguments
* 4. struct arguments
* 5. array and slice arguments
* 6. non-standard formats
* 7. undefined behavior per C99
*
* Per the C Standard, extra arguments are ignored.
*
* No attempt is made to fix the arguments or the format string.
*
* Params:
* loc = location for error messages
* format = format string
* args = arguments to match with format string
* isVa_list = if a "v" function (format check only)
*
* Returns:
* `true` if errors occurred
* References:
* C99 7.19.6.2
* http://www.cplusplus.com/reference/cstdio/scanf/
*/
bool checkScanfFormat(const Loc &loc, const char *format, Expressions &args, bool isVa_list)
{
size_t n = 0;
const size_t format_length = strlen(format);
for (size_t i = 0; i < format_length;)
{
if (format[i] != '%')
{
++i;
continue;
}
bool asterisk = false;
size_t j = i;
const Format fmt = parseScanfFormatSpecifier(format, j, asterisk);
DString slice = DString(j - i, format + i);
i = j;
if (fmt == Format_percent || asterisk)
continue; // "%%", "%*": no arguments
if (isVa_list)
{
// format check only
if (fmt == Format_error)
deprecation(loc, "format specifier `\"%.*s\"` is invalid", (int)slice.length, slice.ptr);
continue;
}
Expression *e = getNextScanfArg(loc, args, n, asterisk);
if (!e)
return true;
Type *t = e->type->toBasetype();
Type *tnext = t->nextOf();
const unsigned c_longsize = target.c.longsize;
const unsigned ptrsize = target.ptrsize;
switch (fmt)
{
case Format_n:
case Format_d: // pointer to int
if (!(t->ty == Tpointer && tnext->ty == Tint32))
errorScanfFormat(NULL, slice, e, "int*", t);
break;
case Format_hhn:
case Format_hhd: // pointer to signed char
if (!(t->ty == Tpointer && tnext->ty == Tint16))
errorScanfFormat(NULL, slice, e, "byte*", t);
break;
case Format_hn:
case Format_hd: // pointer to short
if (!(t->ty == Tpointer && tnext->ty == Tint16))
errorScanfFormat(NULL, slice, e, "short*", t);
break;
case Format_ln:
case Format_ld: // pointer to long int
if (!(t->ty == Tpointer && tnext->isintegral() && tnext->size() == c_longsize))
errorScanfFormat(NULL, slice, e, (c_longsize == 4 ? "int*" : "long*"), t);
break;
case Format_lln:
case Format_lld: // pointer to long long int
if (!(t->ty == Tpointer && tnext->ty == Tint64))
errorScanfFormat(NULL, slice, e, "long*", t);
break;
case Format_jn:
case Format_jd: // pointer to intmax_t
if (!(t->ty == Tpointer && tnext->ty == Tint64))
errorScanfFormat(NULL, slice, e, "core.stdc.stdint.intmax_t*", t);
break;
case Format_zn:
case Format_zd: // pointer to size_t
if (!(t->ty == Tpointer && tnext->isintegral() && tnext->isunsigned() && tnext->size() == ptrsize))
errorScanfFormat(NULL, slice, e, "size_t*", t);
break;
case Format_tn:
case Format_td: // pointer to ptrdiff_t
if (!(t->ty == Tpointer && tnext->isintegral() && !tnext->isunsigned() && tnext->size() == ptrsize))
errorScanfFormat(NULL, slice, e, "ptrdiff_t*", t);
break;
case Format_u: // pointer to unsigned int
if (!(t->ty == Tpointer && tnext->ty == Tuns32))
errorScanfFormat(NULL, slice, e, "uint*", t);
break;
case Format_hhu: // pointer to unsigned char
if (!(t->ty == Tpointer && tnext->ty == Tuns8))
errorScanfFormat(NULL, slice, e, "ubyte*", t);
break;
case Format_hu: // pointer to unsigned short int
if (!(t->ty == Tpointer && tnext->ty == Tuns16))
errorScanfFormat(NULL, slice, e, "ushort*", t);
break;
case Format_lu: // pointer to unsigned long int
if (!(t->ty == Tpointer && tnext->isintegral() && tnext->isunsigned() && tnext->size() == c_longsize))
errorScanfFormat(NULL, slice, e, (c_longsize == 4 ? "uint*" : "ulong*"), t);
break;
case Format_llu: // pointer to unsigned long long int
if (!(t->ty == Tpointer && tnext->ty == Tuns64))
errorScanfFormat(NULL, slice, e, "ulong*", t);
break;
case Format_ju: // pointer to uintmax_t
if (!(t->ty == Tpointer && tnext->ty == Tuns64))
errorScanfFormat(NULL, slice, e, "ulong*", t);
break;
case Format_g: // pointer to float
if (!(t->ty == Tpointer && tnext->ty == Tfloat32))
errorScanfFormat(NULL, slice, e, "float*", t);
break;
case Format_lg: // pointer to double
if (!(t->ty == Tpointer && tnext->ty == Tfloat64))
errorScanfFormat(NULL, slice, e, "double*", t);
break;
case Format_Lg: // pointer to long double
if (!(t->ty == Tpointer && tnext->ty == Tfloat80))
errorScanfFormat(NULL, slice, e, "real*", t);
break;
case Format_GNU_a:
case Format_GNU_m:
case Format_c:
case Format_s: // pointer to char string
if (!(t->ty == Tpointer && (tnext->ty == Tchar || tnext->ty == Tint8 || tnext->ty == Tuns8)))
errorScanfFormat(NULL, slice, e, "char*", t);
break;
case Format_lc:
case Format_ls: // pointer to wchar_t string
{
if (!(t->ty == Tpointer && tnext == target.c.twchar_t))
errorScanfFormat(NULL, slice, e, "wchar_t*", t);
break;
}
case Format_p: // double pointer
if (!(t->ty == Tpointer && tnext->ty == Tpointer))
errorScanfFormat(NULL, slice, e, "void**", t);
break;
case Format_error:
deprecation(loc, "format specifier `\"%.*s\"` is invalid", (int)slice.length, slice.ptr);
break;
case Format_percent:
default:
assert(0);
}
}
return false;
}
|