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