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