check-format.pl: Fix report on constant on LHS of comparison/assignment
[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 int ret, was_NULL = *certs == NULL;
192
193 const OPTIONS passwd_options[] = {
194     {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
195 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
196     {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
197 #endif
198     OPT_R_OPTIONS,
199
200     {NULL}
201 };
202
203 typedef * d(int)
204     x;
205 typedef (int)
206 x;
207 typedef (int)*()
208     x;
209 typedef *int *
210 x;
211 typedef OSSL_CMP_MSG *(*cmp_srv_process_cb_t)
212     (OSSL_CMP_SRV_CTX *ctx, OSSL_CMP_MSG *msg)
213     xx;
214 int f()
215 {
216     c;
217     if (1) {
218         c;
219     }
220     c;
221     if (1)
222         if (2)
223         { /*@ brace after 'if' not on same line just to construct case */
224             c;
225         }
226     e;
227     const usign = {
228                    0xDF,
229                    {
230                     dd
231                    },
232                    dd
233     };
234     const unsign = {
235                     0xDF, {
236                            dd
237                     },
238                     dd
239     };
240 }
241 const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = {
242                                                                0xDF,
243 };
244 const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] =
245     {
246      0xDF,
247     };
248 typedef
249 int
250 a;
251
252 typedef
253 struct
254 {
255     int a;
256 } b;
257 typedef enum {
258               w = 0
259 } e_type;
260 typedef struct {
261     enum {
262           w = 0
263     } e_type;
264     enum {
265           w = 0
266     } e_type;
267 } e;
268 struct s_type {
269     enum e_type {
270                  w = 0
271     };
272 };
273 struct s_type
274 {
275     enum e_type {
276                  w = 0
277     };
278     enum e2_type {
279                   w = 0
280     };
281 };
282
283 #define X  1          + 1
284 #define Y  /* .. */ 2 + 2
285 #define Z  3          + 3 * (*a++)
286
287 static varref cmp_vars[] = { /* comment.  comment?  comment!  */
288     {&opt_config}, {&opt_section},
289
290     {&opt_server}, {&opt_proxy}, {&opt_path},
291 };
292
293 #define SWITCH(x)                               \
294     switch (x) {                                \
295     case 0:                                     \
296         break;                                  \
297     default:                                    \
298         break;                                  \
299     }
300
301 #define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
302                                  DEFAULT, NEW, FREE) \
303     static int execute_CTX_##SETN##_##GETN##_##FIELD( \
304                                                      TEST_FIXTURE *fixture) \
305     { \
306         CTX *ctx = fixture->ctx; \
307         int (*set_fn)(CTX *ctx, TYPE) = \
308             (int (*)(CTX *ctx, TYPE))PREFIX##_##SETN##_##FIELD; \
309         /* comment */ \
310     }
311
312 union un var; /* struct/union/enum in variable type */
313 struct provider_store_st *f() /* struct/union/enum in function return type */
314 {
315 }
316 static void f(struct pem_pass_data *data) /* struct/union/enum in arg list */
317 {
318 }
319
320 static void *fun(void)
321 {
322     if (pem_name != NULL)
323         /* comment */
324         return NULL;
325
326 label0:
327  label1: /* allow special indent 1 for label at outermost level in body */
328     do {
329     label2:
330         size_t available_len, data_len;
331         const char *curr = txt, *next = txt;
332         char *tmp;
333
334         {
335         label3:
336         }
337     } while (1);
338
339     char *intraline_string_with_comment_delimiters_and_dbl_space = "1  /*1";
340     char *multiline_string_with_comment_delimiters_and_dbl_space = "1  /*1\
341 2222222\'22222222222222222\"222222222" "33333  /*3333333333" "44  /*44444444444\
342 55555555555555\
343 6666";
344 }
345
346 ASN1_CHOICE(OSSL_CRMF_POPO) = {
347     ASN1_IMP(OSSL_CRMF_POPO, value.raVerified, ASN1_NULL, 0),
348     ASN1_EXP(OSSL_CRMF_POPO, value.keyAgreement, OSSL_CRMF_POPOPRIVKEY, 3)
349 } ASN1_CHOICE_END(OSSL_CRMF_POPO)
350 IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPO)
351
352 ASN1_ADB(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) = {
353     ADB_ENTRY(NID_id_regCtrl_regToken,
354               ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE,
355                           value.regToken, ASN1_UTF8STRING)),
356 } ASN1_ADB_END(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, 0, type, 0,
357                &attributetypeandvalue_default_tt, NULL);
358
359 ASN1_ITEM_TEMPLATE(OSSL_CRMF_MSGS) =
360     ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
361                           OSSL_CRMF_MSGS, OSSL_CRMF_MSG)
362 ASN1_ITEM_TEMPLATE_END(OSSL_CRMF_MSGS)
363
364 void f_looong_body_200()
365 { /* function body length up to 200 lines accepted */
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 void f_looong_body_201()
569 { /* function body length > 200 lines, but LONG BODY marker present */
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 }