Document SSL_CTX_get_ssl_method
[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     for (;;) ; /* should not trigger: space before ';' */
74  lab: ;  /* should not trigger: space before ';' */
75
76 #if X
77     if (1) /* bad style: just part of control structure depends on #if */
78 #else
79     if (2) /*@ resulting false positive */
80 #endif
81         c; /*@ resulting false positive */
82
83     if (1)
84         if (2)
85             c;
86         else
87             e;
88     else
89         f;
90     do
91         do
92             2;
93         while (1);
94     while (2);
95
96     if (1)
97         f(a, b);
98     do
99         1; while (2); /*@ more than one stmt just to construct case */
100     if (1)
101         f(a, b);
102     else
103         do
104             1;
105         while (2);
106     if (1)
107         f(a, b);
108     else do /*@ (non-brace) code before 'do' just to construct case */
109              1;
110         while (2);
111     f1234(a,
112           b); do /*@ (non-brace) code before 'do' just to construct case */
113                   1;
114     while (2);
115     if (1)
116         f(a,
117           b); do /*@ (non-brace) code before 'do' just to construct case */
118                   1;
119     while (2);
120     if (1)
121         f(a, b);
122     else
123         do f(c, c); /*@ (non-brace) code after 'do' just to construct case */
124         while (2);
125
126     if (1)
127         f(a, b);
128     else
129         return;
130     if (1)
131         f(a,
132           b); else /*@ (non-brace) code before 'else' just to construct case */
133         do
134             1;
135         while (2);
136
137     if (1)
138     { /*@ brace after 'if' not on same line just to construct case */
139         c;
140         d;
141     }
142     /* this comment is correctly indented if it refers to the following line */
143     d;
144
145     if (1) {
146         2;
147     } else /*@ no brace after 'else' just to construct case */
148         3;
149     do {
150     } while (x);
151     if (1) {
152         2;
153     } else {
154         3;
155     }
156     if (4)
157         5;
158     else
159         6;
160
161     if (1) {
162         if (2) {
163         case MAC_TYPE_MAC:
164             {
165                 EVP_MAC_CTX *new_mac_ctx;
166
167                 if (ctx->pkey == NULL)
168                     return 0;
169             }
170             break;
171         default:
172             /* This should be dead code */
173             return 0;
174         }
175     }
176     if (expr_line1
177         == expr_line2
178             && expr_line3) {
179         c1;
180     } else {
181         c;
182         d;
183     }
184     if (expr_line1
185         == expr_line2
186             && expr_line3)
187         hanging_stmt;
188 }
189
190 /* should not trigger: constant on LHS of comparison or assignment operator */
191 X509 *x509 = NULL;
192 int y = a + 1 < b;
193 int ret, was_NULL = *certs == NULL;
194
195 /* should not trigger: no space before binary ... operator */
196 float z = 1e-6 * (-1) * b[+6] * 1e+1 * (a)->f * (long)+1
197     - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9);
198 struct st = {-1, 0};
199
200 const OPTIONS passwd_options[] = {
201     {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
202 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
203     {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
204 #endif
205     OPT_R_OPTIONS,
206
207     {NULL}
208 };
209
210 typedef * d(int)
211     x;
212 typedef (int)
213 x;
214 typedef (int)*()
215     x;
216 typedef *int *
217 x;
218 typedef OSSL_CMP_MSG *(*cmp_srv_process_cb_t)
219     (OSSL_CMP_SRV_CTX *ctx, OSSL_CMP_MSG *msg)
220     xx;
221 int f()
222 {
223     c;
224     if (1) {
225         c;
226     }
227     c;
228     if (1)
229         if (2)
230         { /*@ brace after 'if' not on same line just to construct case */
231             c;
232         }
233     e;
234     const usign = {
235                    0xDF,
236                    {
237                     dd
238                    },
239                    dd
240     };
241     const unsign = {
242                     0xDF, {
243                            dd
244                     },
245                     dd
246     };
247 }
248 const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = {
249                                                                0xDF,
250 };
251 const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] =
252     {
253      0xDF,
254     };
255 typedef
256 int
257 a;
258
259 typedef
260 struct
261 {
262     int a;
263 } b;
264 typedef enum {
265               w = 0
266 } e_type;
267 typedef struct {
268     enum {
269           w = 0
270     } e_type;
271     enum {
272           w = 0
273     } e_type;
274 } e;
275 struct s_type {
276     enum e_type {
277                  w = 0
278     };
279 };
280 struct s_type
281 {
282     enum e_type {
283                  w = 0
284     };
285     enum e2_type {
286                   w = 0
287     };
288 };
289
290 #define X  1          + 1
291 #define Y  /* .. */ 2 + 2
292 #define Z  3          + 3 * (*a++)
293
294 static varref cmp_vars[] = { /* comment.  comment?  comment!  */
295     {&opt_config}, {&opt_section},
296
297     {&opt_server}, {&opt_proxy}, {&opt_path},
298 };
299
300 #define SWITCH(x)                               \
301     switch (x) {                                \
302     case 0:                                     \
303         break;                                  \
304     default:                                    \
305         break;                                  \
306     }
307
308 #define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
309                                  DEFAULT, NEW, FREE) \
310     static int execute_CTX_##SETN##_##GETN##_##FIELD( \
311                                                      TEST_FIXTURE *fixture) \
312     { \
313         CTX *ctx = fixture->ctx; \
314         int (*set_fn)(CTX *ctx, TYPE) = \
315             (int (*)(CTX *ctx, TYPE))PREFIX##_##SETN##_##FIELD; \
316         /* comment */ \
317     }
318
319 union un var; /* struct/union/enum in variable type */
320 struct provider_store_st *f() /* struct/union/enum in function return type */
321 {
322 }
323 static void f(struct pem_pass_data *data) /* struct/union/enum in arg list */
324 {
325 }
326
327 static void *fun(void)
328 {
329     if (pem_name != NULL)
330         /* comment */
331         return NULL;
332
333 label0:
334  label1: /* allow special indent 1 for label at outermost level in body */
335     do {
336     label2:
337         size_t available_len, data_len;
338         const char *curr = txt, *next = txt;
339         char *tmp;
340
341         {
342         label3:
343         }
344     } while (1);
345
346     char *intraline_string_with_comment_delimiters_and_dbl_space = "1  /*1";
347     char *multiline_string_with_comment_delimiters_and_dbl_space = "1  /*1\
348 2222222\'22222222222222222\"222222222" "33333  /*3333333333" "44  /*44444444444\
349 55555555555555\
350 6666";
351 }
352
353 ASN1_CHOICE(OSSL_CRMF_POPO) = {
354     ASN1_IMP(OSSL_CRMF_POPO, value.raVerified, ASN1_NULL, 0),
355     ASN1_EXP(OSSL_CRMF_POPO, value.keyAgreement, OSSL_CRMF_POPOPRIVKEY, 3)
356 } ASN1_CHOICE_END(OSSL_CRMF_POPO)
357 IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPO)
358
359 ASN1_ADB(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) = {
360     ADB_ENTRY(NID_id_regCtrl_regToken,
361               ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE,
362                           value.regToken, ASN1_UTF8STRING)),
363 } ASN1_ADB_END(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, 0, type, 0,
364                &attributetypeandvalue_default_tt, NULL);
365
366 ASN1_ITEM_TEMPLATE(OSSL_CRMF_MSGS) =
367     ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
368                           OSSL_CRMF_MSGS, OSSL_CRMF_MSG)
369 ASN1_ITEM_TEMPLATE_END(OSSL_CRMF_MSGS)
370
371 void f_looong_body_200()
372 { /* function body length up to 200 lines accepted */
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 void f_looong_body_201()
576 { /* function body length > 200 lines, but LONG BODY marker present */
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 }