xref: /freebsd/crypto/openssl/util/check-format-test-negatives.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /*
2  * Copyright 2007-2025 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 using /*@
14  */
15 
16 #include <errno.h> /* should not report whitespace nits within <...> */
17 #define F                                       \
18     void f()                                    \
19     {                                           \
20         int i;                                  \
21         int j;                                  \
22                                                 \
23         return;                                 \
24     }
25 
26 /* allow extra  SPC in single-line comment */
27 /*
28  * allow extra  SPC in regular multi-line comment
29  */
30 /*-
31  * allow extra  SPC in format-tagged multi-line comment
32  */
33 /** allow extra '*' in comment opening */
34 /*! allow extra '!' in comment opening */
35 /*
36  ** allow "**" as first non-space chars of a line within multi-line comment
37  */
38 
39 int f(void) /*
40              * trailing multi-line comment
41              */
42 {
43     typedef int INT;
44     void v;
45     short b;
46     char c;
47     signed s;
48     unsigned u;
49     int i;
50     long l;
51     float f;
52     double d;
53     enum {} enu;
54     struct {} stru;
55     union {} un;
56     auto a;
57     extern e;
58     static int stat;
59     const int con;
60     volatile int vola;
61     register int reg;
62     OSSL_x y, *p = params;
63     int params[];
64     OSSL_PARAM * (* params []) [MAX + 1];
65     XY *(* fn)(int a, char b);
66     /*
67      * multi-line comment should not disturb detection of local decls
68      */
69     BIO1 ***b;
70     /* intra-line comment should not disturb detection of local decls */
71     unsigned k;
72 
73     /* intra-line comment should not disturb detection of end of local decls */
74 
75     {
76         int x; /* just decls in block */
77     }
78     if (p != (unsigned char *)
79         &(ctx->tmp[0])) {
80         i -= (p - (unsigned char *) /* do not confuse with var decl */
81               &(ctx->tmp[0]));
82     }
83     {
84         ctx->buf_off = 0; /* do not confuse with var decl */
85         return 0;
86     }
87     {
88         ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
89                                        (unsigned char *)ctx->tmp, /* no decl */
90                                        ctx->tmp_len);
91     }
92     {
93         EVP_EncodeFinal(ctx->base64,
94                         (unsigned char *)ctx->buf, &(ctx->len)); /* no decl */
95         /* push out the bytes */
96         goto again;
97     }
98     {
99         f(1, (unsigned long)2); /* no decl */
100         x;
101     }
102     {
103         char *pass_str = get_passwd(opt_srv_secret, "x");
104 
105         if (pass_str != NULL) {
106             cleanse(opt_srv_secret);
107             res = OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)pass_str,
108                                                 strlen(pass_str));
109             clear_free(pass_str);
110         }
111     }
112 }
113 
114 int g(void)
115 {
116     if (ctx == NULL) {    /* non-leading end-of-line comment */
117         if (/* comment after '(' */ pem_name != NULL /* comment before ')' */)
118             /* entire-line comment indent usually like for the following line */
119             return NULL; /* hanging indent also for this line after comment */
120         /* leading comment has same indentation as normal code */ stmt;
121         /* entire-line comment may have same indent as normal code */
122     }
123     for (i = 0; i < n; i++)
124         for (; i < n; i++)
125             for (i = 0; ; i++)
126                 for (i = 0;; i++)
127                     for (i = 0; i < n; )
128                         for (i = 0; i < n;)
129                             ;
130     for (i = 0; ; )
131         for (i = 0; ;)
132             for (i = 0;; )
133                 for (i = 0;;)
134                     for (; i < n; )
135                         for (; j < n;)
136                             for (; ; i++)
137                                 for (;; i++)
138                                     ;
139     for (;;) /* the only variant allowed in case of "empty" for (...) */
140         ;
141     for (;;) ; /* should not trigger: space before ';' */
142  lab: ;  /* should not trigger: space before ';' */
143 
144 #if X
145     if (1) /* bad style: just part of control structure depends on #if */
146 #else
147     if (2) /*@ resulting false positive */
148 #endif
149         c; /*@ resulting false positive */
150 
151     if (1)
152         if (2)
153             c;
154         else
155             e;
156     else
157         f;
158     do
159         do
160             2;
161         while (1);
162     while (2);
163 
164     if (pcrl != NULL) {
165         1;
166         2;
167     } else if (pcrls != NULL) {
168         1;
169     }
170 
171     if (1)
172         f(a, b);
173     do
174         1; while (2); /*@ more than one stmt just to construct case */
175     if (1)
176         f(a, b);
177     else
178         do
179             1;
180         while (2);
181     if (1)
182         f(a, b);
183     else do /*@ (non-brace) code before 'do' just to construct case */
184              1;
185         while (2);
186     f1234(a,
187           b); do /*@ (non-brace) code before 'do' just to construct case */
188                   1;
189     while (2);
190     if (1)
191         f(a,
192           b); do /*@ (non-brace) code before 'do' just to construct case */
193                   1;
194     while (2);
195     if (1)
196         f(a, b);
197     else
198         do f(c, c); /*@ (non-brace) code after 'do' just to construct case */
199         while (2);
200 
201     if (1)
202         f(a, b);
203     else
204         return;
205     if (1)
206         f(a,
207           b); else /*@ (non-brace) code before 'else' just to construct case */
208         do
209             1;
210         while (2);
211 
212     if (1)
213     { /*@ brace after 'if' not on same line just to construct case */
214         c;
215         d;
216     }
217     /* this comment is correctly indented if it refers to the following line */
218     d;
219 
220     if (1) {
221         2;
222     } else /*@ no brace after 'else' just to construct case */
223         3;
224     do {
225     } while (x);
226     if (1) {
227         2;
228     } else {
229         3;
230     }
231     if (4)
232         5;
233     else
234         6;
235 
236     if (1) {
237         if (2) {
238         case MAC_TYPE_MAC:
239             {
240                 EVP_MAC_CTX *new_mac_ctx;
241 
242                 if (ctx->pkey == NULL)
243                     return 0;
244             }
245             break;
246         case 1: {
247             ;
248         }
249         default:
250             /* This should be dead code */
251             return 0;
252         }
253     }
254     if (expr_line1
255         == expr_line2
256             && expr_line3) {
257         c1;
258     } else {
259         c;
260         d;
261     }
262     if (expr_line1
263         == expr_line2
264             && expr_line3)
265         hanging_stmt;
266 }
267 
268 #define m1                           \
269     if (ctx == NULL)                 \
270         return 0;                    \
271     if (ossl_param_is_empty(params)) \
272         return 1;                    \
273 
274 #define m2                                                               \
275     do { /* should not be confused with function header followed by '{' */ \
276     } while (0)
277 
278 /* should not trigger: constant on LHS of comparison or assignment operator */
279 X509 *x509 = NULL;
280 int y = a + 1 < b;
281 int ret, was_NULL = *certs == NULL;
282 
283 /* should not trigger: missing space before ... */
284 float z = 1e-6 * (-1) * b[+6] * 1e+1 * (a)->f * (long)+1
285     - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9);
286 struct st = {-1, 0};
287 int x = (y <<= 1) + (z <= 5.0);
288 
289 const OPTIONS passwd_options[] = {
290     {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
291 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
292     {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
293 #endif
294     OPT_R_OPTIONS,
295 
296     {NULL}
297 };
298 
299 typedef bool (*LOG_cb_t)(int lineno, severity level, const char *msg);
300 typedef * d(int)
301     x;
302 typedef (int)
303 x;
304 typedef (int)*()
305     x;
306 typedef *int *
307 x;
308 typedef OSSL_CMP_MSG *(*cmp_srv_process_cb_t)
309     (OSSL_CMP_SRV_CTX *ctx, OSSL_CMP_MSG *msg)
310     xx;
311 
312 #define IF(cond) if (cond)
313 
314 _Pragma("GCC diagnostic push")
315 _Pragma("GCC diagnostic pop")
316 
317 #define CB_ERR_IF(cond, ctx, cert, depth, err) \
318     if ((cond) && ((depth) < 0 || verify_cb_cert(ctx, cert, depth, err) == 0)) \
319         return err
320 static int verify_cb_crl(X509_STORE_CTX *ctx, int err)
321 {
322     ctx->error = err;
323     return ctx->verify_cb(0, ctx);
324 }
325 
326 #ifdef CMP_FALLBACK_EST
327 # define CMP_FALLBACK_CERT_FILE "cert.pem"
328 #endif
329 
330 #define X509_OBJECT_get0_X509(obj)                                      \
331     ((obj) == NULL || (obj)->type != X509_LU_X509 ? NULL : (obj)->data.x509)
332 #define X509_STORE_CTX_set_current_cert(ctx, x) { (ctx)->current_cert = (x); }
333 #define X509_STORE_set_ex_data(ctx, idx, data) \
334     CRYPTO_set_ex_data(&(ctx)->ex_data, (idx), (data))
335 
336 typedef int (*X509_STORE_CTX_check_revocation_fn)(X509_STORE_CTX *ctx);
337 #define X509_STORE_CTX_set_error_depth(ctx, depth) \
338     { (ctx)->error_depth = (depth); }
339 #define EVP_PKEY_up_ref(x) ((x)->references++)
340 /* should not report missing blank line: */
341 DECLARE_STACK_OF(OPENSSL_CSTRING)
342 bool UTIL_iterate_dir(int (*fn)(const char *file, void *arg), void *arg,
343                       const char *path, bool recursive);
344 size_t UTIL_url_encode(
345                        size_t *size_needed
346                        );
347 size_t UTIL_url_encode(const char  *source,
348                        char        *destination,
349                        size_t      destination_len,
350                        size_t      *size_needed);
351 #error well. oops.
352 
353 int f()
354 {
355     c;
356     if (1)
357         c;
358     c;
359     if (1)
360         if (2)
361         { /*@ brace after 'if' not on same line just to construct case */
362             c;
363         }
364     e;
365     const usign = {
366                    0xDF,
367                    {
368                     dd
369                    },
370                    dd
371     };
372     const unsign = {
373                     0xDF, {
374                            dd
375                     },
376                     dd
377     };
378 }
379 const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = {
380                                                                0xDF,
381 };
382 const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] =
383     {
384      0xDF,
385     };
386 typedef
387 int
388 a;
389 
390 typedef
391 struct
392 {
393     int a;
394 } b;
395 typedef enum {
396               w = 0
397 } e_type;
398 typedef struct {
399     enum {
400           w = 0
401     } e_type;
402     enum {
403           w = 0
404     } e_type;
405 } e;
406 struct s_type {
407     enum e_type {
408                  w = 0
409     };
410 };
411 struct s_type
412 {
413     enum e_type {
414                  w = 0
415     };
416     enum e2_type {
417                   w = 0
418     };
419 };
420 
421 #define X  1          + 1
422 #define Y  /* .. */ 2 + 2
423 #define Z  3          + 3 * (*a++)
424 
425 static varref cmp_vars[] = { /* comment.  comment?  comment!  */
426     {&opt_config}, {&opt_section},
427 
428     {&opt_server}, {&opt_proxy}, {&opt_path},
429 };
430 
431 #define SWITCH(x)                               \
432     switch (x) {                                \
433     case 0:                                     \
434         break;                                  \
435     default:                                    \
436         break;                                  \
437     }
438 
439 #define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
440                                  DEFAULT, NEW, FREE) \
441     static int execute_CTX_##SETN##_##GETN##_##FIELD( \
442                                                      TEST_FIXTURE *fixture) \
443     { \
444         CTX *ctx = fixture->ctx; \
445         int (*set_fn)(CTX *ctx, TYPE) = \
446             (int (*)(CTX *ctx, TYPE))PREFIX##_##SETN##_##FIELD; \
447         /* comment */ \
448     }
449 
450 union un var; /* struct/union/enum in variable type */
451 struct provider_store_st *f() /* struct/union/enum in function return type */
452 {
453 }
454 static void f(struct pem_pass_data *data) /* struct/union/enum in arg list */
455 {
456 }
457 
458 static void *fun(void)
459 {
460     if (pem_name != NULL)
461         /* comment */
462         return NULL;
463 
464 label0:
465  label1: /* allow special indent 1 for label at outermost level in body */
466     do {
467     label2:
468         size_t available_len, data_len;
469         const char *curr = txt, *next = txt;
470         char *tmp;
471 
472         {
473         label3:
474         }
475     } while (1);
476 
477     char *intraline_string_with_comment_delimiters_and_dbl_space = "1  /*1";
478     char *multiline_string_with_comment_delimiters_and_dbl_space = "1  /*1\
479 2222222\'22222222222222222\"222222222" "33333  /*3333333333" "44  /*44444444444\
480 55555555555555\
481 6666";
482 }
483 
484 ASN1_CHOICE(OSSL_CRMF_POPO) = {
485     ASN1_IMP(OSSL_CRMF_POPO, value.raVerified, ASN1_NULL, 0),
486     ASN1_EXP(OSSL_CRMF_POPO, value.keyAgreement, OSSL_CRMF_POPOPRIVKEY, 3)
487 } ASN1_CHOICE_END(OSSL_CRMF_POPO)
488 IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPO)
489 
490 ASN1_ADB(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) = {
491     ADB_ENTRY(NID_id_regCtrl_regToken,
492               ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE,
493                           value.regToken, ASN1_UTF8STRING)),
494 } ASN1_ADB_END(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, 0, type, 0,
495                &attributetypeandvalue_default_tt, NULL);
496 
497 ASN1_ITEM_TEMPLATE(OSSL_CRMF_MSGS) =
498     ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
499                           OSSL_CRMF_MSGS, OSSL_CRMF_MSG)
500 ASN1_ITEM_TEMPLATE_END(OSSL_CRMF_MSGS)
501 
502 void f_looong_body_200()
503 { /* function body length up to 200 lines accepted */
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     ;
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 void f_looong_body_201()
707 { /* function body length > 200 lines, but LONG BODY marker present */
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     ;
783     ;
784     ;
785     ;
786     ;
787     ;
788     ;
789     ;
790     ;
791     ;
792     ;
793     ;
794     ;
795     ;
796     ;
797     ;
798     ;
799     ;
800     ;
801     ;
802     ;
803     ;
804     ;
805     ;
806     ;
807     ;
808     ;
809     ;
810     ;
811     ;
812     ;
813     ;
814     ;
815     ;
816     ;
817     ;
818     ;
819     ;
820     ;
821     ;
822     ;
823     ;
824     ;
825     ;
826     ;
827     ;
828     ;
829     ;
830     ;
831     ;
832     ;
833     ;
834     ;
835     ;
836     ;
837     ;
838     ;
839     ;
840     ;
841     ;
842     ;
843     ;
844     ;
845     ;
846     ;
847     ;
848     ;
849     ;
850     ;
851     ;
852     ;
853     ;
854     ;
855     ;
856     ;
857     ;
858     ;
859     ;
860     ;
861     ;
862     ;
863     ;
864     ;
865     ;
866     ;
867     ;
868     ;
869     ;
870     ;
871     ;
872     ;
873     ;
874     ;
875     ;
876     ;
877     ;
878     ;
879     ;
880     ;
881     ;
882     ;
883     ;
884     ;
885     ;
886     ;
887     ;
888     ;
889     ;
890     ;
891     ;
892     ;
893     ;
894     ;
895     ;
896     ;
897     ;
898     ;
899     ;
900     ;
901     ;
902     ;
903     ;
904     ;
905     ;
906     ;
907     ;
908     ;
909     ;
910 }
911