Fix X509_PUBKEY_cmp(), move to crypto/x509/x_pubkey.c, rename, export, and document it
[openssl.git] / util / check-format-test-negatives.c
1 /*
2  * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright Nokia 2007-2019
4  * Copyright Siemens AG 2015-2019
5  *
6  * Licensed under the Apache License 2.0 (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11
12 /*
13  * A collection of test cases where check-format.pl should not report issues.
14  * There are some known false positives, though, which are marked below.
15  */
16
17 /*-
18  * allow double space  in format-tagged multi-line comment
19  */
20 int f(void) /*
21              * trailing multi-line comment
22              */
23 {
24     if (ctx == NULL) { /* non-leading intra-line comment */
25         if (pem_name != NULL)
26             /* entire-line comment indent usually like for the following line */
27             return NULL; /* hanging indent also for this line after comment */
28         /* leading comment has same indentation as normal code */ stmt;
29         /* entire-line comment may have same indent as normal code */
30     }
31
32 #if X
33     if (1) /* bad style: just part of control structure depends on #if */
34 #else
35     if (2) /*@ resulting false positive */
36 #endif
37         c; /*@ resulting false positive */
38
39     if (1)
40         if (2)
41             c;
42         else
43             e;
44     else
45         f;
46     do
47         do
48             2;
49         while (1);
50     while (2);
51
52     if (1)
53         f(a, b);
54     do
55         1; while (2); /*@ more than one stmt just to construct case */
56     if (1)
57         f(a, b);
58     else
59         do
60             1;
61         while (2);
62     if (1)
63         f(a, b);
64     else do /*@ (non-brace) code before 'do' just to construct case */
65              1;
66         while (2);
67     f1234(a,
68           b); do /*@ (non-brace) code before 'do' just to construct case */
69                   1;
70     while (2);
71     if (1)
72         f(a,
73           b); do /*@ (non-brace) code before 'do' just to construct case */
74                   1;
75     while (2);
76     if (1)
77         f(a, b);
78     else
79         do f(c, c); /*@ (non-brace) code after 'do' just to construct case */
80         while (2);
81
82     if (1)
83         f(a, b);
84     else
85         return;
86     if (1)
87         f(a,
88           b); else /*@ (non-brace) code before 'else' just to construct case */
89         do
90             1;
91         while (2);
92
93     if (1)
94     { /*@ brace after 'if' not on same line just to construct case */
95         c;
96         d;
97     }
98     /* this comment is correctly indented if it refers to the following line */
99     d;
100
101     if (1) {
102         2;
103     } else /*@ no brace after 'else' just to construct case */
104         3;
105     do {
106     } while (x);
107     if (1) {
108         2;
109     } else {
110         3;
111     }
112     if (4)
113         5;
114     else
115         6;
116
117     if (1) {
118         if (2) {
119         case MAC_TYPE_MAC:
120             {
121                 EVP_MAC_CTX *new_mac_ctx;
122
123                 if (ctx->pkey == NULL)
124                     return 0;
125             }
126             break;
127         default:
128             /* This should be dead code */
129             return 0;
130         }
131     }
132     if (expr_line1
133         == expr_line2
134             && expr_line3) {
135         c1;
136     } else {
137         c;
138         d;
139     }
140     if (expr_line1
141         == expr_line2
142             && expr_line3)
143         hanging_stmt;
144 }
145
146 const OPTIONS passwd_options[] = {
147     {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
148 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
149     {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
150 #endif
151     OPT_R_OPTIONS,
152
153     {NULL}
154 };
155
156 typedef * d(int)
157     x;
158 typedef (int)
159 x;
160 typedef (int)*()
161     x;
162 typedef *int *
163 x;
164 typedef OSSL_CMP_MSG *(*cmp_srv_process_cb_t)
165     (OSSL_CMP_SRV_CTX *ctx, OSSL_CMP_MSG *msg)
166     xx;
167 int f()
168 {
169     c;
170     if (1) {
171         c;
172     }
173     c;
174     if (1)
175         if (2)
176         { /*@ brace after 'if' not on same line just to construct case */
177             c;
178         }
179     e;
180     const usign = {
181                    0xDF,
182                    {
183                     dd
184                    },
185                    dd
186     };
187     const unsign = {
188                     0xDF, {
189                            dd
190                     },
191                     dd
192     };
193 }
194 const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = {
195                                                                0xDF,
196 };
197 const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] =
198     {
199      0xDF,
200     };
201 typedef
202 int
203 a;
204
205 typedef
206 struct
207 {
208     int a;
209 } b;
210 typedef enum {
211               w = 0
212 } e_type;
213 typedef struct {
214     enum {
215           w = 0
216     } e_type;
217     enum {
218           w = 0
219     } e_type;
220 } e;
221 struct s_type {
222     enum e_type {
223                  w = 0
224     };
225 };
226 struct s_type
227 {
228     enum e_type {
229                  w = 0
230     };
231     enum e2_type {
232                   w = 0
233     };
234 };
235
236 #define X  1          + 1
237 #define Y  /* .. */ 2 + 2
238 #define Z  3          + 3
239
240 static varref cmp_vars[] = { /* comment */
241     {&opt_config}, {&opt_section},
242
243     {&opt_server}, {&opt_proxy}, {&opt_path},
244 };
245
246 #define SWITCH(x)                               \
247     switch (x) {                                \
248     case 0:                                     \
249         break;                                  \
250     default:                                    \
251         break;                                  \
252     }
253
254 #define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
255                                  DEFAULT, NEW, FREE) \
256     static int execute_CTX_##SETN##_##GETN##_##FIELD( \
257                                                      TEST_FIXTURE *fixture) \
258     { \
259         CTX *ctx = fixture->ctx; \
260         int (*set_fn)(CTX *ctx, TYPE) = \
261             (int (*)(CTX *ctx, TYPE))PREFIX##_##SETN##_##FIELD; \
262         /* comment */ \
263     }
264
265 /* 'struct' in function header */
266 static int f(struct pem_pass_data *pass_data)
267 {
268     if (pass_data == NULL)
269         return 0;
270 }
271
272 static void *fun(void)
273 {
274     if (pem_name != NULL)
275         /* comment */
276         return NULL;
277
278     do {
279         size_t available_len, data_len;
280         const char *curr = txt, *next = txt;
281         char *tmp;
282     } while (1);
283
284     char *intraline_string_with_comment_delimiters_and_dbl_space = "1  /*1";
285     char *multiline_string_with_comment_delimiters_and_dbl_space = "1  /*1\
286 2222222\'22222222222222222\"222222222" "33333  /*3333333333" "44  /*44444444444\
287 55555555555555\
288 6666";
289 }
290
291 ASN1_CHOICE(OSSL_CRMF_POPO) = {
292     ASN1_IMP(OSSL_CRMF_POPO, value.raVerified, ASN1_NULL, 0),
293     ASN1_EXP(OSSL_CRMF_POPO, value.keyAgreement, OSSL_CRMF_POPOPRIVKEY, 3)
294 } ASN1_CHOICE_END(OSSL_CRMF_POPO)
295 IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPO)
296
297 ASN1_ADB(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) = {
298     ADB_ENTRY(NID_id_regCtrl_regToken,
299               ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE,
300                           value.regToken, ASN1_UTF8STRING)),
301 } ASN1_ADB_END(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, 0, type, 0,
302                &attributetypeandvalue_default_tt, NULL);
303
304 ASN1_ITEM_TEMPLATE(OSSL_CRMF_MSGS) =
305     ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
306                           OSSL_CRMF_MSGS, OSSL_CRMF_MSG)
307 ASN1_ITEM_TEMPLATE_END(OSSL_CRMF_MSGS)
308
309 void f_looong_body_200()
310 { /* function body length up to 200 lines accepted */
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 void f_looong_body_201()
514 { /* function body length > 200 lines, but LONG BODY marker present */
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 }