xref: /freebsd/crypto/openssl/util/check-format-test-positives.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1e0c4386eSCy Schubert /*
2*e7be843bSPierre Pronchery  * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved.
3e0c4386eSCy Schubert  * Copyright Siemens AG 2015-2022
4e0c4386eSCy Schubert  *
5e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
6e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
7e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
8e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
9e0c4386eSCy Schubert  */
10e0c4386eSCy Schubert 
11e0c4386eSCy Schubert /*
12e0c4386eSCy Schubert  * This demonstrates/tests cases where check-format.pl should report issues.
13e0c4386eSCy Schubert  * Some of the reports are due to sanity checks for proper nesting of comment
14e0c4386eSCy Schubert  * delimiters and parenthesis-like symbols, e.g., on unexpected/unclosed braces.
15e0c4386eSCy Schubert  */
16e0c4386eSCy Schubert 
17e0c4386eSCy Schubert /*
18*e7be843bSPierre Pronchery  * The '@'s after leading '*' in comment lines are used for self-tests:
19*e7be843bSPierre Pronchery  * they mark lines containing a single issue that should be reported.
20*e7be843bSPierre Pronchery  * Normally it should be reported while handling the given line,
21*e7be843bSPierre Pronchery  * but in case of delayed checks there is a following digit
22*e7be843bSPierre Pronchery  * indicating the number of reports expected for this line.
23e0c4386eSCy Schubert  */
24e0c4386eSCy Schubert 
25*e7be843bSPierre Pronchery /* this line is between 81 and 100 chars long, to be reported with -strict-len */
26*e7be843bSPierre Pronchery 
27e0c4386eSCy Schubert /* For each of the following set of lines the tool should complain once */
28e0c4386eSCy Schubert /*@ tab character: 	 */
29e0c4386eSCy Schubert /*@ intra-line carriage return character:
30e0c4386eSCy Schubert  */
31e0c4386eSCy Schubert /*@ non-printable ASCII character:  */
32e0c4386eSCy Schubert /*@ non-ASCII character: ä */
33e0c4386eSCy Schubert /*@ whitespace at EOL: */
34e0c4386eSCy Schubert // /*@ end-of-line comment style not allowed (for C90 compatibility) */
35e0c4386eSCy Schubert  /*@0 intra-line comment indent off by 1, reported unless sloppy-cmt */
36e0c4386eSCy Schubert /*X */ /*@2 missing spc or '*' after comment start reported unless sloppy-spc */
37e0c4386eSCy Schubert /* X*/ /*@ missing space before comment end , reported unless sloppy-spc */
38e0c4386eSCy Schubert /*@ comment starting delimiter: /* inside intra-line comment */
39e0c4386eSCy Schubert  /*@0
40e0c4386eSCy Schubert   *@ above multi-line comment start indent off by 1, reported unless sloppy-cmt; this comment line is too long
41e0c4386eSCy Schubert    *@ multi-line comment indent further off by 1 relative to comment start
42e0c4386eSCy Schubert   *@ multi-line comment ending with text on last line */
43e0c4386eSCy Schubert /*@2 multi-line comment starting with text on first line
44e0c4386eSCy Schubert  *@ comment starting delimiter: /* inside multi-line comment
45e0c4386eSCy Schubert *@ multi-line comment indent off by -1
46e0c4386eSCy Schubert  *X*@ no spc after leading '*' in multi-line comment, reported unless sloppy-spc
47e0c4386eSCy Schubert  *@0 more than two spaces after .   in comment, no more reported
48e0c4386eSCy Schubert  *@0 more than two spaces after ?   in comment, no more reported
49e0c4386eSCy Schubert  *@0 more than two spaces after !   in comment, no more reported
50e0c4386eSCy Schubert */ /*@ multi-line comment end indent off by -1 (relative to comment start) */
51e0c4386eSCy Schubert */ /*@ unexpected comment ending delimiter outside comment */
52*e7be843bSPierre Pronchery /*- '-' for formatted comment not allowed in intra-line comment */
53e0c4386eSCy Schubert /*@ comment line is toooooooooooo wide by 1 char, or by 21 chars in case strict-len option is used */
54e0c4386eSCy Schubert #if ~0              /*@ '#if' with constant condition */
55e0c4386eSCy Schubert  #endif             /*@ indent of preproc. directive off by 1 (must be 0) */
56e0c4386eSCy Schubert #define X (1 +  1)  /*@0 extra space in body, reported unless sloppy-spc */
57e0c4386eSCy Schubert #define Y   1       /*@ extra space before body, reported unless sloppy-spc */ \
58e0c4386eSCy Schubert #define Z           /*@2 preprocessor directive within multi-line directive */
59e0c4386eSCy Schubert typedef struct  {   /*@0 extra space in code, reported unless sloppy-spc */
60e0c4386eSCy Schubert     enum {          /*@1 extra space  in intra-line comment, no more reported */
61e0c4386eSCy Schubert            w = 0 /*@ hanging expr indent off by 1, or 3 for lines after '{' */
62e0c4386eSCy Schubert              && 1,  /*@ hanging expr indent off by 3, or -1 for leading '&&' */
63e0c4386eSCy Schubert          x = 1,     /*@ hanging expr indent off by -1 */
64e0c4386eSCy Schubert           y,z       /*@ no space after ',', reported unless sloppy-spc */
65e0c4386eSCy Schubert     } e_member ;    /*@ space before ';', reported unless sloppy-spc */
66e0c4386eSCy Schubert     int v[1;        /*@ unclosed bracket in type declaration */
67e0c4386eSCy Schubert    union {          /*@ statement/type declaration indent off by -1 */
68e0c4386eSCy Schubert         struct{} s; /*@ no space before '{', reported unless sloppy-spc */
69e0c4386eSCy Schubert     }u_member;      /*@ no space after '}', reported unless sloppy-spc */
70e0c4386eSCy Schubert     } s_type;       /*@ statement/type declaration indent off by 4 */
71e0c4386eSCy Schubert int* somefunc();    /*@ no space before '*' in type decl, r unless sloppy-spc */
72e0c4386eSCy Schubert void main(int n) {  /*@ opening brace at end of function definition header */
73e0c4386eSCy Schubert     for (; ; ) ;    /*@ space before ')', reported unless sloppy-spc */
74e0c4386eSCy Schubert     for ( ; x; y) ; /*@2 space after '(' and before ';', unless sloppy-spc */
75e0c4386eSCy Schubert     for (;;n++) {   /*@ missing space after ';', reported unless sloppy-spc */
76e0c4386eSCy Schubert         return;     /*@0 (1-line) single statement in braces */
77e0c4386eSCy Schubert     }}              /*@2 code after '}' outside expr */
78e0c4386eSCy Schubert }                   /*@ unexpected closing brace (too many '}') outside expr */
79e0c4386eSCy Schubert )                   /*@ unexpected closing paren outside expr */
80e0c4386eSCy Schubert #endif              /*@ unexpected #endif */
81e0c4386eSCy Schubert int f (int a,       /*@ space after fn before '(', reported unless sloppy-spc */
82e0c4386eSCy Schubert       int b,        /*@ hanging expr indent off by -1 */
83e0c4386eSCy Schubert        long I)      /*@ single-letter name 'I' */
84e0c4386eSCy Schubert { int x;            /*@ code after '{' opening a block */
85e0c4386eSCy Schubert     int xx = 1) +   /*@ unexpected closing parenthesis */
86e0c4386eSCy Schubert         0L <        /*@ constant on LHS of comparison operator */
87e0c4386eSCy Schubert         a] -        /*@ unexpected closing bracket */
88e0c4386eSCy Schubert         3: *        /*@ unexpected ':' (without preceding '?') within expr */
89e0c4386eSCy Schubert         4};         /*@ unexpected closing brace within expression */
90e0c4386eSCy Schubert     char y[] = {    /*@0 unclosed brace within initializer/enum expression */
91e0c4386eSCy Schubert         1* 1,       /*@ no space etc. before '*', reported unless sloppy-spc */
92e0c4386eSCy Schubert          2,         /*@ hanging expr indent (for lines after '{') off by 1 */
93e0c4386eSCy Schubert         (xx         /*@0 unclosed parenthesis in expression */
94e0c4386eSCy Schubert          ? y        /*@0 unclosed '? (conditional expression) */
95e0c4386eSCy Schubert          [0;        /*@4 unclosed bracket in expression */
96e0c4386eSCy Schubert     /*@ blank line within local decls */
97e0c4386eSCy Schubert    s_type s;        /*@2 local variable declaration indent off by -1 */
98e0c4386eSCy Schubert    t_type t;        /*@ local variable declaration indent again off by -1 */
99e0c4386eSCy Schubert     /* */           /*@0 missing blank line after local decls */
100e0c4386eSCy Schubert    somefunc(a,      /*@2 statement indent off by -1 */
101e0c4386eSCy Schubert           "aligned" /*@ expr indent off by -2 accepted if sloppy-hang */ "right"
102e0c4386eSCy Schubert            , b,     /*@ expr indent off by -1 */
103e0c4386eSCy Schubert            b,       /*@ expr indent as on line above, accepted if sloppy-hang */
104e0c4386eSCy Schubert     b, /*@ expr indent off -8 but @ extra indent accepted if sloppy-hang */
105*e7be843bSPierre Pronchery    "again aligned" /*@ expr indent off by -9 (left of stmt indent, */ "right",
106e0c4386eSCy Schubert             abc == /*@ .. so reported also with sloppy-hang; this line is too long by 6 or 26 chars */ 456
107e0c4386eSCy Schubert # define MAC(A) (A) /*@ nesting indent of preprocessor directive off by 1 */
108e0c4386eSCy Schubert              ? 1    /*@ hanging expr indent off by 1 */
109e0c4386eSCy Schubert               : 2); /*@ hanging expr indent off by 2, or 1 for leading ':' */
110e0c4386eSCy Schubert     if(a            /*@ missing space after 'if', reported unless sloppy-spc */
111e0c4386eSCy Schubert           /*@0 intra-line comment indent off by -1 (not: by 3 due to '&&') */
112e0c4386eSCy Schubert            && ! 0   /*@2 space after '!', reported unless sloppy-spc */
113e0c4386eSCy Schubert          || b ==    /*@ hanging expr indent off by 2, or -2 for leading '||' */
114e0c4386eSCy Schubert        (x<<= 1) +   /*@ missing space before '<<=' reported unless sloppy-spc */
115e0c4386eSCy Schubert        (xx+= 2) +   /*@ missing space before '+=', reported unless sloppy-spc */
116e0c4386eSCy Schubert        (a^ 1) +     /*@ missing space before '^', reported unless sloppy-spc */
117e0c4386eSCy Schubert        (y *=z) +    /*@ missing space after '*=' reported unless sloppy-spc */
118e0c4386eSCy Schubert        a %2 /       /*@ missing space after '%', reported unless sloppy-spc */
119e0c4386eSCy Schubert        1 +/* */     /*@ no space before comment, reported unless sloppy-spc */
120e0c4386eSCy Schubert        /* */+       /*@ no space after comment, reported unless sloppy-spc */
121e0c4386eSCy Schubert        s. e_member) /*@ space after '.', reported unless sloppy-spc */
122e0c4386eSCy Schubert          xx = a + b /*@ extra single-statement indent off by 1 */
123e0c4386eSCy Schubert                + 0; /*@ two times extra single-statement indent off by 3 */
124e0c4386eSCy Schubert     if (a ++)       /*@ space before postfix '++', reported unless sloppy-spc */
125e0c4386eSCy Schubert     {               /*@ {' not on same line as preceding 'if' */
126e0c4386eSCy Schubert         c;          /*@0 single stmt in braces, reported on 1-stmt */
127e0c4386eSCy Schubert     } else          /*@ missing '{' on same line after '} else' */
128e0c4386eSCy Schubert       {             /*@ statement indent off by 2 */
129e0c4386eSCy Schubert         d;          /*@0 single stmt in braces, reported on 1-stmt */
130e0c4386eSCy Schubert           }         /*@ statement indent off by 6 */
131e0c4386eSCy Schubert     if (1) f(a,     /*@ (non-brace) code after end of 'if' condition */
132e0c4386eSCy Schubert              b); else /*@ (non-brace) code before 'else' */
133e0c4386eSCy Schubert         do f(c, c); /*@ (non-brace) code after 'do' */
134e0c4386eSCy Schubert         while ( 2); /*@ space after '(', reported unless sloppy-spc */
135e0c4386eSCy Schubert     b; c;           /*@ more than one statement per line */
136e0c4386eSCy Schubert   outer:            /*@ outer label special indent off by 1 */
137e0c4386eSCy Schubert     do{             /*@ missing space before '{', reported unless sloppy-spc */
138e0c4386eSCy Schubert      inner:         /*@ inner label normal indent off by 1 */
139e0c4386eSCy Schubert         f (3,       /*@ space after fn before '(', reported unless sloppy-spc */
140e0c4386eSCy Schubert            4);      /*@0 false negative: should report single stmt in braces */
141e0c4386eSCy Schubert     }               /*@0 'while' not on same line as preceding '}' */
142e0c4386eSCy Schubert     while (a+ 0);   /*@2 missing space before '+', reported unless sloppy-spc */
143e0c4386eSCy Schubert     switch (b ) {   /*@ space before ')', reported unless sloppy-spc */
144e0c4386eSCy Schubert    case 1:          /*@ 'case' special statement indent off by -1 */
145e0c4386eSCy Schubert     case(2):       /*@ missing space after 'case', reported unless sloppy-spc */
146e0c4386eSCy Schubert     default: ;      /*@ code after 'default:' */
147e0c4386eSCy Schubert }                   /*@ statement indent off by -4 */
148e0c4386eSCy Schubert     return(      /*@ missing space after 'return', reported unless sloppy-spc */
149e0c4386eSCy Schubert            x); }    /*@ code before block-level '}' */
150e0c4386eSCy Schubert /* Here the tool should stop complaining apart from the below issues at EOF */
151e0c4386eSCy Schubert 
152e0c4386eSCy Schubert void f_looong_body()
153e0c4386eSCy Schubert {
154e0c4386eSCy Schubert     ;
155e0c4386eSCy Schubert     ;
156e0c4386eSCy Schubert     ;
157e0c4386eSCy Schubert     ;
158e0c4386eSCy Schubert     ;
159e0c4386eSCy Schubert     ;
160e0c4386eSCy Schubert     ;
161e0c4386eSCy Schubert     ;
162e0c4386eSCy Schubert     ;
163e0c4386eSCy Schubert     ;
164e0c4386eSCy Schubert     ;
165e0c4386eSCy Schubert     ;
166e0c4386eSCy Schubert     ;
167e0c4386eSCy Schubert     ;
168e0c4386eSCy Schubert     ;
169e0c4386eSCy Schubert     ;
170e0c4386eSCy Schubert     ;
171e0c4386eSCy Schubert     ;
172e0c4386eSCy Schubert     ;
173e0c4386eSCy Schubert     ;
174e0c4386eSCy Schubert     ;
175e0c4386eSCy Schubert     ;
176e0c4386eSCy Schubert     ;
177e0c4386eSCy Schubert     ;
178e0c4386eSCy Schubert     ;
179e0c4386eSCy Schubert     ;
180e0c4386eSCy Schubert     ;
181e0c4386eSCy Schubert     ;
182e0c4386eSCy Schubert     ;
183e0c4386eSCy Schubert     ;
184e0c4386eSCy Schubert     ;
185e0c4386eSCy Schubert     ;
186e0c4386eSCy Schubert     ;
187e0c4386eSCy Schubert     ;
188e0c4386eSCy Schubert     ;
189e0c4386eSCy Schubert     ;
190e0c4386eSCy Schubert     ;
191e0c4386eSCy Schubert     ;
192e0c4386eSCy Schubert     ;
193e0c4386eSCy Schubert     ;
194e0c4386eSCy Schubert     ;
195e0c4386eSCy Schubert     ;
196e0c4386eSCy Schubert     ;
197e0c4386eSCy Schubert     ;
198e0c4386eSCy Schubert     ;
199e0c4386eSCy Schubert     ;
200e0c4386eSCy Schubert     ;
201e0c4386eSCy Schubert     ;
202e0c4386eSCy Schubert     ;
203e0c4386eSCy Schubert     ;
204e0c4386eSCy Schubert     ;
205e0c4386eSCy Schubert     ;
206e0c4386eSCy Schubert     ;
207e0c4386eSCy Schubert     ;
208e0c4386eSCy Schubert     ;
209e0c4386eSCy Schubert     ;
210e0c4386eSCy Schubert     ;
211e0c4386eSCy Schubert     ;
212e0c4386eSCy Schubert     ;
213e0c4386eSCy Schubert     ;
214e0c4386eSCy Schubert     ;
215e0c4386eSCy Schubert     ;
216e0c4386eSCy Schubert     ;
217e0c4386eSCy Schubert     ;
218e0c4386eSCy Schubert     ;
219e0c4386eSCy Schubert     ;
220e0c4386eSCy Schubert     ;
221e0c4386eSCy Schubert     ;
222e0c4386eSCy Schubert     ;
223e0c4386eSCy Schubert     ;
224e0c4386eSCy Schubert     ;
225e0c4386eSCy Schubert     ;
226e0c4386eSCy Schubert     ;
227e0c4386eSCy Schubert     ;
228e0c4386eSCy Schubert     ;
229e0c4386eSCy Schubert     ;
230e0c4386eSCy Schubert     ;
231e0c4386eSCy Schubert     ;
232e0c4386eSCy Schubert     ;
233e0c4386eSCy Schubert     ;
234e0c4386eSCy Schubert     ;
235e0c4386eSCy Schubert     ;
236e0c4386eSCy Schubert     ;
237e0c4386eSCy Schubert     ;
238e0c4386eSCy Schubert     ;
239e0c4386eSCy Schubert     ;
240e0c4386eSCy Schubert     ;
241e0c4386eSCy Schubert     ;
242e0c4386eSCy Schubert     ;
243e0c4386eSCy Schubert     ;
244e0c4386eSCy Schubert     ;
245e0c4386eSCy Schubert     ;
246e0c4386eSCy Schubert     ;
247e0c4386eSCy Schubert     ;
248e0c4386eSCy Schubert     ;
249e0c4386eSCy Schubert     ;
250e0c4386eSCy Schubert     ;
251e0c4386eSCy Schubert     ;
252e0c4386eSCy Schubert     ;
253e0c4386eSCy Schubert     ;
254e0c4386eSCy Schubert     ;
255e0c4386eSCy Schubert     ;
256e0c4386eSCy Schubert     ;
257e0c4386eSCy Schubert     ;
258e0c4386eSCy Schubert     ;
259e0c4386eSCy Schubert     ;
260e0c4386eSCy Schubert     ;
261e0c4386eSCy Schubert     ;
262e0c4386eSCy Schubert     ;
263e0c4386eSCy Schubert     ;
264e0c4386eSCy Schubert     ;
265e0c4386eSCy Schubert     ;
266e0c4386eSCy Schubert     ;
267e0c4386eSCy Schubert     ;
268e0c4386eSCy Schubert     ;
269e0c4386eSCy Schubert     ;
270e0c4386eSCy Schubert     ;
271e0c4386eSCy Schubert     ;
272e0c4386eSCy Schubert     ;
273e0c4386eSCy Schubert     ;
274e0c4386eSCy Schubert     ;
275e0c4386eSCy Schubert     ;
276e0c4386eSCy Schubert     ;
277e0c4386eSCy Schubert     ;
278e0c4386eSCy Schubert     ;
279e0c4386eSCy Schubert     ;
280e0c4386eSCy Schubert     ;
281e0c4386eSCy Schubert     ;
282e0c4386eSCy Schubert     ;
283e0c4386eSCy Schubert     ;
284e0c4386eSCy Schubert     ;
285e0c4386eSCy Schubert     ;
286e0c4386eSCy Schubert     ;
287e0c4386eSCy Schubert     ;
288e0c4386eSCy Schubert     ;
289e0c4386eSCy Schubert     ;
290e0c4386eSCy Schubert     ;
291e0c4386eSCy Schubert     ;
292e0c4386eSCy Schubert     ;
293e0c4386eSCy Schubert     ;
294e0c4386eSCy Schubert     ;
295e0c4386eSCy Schubert     ;
296e0c4386eSCy Schubert     ;
297e0c4386eSCy Schubert     ;
298e0c4386eSCy Schubert     ;
299e0c4386eSCy Schubert     ;
300e0c4386eSCy Schubert     ;
301e0c4386eSCy Schubert     ;
302e0c4386eSCy Schubert     ;
303e0c4386eSCy Schubert     ;
304e0c4386eSCy Schubert     ;
305e0c4386eSCy Schubert     ;
306e0c4386eSCy Schubert     ;
307e0c4386eSCy Schubert     ;
308e0c4386eSCy Schubert     ;
309e0c4386eSCy Schubert     ;
310e0c4386eSCy Schubert     ;
311e0c4386eSCy Schubert     ;
312e0c4386eSCy Schubert     ;
313e0c4386eSCy Schubert     ;
314e0c4386eSCy Schubert     ;
315e0c4386eSCy Schubert     ;
316e0c4386eSCy Schubert     ;
317e0c4386eSCy Schubert     ;
318e0c4386eSCy Schubert     ;
319e0c4386eSCy Schubert     ;
320e0c4386eSCy Schubert     ;
321e0c4386eSCy Schubert     ;
322e0c4386eSCy Schubert     ;
323e0c4386eSCy Schubert     ;
324e0c4386eSCy Schubert     ;
325e0c4386eSCy Schubert     ;
326e0c4386eSCy Schubert     ;
327e0c4386eSCy Schubert     ;
328e0c4386eSCy Schubert     ;
329e0c4386eSCy Schubert     ;
330e0c4386eSCy Schubert     ;
331e0c4386eSCy Schubert     ;
332e0c4386eSCy Schubert     ;
333e0c4386eSCy Schubert     ;
334e0c4386eSCy Schubert     ;
335e0c4386eSCy Schubert     ;
336e0c4386eSCy Schubert     ;
337e0c4386eSCy Schubert     ;
338e0c4386eSCy Schubert     ;
339e0c4386eSCy Schubert     ;
340e0c4386eSCy Schubert     ;
341e0c4386eSCy Schubert     ;
342e0c4386eSCy Schubert     ;
343e0c4386eSCy Schubert     ;
344e0c4386eSCy Schubert     ;
345e0c4386eSCy Schubert     ;
346e0c4386eSCy Schubert     ;
347e0c4386eSCy Schubert     ;
348e0c4386eSCy Schubert     ;
349e0c4386eSCy Schubert     ;
350e0c4386eSCy Schubert     ;
351e0c4386eSCy Schubert     ;
352e0c4386eSCy Schubert 
353e0c4386eSCy Schubert 
354e0c4386eSCy Schubert     ;               /*@ 2 essentially blank lines before, if !sloppy-spc */
355e0c4386eSCy Schubert }                   /*@ function body length > 200 lines */
356e0c4386eSCy Schubert #if X               /*@0 unclosed #if */
357e0c4386eSCy Schubert struct t {          /*@0 unclosed brace at decl/block level */
358e0c4386eSCy Schubert     enum {          /*@0 unclosed brace at enum/expression level */
359e0c4386eSCy Schubert           v = (1    /*@0 unclosed parenthesis */
360e0c4386eSCy Schubert                etyp /*@0 blank line follows just before EOF, if !sloppy-spc: */
361 
362