1/*
2 * Copyright 2007-2023 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#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
39int 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
114int 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 (1)
165        f(a, b);
166    do
167        1; while (2); /*@ more than one stmt just to construct case */
168    if (1)
169        f(a, b);
170    else
171        do
172            1;
173        while (2);
174    if (1)
175        f(a, b);
176    else do /*@ (non-brace) code before 'do' just to construct case */
177             1;
178        while (2);
179    f1234(a,
180          b); do /*@ (non-brace) code before 'do' just to construct case */
181                  1;
182    while (2);
183    if (1)
184        f(a,
185          b); do /*@ (non-brace) code before 'do' just to construct case */
186                  1;
187    while (2);
188    if (1)
189        f(a, b);
190    else
191        do f(c, c); /*@ (non-brace) code after 'do' just to construct case */
192        while (2);
193
194    if (1)
195        f(a, b);
196    else
197        return;
198    if (1)
199        f(a,
200          b); else /*@ (non-brace) code before 'else' just to construct case */
201        do
202            1;
203        while (2);
204
205    if (1)
206    { /*@ brace after 'if' not on same line just to construct case */
207        c;
208        d;
209    }
210    /* this comment is correctly indented if it refers to the following line */
211    d;
212
213    if (1) {
214        2;
215    } else /*@ no brace after 'else' just to construct case */
216        3;
217    do {
218    } while (x);
219    if (1) {
220        2;
221    } else {
222        3;
223    }
224    if (4)
225        5;
226    else
227        6;
228
229    if (1) {
230        if (2) {
231        case MAC_TYPE_MAC:
232            {
233                EVP_MAC_CTX *new_mac_ctx;
234
235                if (ctx->pkey == NULL)
236                    return 0;
237            }
238            break;
239        default:
240            /* This should be dead code */
241            return 0;
242        }
243    }
244    if (expr_line1
245        == expr_line2
246            && expr_line3) {
247        c1;
248    } else {
249        c;
250        d;
251    }
252    if (expr_line1
253        == expr_line2
254            && expr_line3)
255        hanging_stmt;
256}
257#define m \
258    do { /* should not be confused with function header followed by '{' */ \
259    } while (0)
260
261/* should not trigger: constant on LHS of comparison or assignment operator */
262X509 *x509 = NULL;
263int y = a + 1 < b;
264int ret, was_NULL = *certs == NULL;
265
266/* should not trigger: missing space before ... */
267float z = 1e-6 * (-1) * b[+6] * 1e+1 * (a)->f * (long)+1
268    - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9);
269struct st = {-1, 0};
270int x = (y <<= 1) + (z <= 5.0);
271
272const OPTIONS passwd_options[] = {
273    {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
274#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
275    {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
276#endif
277    OPT_R_OPTIONS,
278
279    {NULL}
280};
281
282typedef * d(int)
283    x;
284typedef (int)
285x;
286typedef (int)*()
287    x;
288typedef *int *
289x;
290typedef OSSL_CMP_MSG *(*cmp_srv_process_cb_t)
291    (OSSL_CMP_SRV_CTX *ctx, OSSL_CMP_MSG *msg)
292    xx;
293
294#define IF(cond) if (cond)
295
296_Pragma("GCC diagnostic push")
297_Pragma("GCC diagnostic pop")
298
299#define CB_ERR_IF(cond, ctx, cert, depth, err) \
300    if ((cond) && ((depth) < 0 || verify_cb_cert(ctx, cert, depth, err) == 0)) \
301        return err
302static int verify_cb_crl(X509_STORE_CTX *ctx, int err)
303{
304    ctx->error = err;
305    return ctx->verify_cb(0, ctx);
306}
307
308#ifdef CMP_FALLBACK_EST
309# define CMP_FALLBACK_CERT_FILE "cert.pem"
310#endif
311
312#define X509_OBJECT_get0_X509(obj)                                      \
313    ((obj) == NULL || (obj)->type != X509_LU_X509 ? NULL : (obj)->data.x509)
314#define X509_STORE_CTX_set_current_cert(ctx, x) { (ctx)->current_cert = (x); }
315#define X509_STORE_set_ex_data(ctx, idx, data) \
316    CRYPTO_set_ex_data(&(ctx)->ex_data, (idx), (data))
317
318typedef int (*X509_STORE_CTX_check_revocation_fn)(X509_STORE_CTX *ctx);
319#define X509_STORE_CTX_set_error_depth(ctx, depth) \
320    { (ctx)->error_depth = (depth); }
321#define EVP_PKEY_up_ref(x) ((x)->references++)
322/* should not report missing blank line: */
323DECLARE_STACK_OF(OPENSSL_CSTRING)
324bool UTIL_iterate_dir(int (*fn)(const char *file, void *arg), void *arg,
325                      const char *path, bool recursive);
326size_t UTIL_url_encode(
327                       size_t *size_needed
328                       );
329size_t UTIL_url_encode(const char  *source,
330                       char        *destination,
331                       size_t      destination_len,
332                       size_t      *size_needed);
333#error well. oops.
334
335int f()
336{
337    c;
338    if (1) {
339        c;
340    }
341    c;
342    if (1)
343        if (2)
344        { /*@ brace after 'if' not on same line just to construct case */
345            c;
346        }
347    e;
348    const usign = {
349                   0xDF,
350                   {
351                    dd
352                   },
353                   dd
354    };
355    const unsign = {
356                    0xDF, {
357                           dd
358                    },
359                    dd
360    };
361}
362const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = {
363                                                               0xDF,
364};
365const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] =
366    {
367     0xDF,
368    };
369typedef
370int
371a;
372
373typedef
374struct
375{
376    int a;
377} b;
378typedef enum {
379              w = 0
380} e_type;
381typedef struct {
382    enum {
383          w = 0
384    } e_type;
385    enum {
386          w = 0
387    } e_type;
388} e;
389struct s_type {
390    enum e_type {
391                 w = 0
392    };
393};
394struct s_type
395{
396    enum e_type {
397                 w = 0
398    };
399    enum e2_type {
400                  w = 0
401    };
402};
403
404#define X  1          + 1
405#define Y  /* .. */ 2 + 2
406#define Z  3          + 3 * (*a++)
407
408static varref cmp_vars[] = { /* comment.  comment?  comment!  */
409    {&opt_config}, {&opt_section},
410
411    {&opt_server}, {&opt_proxy}, {&opt_path},
412};
413
414#define SWITCH(x)                               \
415    switch (x) {                                \
416    case 0:                                     \
417        break;                                  \
418    default:                                    \
419        break;                                  \
420    }
421
422#define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
423                                 DEFAULT, NEW, FREE) \
424    static int execute_CTX_##SETN##_##GETN##_##FIELD( \
425                                                     TEST_FIXTURE *fixture) \
426    { \
427        CTX *ctx = fixture->ctx; \
428        int (*set_fn)(CTX *ctx, TYPE) = \
429            (int (*)(CTX *ctx, TYPE))PREFIX##_##SETN##_##FIELD; \
430        /* comment */ \
431    }
432
433union un var; /* struct/union/enum in variable type */
434struct provider_store_st *f() /* struct/union/enum in function return type */
435{
436}
437static void f(struct pem_pass_data *data) /* struct/union/enum in arg list */
438{
439}
440
441static void *fun(void)
442{
443    if (pem_name != NULL)
444        /* comment */
445        return NULL;
446
447label0:
448 label1: /* allow special indent 1 for label at outermost level in body */
449    do {
450    label2:
451        size_t available_len, data_len;
452        const char *curr = txt, *next = txt;
453        char *tmp;
454
455        {
456        label3:
457        }
458    } while (1);
459
460    char *intraline_string_with_comment_delimiters_and_dbl_space = "1  /*1";
461    char *multiline_string_with_comment_delimiters_and_dbl_space = "1  /*1\
4622222222\'22222222222222222\"222222222" "33333  /*3333333333" "44  /*44444444444\
46355555555555555\
4646666";
465}
466
467ASN1_CHOICE(OSSL_CRMF_POPO) = {
468    ASN1_IMP(OSSL_CRMF_POPO, value.raVerified, ASN1_NULL, 0),
469    ASN1_EXP(OSSL_CRMF_POPO, value.keyAgreement, OSSL_CRMF_POPOPRIVKEY, 3)
470} ASN1_CHOICE_END(OSSL_CRMF_POPO)
471IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPO)
472
473ASN1_ADB(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) = {
474    ADB_ENTRY(NID_id_regCtrl_regToken,
475              ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE,
476                          value.regToken, ASN1_UTF8STRING)),
477} ASN1_ADB_END(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, 0, type, 0,
478               &attributetypeandvalue_default_tt, NULL);
479
480ASN1_ITEM_TEMPLATE(OSSL_CRMF_MSGS) =
481    ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
482                          OSSL_CRMF_MSGS, OSSL_CRMF_MSG)
483ASN1_ITEM_TEMPLATE_END(OSSL_CRMF_MSGS)
484
485void f_looong_body_200()
486{ /* function body length up to 200 lines accepted */
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    ;
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
689void f_looong_body_201()
690{ /* function body length > 200 lines, but LONG BODY marker present */
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    ;
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