Debian Codegraph @

docs/misra/rules.rst

raw
1 .. SPDX-License-Identifier: CC-BY-4.0
2
3 MISRA C rules for Xen
4 =====================
5
6 .. note::
7
8 **IMPORTANT** All MISRA C rules, text, and examples are copyrighted
9 by the MISRA Consortium Limited and used with permission.
10
11 Please refer to https://www.misra.org.uk/ to obtain a copy of MISRA
12 C, or for licensing options for other use of the rules.
13
14 The following is the list of MISRA C rules that apply to the Xen
15 hypervisor.
16
17 It is possible that in specific circumstances it is best not to follow a
18 rule because it is not possible or because the alternative leads to
19 better code quality. Those cases are called "deviations". They are
20 permissible as long as they are documented. For details, please refer to
21 docs/misra/documenting-violations.rst and docs/misra/deviations.rst
22
23 Other documentation mechanisms are work-in-progress.
24
25 The existing codebase is not 100% compliant with the rules. Some of the
26 violations are meant to be documented as deviations, while some others
27 should be fixed. Both compliance and documenting deviations on the
28 existing codebase are work-in-progress.
29
30 The list below might need to be updated over time. Reach out to THE REST
31 maintainers if you want to suggest a change.
32
33 .. list-table::
34 :header-rows: 1
35
36 * - Dir number
37 - Severity
38 - Summary
39 - Notes
40
41 * - `Dir 1.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/D_01_01.c>`_
42 - Required
43 - Any implementation-defined behaviour on which the output of the
44 program depends shall be documented and understood
45 -
46
47 * - `Dir 2.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/D_02_01.c>`_
48 - Required
49 - All source files shall compile without any compilation errors
50 -
51
52 * - `Dir 4.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/D_04_01.c>`_
53 - Required
54 - Run-time failures shall be minimized
55 - The strategies adopted by Xen to prevent certain classes of runtime
56 failures is documented by
57 `C-runtime-failures.rst <docs/misra/C-runtime-failures.rst>`_
58
59 * - `Dir 4.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/D_04_07.c>`_
60 - Required
61 - If a function returns error information then that error
62 information shall be tested
63 -
64
65 * - `Dir 4.10 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/D_04_10.c>`_
66 - Required
67 - Precautions shall be taken in order to prevent the contents of a
68 header file being included more than once
69 - Files that are intended to be included more than once do not need to
70 conform to the directive
71
72 * - `Dir 4.11 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/D_04_11.c>`_
73 - Required
74 - The validity of values passed to library functions shall be checked
75 - We do not have libraries in Xen (libfdt and others are not
76 considered libraries from MISRA C point of view as they are
77 imported in source form)
78
79 * - `Dir 4.14 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/D_04_14.c>`_
80 - Required
81 - The validity of values received from external sources shall be
82 checked
83 -
84
85 .. list-table::
86 :header-rows: 1
87
88 * - Rule number
89 - Severity
90 - Summary
91 - Notes
92
93 * - `Rule 1.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_01_01.c>`_
94 - Required
95 - The program shall contain no violations of the standard C syntax
96 and constraints, and shall not exceed the implementation's
97 translation limits
98 - We make use of several compiler extensions as documented by
99 `C-language-toolchain.rst <docs/misra/C-language-toolchain.rst>`_
100
101 * - `Rule 1.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_01_03.c>`_
102 - Required
103 - There shall be no occurrence of undefined or critical unspecified
104 behaviour
105 -
106
107 * - `Rule 1.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/>`_
108 - Required
109 - Emergent language features shall not be used
110 - Emergent language features, such as C11 features, should not be
111 confused with similar compiler extensions, which we use. When the
112 time comes to adopt C11, this rule will be revisited.
113
114 * - `Rule 2.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_02_01_1.c>`_
115 - Required
116 - A project shall not contain unreachable code
117 - The following are allowed:
118 - Invariantly constant conditions, e.g. if(IS_ENABLED(CONFIG_HVM)) { S; }
119 - Switch with a controlling value statically determined not to
120 match one or more case statements
121 - Functions that are intended to be referenced only from
122 assembly code (e.g. 'do_trap_fiq')
123 - asm-offsets.c, as they are not linked deliberately, because
124 they are used to generate definitions for asm modules
125 - Declarations without initializer are safe, as they are not
126 executed
127
128 * - `Rule 2.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_02_06.c>`_
129 - Advisory
130 - A function should not contain unused label declarations
131 -
132
133 * - `Rule 3.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_03_01.c>`_
134 - Required
135 - The character sequences /* and // shall not be used within a
136 comment
137 - Comments containing hyperlinks inside C-style block comments are safe
138
139 * - `Rule 3.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_03_02.c>`_
140 - Required
141 - Line-splicing shall not be used in // comments
142 -
143
144 * - `Rule 4.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_04_01.c>`_
145 - Required
146 - Octal and hexadecimal escape sequences shall be terminated
147 -
148
149 * - `Rule 4.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_04_02.c>`_
150 - Advisory
151 - Trigraphs should not be used
152 -
153
154 * - `Rule 5.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_05_01_2.c>`_
155 - Required
156 - External identifiers shall be distinct
157 - The Xen characters limit for identifiers is 63. Public headers
158 (xen/include/public/) are allowed to retain longer identifiers
159 for backward compatibility.
160
161 * - `Rule 5.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_05_02.c>`_
162 - Required
163 - Identifiers declared in the same scope and name space shall be
164 distinct
165 - The Xen characters limit for identifiers is 63. Public headers
166 (xen/include/public/) are allowed to retain longer identifiers
167 for backward compatibility.
168
169 * - `Rule 5.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_05_03.c>`_
170 - Required
171 - An identifier declared in an inner scope shall not hide an
172 identifier declared in an outer scope
173 - Using macros as macro parameters at invocation time is allowed
174 even if both macros use identically named local variables, e.g.
175 max(var0, min(var1, var2))
176
177 * - `Rule 5.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_05_04.c>`_
178 - Required
179 - Macro identifiers shall be distinct
180 - The Xen characters limit for macro identifiers is 63. Public
181 headers (xen/include/public/) are allowed to retain longer
182 identifiers for backward compatibility.
183
184 * - `Rule 5.5 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_05_05.c>`_
185 - Required
186 - Identifiers shall be distinct from macro names
187 - Macros expanding to their own name are allowed, e.g.::
188
189 #define x x
190
191 Clashes between names of function-like macros and identifiers of
192 non-callable entities are allowed. Callable entities having an
193 identifier that is the same of the name of a
194 function-like macro are not allowed. Example (not allowed)::
195
196 #define f(x, y) f(x, y)
197 void f(int x, int y);
198
199 * - `Rule 5.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_05_06.c>`_
200 - Required
201 - A typedef name shall be a unique identifier
202 -
203
204 * - `Rule 6.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_06_01.c>`_
205 - Required
206 - Bit-fields shall only be declared with an appropriate type
207 - In addition to the C99 types, we also consider appropriate types
208 enum and all explicitly signed / unsigned integer types.
209
210 * - `Rule 6.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_06_02.c>`_
211 - Required
212 - Single-bit named bit fields shall not be of a signed type
213 -
214
215 * - `Rule 7.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_07_01.c>`_
216 - Required
217 - Octal constants shall not be used
218 -
219
220 * - `Rule 7.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_07_02.c>`_
221 - Required
222 - A "u" or "U" suffix shall be applied to all integer constants
223 that are represented in an unsigned type
224 - The rule asks that any integer literal that is implicitly
225 unsigned is made explicitly unsigned by using one of the
226 indicated suffixes. As an example, on a machine where the int
227 type is 32-bit wide, 0x77777777 is signed whereas 0x80000000 is
228 (implicitly) unsigned. In order to comply with the rule, the
229 latter should be rewritten as either 0x80000000u or 0x80000000U.
230 Consistency considerations may suggest using the same suffix even
231 when not required by the rule. For instance, if one has:
232
233 Original: f(0x77777777); f(0x80000000);
234
235 one should do
236
237 Solution 1: f(0x77777777U); f(0x80000000U);
238
239 over
240
241 Solution 2: f(0x77777777); f(0x80000000U);
242
243 after having ascertained that "Solution 1" is compatible with the
244 intended semantics.
245
246 * - `Rule 7.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_07_03.c>`_
247 - Required
248 - The lowercase character l shall not be used in a literal suffix
249 -
250
251 * - `Rule 7.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_07_04.c>`_
252 - Required
253 - A string literal shall not be assigned to an object unless the
254 object type is pointer to const-qualified char
255 - All "character types" are permitted, as long as the string
256 element type and the character type match. (There should be no
257 casts.) Assigning a string literal to any object with type
258 "pointer to const-qualified void" is allowed.
259
260 * - `Rule 8.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_01.c>`_
261 - Required
262 - Types shall be explicitly specified
263 -
264
265 * - `Rule 8.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_02.c>`_
266 - Required
267 - Function types shall be in prototype form with named parameters
268 - Clarification: both function and function pointers types shall
269 have named parameters.
270
271 * - `Rule 8.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_03.c>`_
272 - Required
273 - All declarations of an object or function shall use the same
274 names and type qualifiers
275 - The type ret_t maybe be deliberately used and defined as int or
276 long depending on the type of guest to service
277
278 * - `Rule 8.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_04.c>`_
279 - Required
280 - A compatible declaration shall be visible when an object or
281 function with external linkage is defined
282 - Allowed exceptions: asm-offsets.c, definitions for asm modules
283 not called from C code, gcov_base.c
284
285 * - `Rule 8.5 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_05_2.c>`_
286 - Required
287 - An external object or function shall be declared once in one and only one file
288 -
289
290 * - `Rule 8.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_06_2.c>`_
291 - Required
292 - An identifier with external linkage shall have exactly one
293 external definition
294 - Declarations without definitions are allowed (specifically when
295 the definition is compiled-out or optimized-out by the compiler)
296
297 * - `Rule 8.8 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_08.c>`_
298 - Required
299 - The static storage class specifier shall be used in all
300 declarations of objects and functions that have internal linkage
301 -
302
303 * - `Rule 8.10 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_10.c>`_
304 - Required
305 - An inline function shall be declared with the static storage class
306 - gnu_inline (without static) is allowed.
307
308 * - `Rule 8.12 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_12.c>`_
309 - Required
310 - Within an enumerator list the value of an implicitly-specified
311 enumeration constant shall be unique
312 -
313
314 * - `Rule 8.14 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_14.c>`_
315 - Required
316 - The restrict type qualifier shall not be used
317 -
318
319 * - `Rule 9.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_09_01.c>`_
320 - Mandatory
321 - The value of an object with automatic storage duration shall not
322 be read before it has been set
323 - Rule clarification: do not use variables before they are
324 initialized. An explicit initializer is not necessarily required.
325 Try reducing the scope of the variable. If an explicit
326 initializer is added, consider initializing the variable to a
327 poison value.
328
329 * - `Rule 9.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_09_02.c>`_
330 - Required
331 - The initializer for an aggregate or union shall be enclosed in
332 braces
333 -
334
335 * - `Rule 9.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_09_03.c>`_
336 - Required
337 - Arrays shall not be partially initialized
338 - {} is also allowed to specify explicit zero-initialization
339
340 * - `Rule 9.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_09_04.c>`_
341 - Required
342 - An element of an object shall not be initialized more than once
343 -
344
345 * - `Rule 10.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_10_01.c>`_
346 - Required
347 - Operands shall not be of an inappropriate essential type
348 - The following are allowed:
349 - Value-preserving conversions of integer constants
350 - Bitwise and, or, xor, one's complement, bitwise and assignment,
351 bitwise or assignment, bitwise xor assignment (bitwise and, or, xor
352 are safe on non-negative integers; also Xen assumes two's complement
353 representation)
354 - Left shift, right shift, left shift assignment, right shift
355 assignment (see C-language-toolchain.rst for uses of
356 compilers' extensions)
357 - Implicit conversions to boolean for conditionals (?: if while
358 for) and logical operators (! || &&)
359 - The essential type model allows the constants defined by anonymous
360 enums (e.g., enum { A, B, C }) to be used as operands to arithmetic
361 operators, as they have a signed essential type.
362
363 * - `Rule 10.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_10_02.c>`_
364 - Required
365 - Expressions of essentially character type shall not be used
366 inappropriately in addition and subtraction operations
367 -
368
369 * - `Rule 10.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_10_03.c>`_
370 - Required
371 - The value of an expression shall not be assigned to an object
372 with a narrower essential type or of a different essential type
373 category
374 - Please beware that this rule has many violations in the Xen
375 codebase today, and its adoption is aspirational. However, when
376 submitting new patches please try to decrease the number of
377 violations when possible.
378
379 gcc has a helpful warning that can help you spot and remove
380 violations of this kind: conversion. For instance, you can use
381 it as follows:
382
383 CFLAGS="-Wconversion -Wno-error=sign-conversion -Wno-error=conversion" make -C xen
384
385 * - `Rule 10.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_10_04.c>`_
386 - Required
387 - Both operands of an operator in which the usual arithmetic
388 conversions are performed shall have the same essential type
389 category
390 - Please beware that this rule has many violations in the Xen
391 codebase today, and its adoption is aspirational. However, when
392 submitting new patches please try to decrease the number of
393 violations when possible.
394
395 gcc has a helpful warning that can help you spot and remove
396 violations of this kind: arith-conversion. For instance, you
397 can use it as follows:
398
399 CFLAGS="-Warith-conversion -Wno-error=arith-conversion" make -C xen
400
401 * - `Rule 11.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_11_01.c>`_
402 - Required
403 - Conversions shall not be performed between a pointer to a
404 function and any other type
405 - All conversions to integer types are permitted if the destination
406 type has enough bits to hold the entire value. Conversions to
407 bool and void* are permitted.
408
409 * - `Rule 11.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_11_02.c>`_
410 - Required
411 - Conversions shall not be performed between a pointer to an
412 incomplete type and any other type
413 - All conversions to integer types are permitted if the destination
414 type has enough bits to hold the entire value. Conversions to
415 bool and void* are permitted.
416
417 * - `Rule 11.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_11_03.c>`_
418 - Required
419 - A cast shall not be performed between a pointer to object type
420 and a pointer to a different object type
421 -
422
423 * - `Rule 11.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_11_06.c>`_
424 - Required
425 - A cast shall not be performed between pointer to void and an
426 arithmetic type
427 - All conversions to integer types are permitted if the destination
428 type has enough bits to hold the entire value. Conversions to
429 bool are permitted.
430
431 * - `Rule 11.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_11_07.c>`_
432 - Required
433 - A cast shall not be performed between pointer to object and a noninteger arithmetic type
434 -
435
436 * - `Rule 11.8 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_11_08.c>`_
437 - Required
438 - A cast shall not remove any const or volatile qualification from the type pointed to by a pointer
439 -
440
441 * - `Rule 11.9 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_11_09.c>`_
442 - Required
443 - The macro NULL shall be the only permitted form of null pointer constant
444 -
445
446 * - `Rule 12.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_12_02.c>`_
447 - Required
448 - The right hand operand of a shift operator shall lie in the range
449 zero to one less than the width in bits of the essential type of
450 the left hand operand
451 - We rely on gcc -fsanitize=undefined to check for dangerious
452 violations to this rule and to ensure compliance
453
454 * - `Rule 12.5 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_12_05.c>`_
455 - Mandatory
456 - The sizeof operator shall not have an operand which is a function
457 parameter declared as "array of type"
458 -
459
460 * - `Rule 13.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_13_01_1.c>`_
461 - Required
462 - Initializer lists shall not contain persistent side effects
463 -
464
465 * - `Rule 13.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_13_02.c>`_
466 - Required
467 - The value of an expression and its persistent side-effects shall
468 be the same under all permitted evaluation orders
469 - Be aware that the static analysis tool Eclair might report
470 several findings for Rule 13.2 of type "caution". These are
471 instances where Eclair is unable to verify that the code is valid
472 in regard to Rule 13.2. Caution reports might not be violations.
473 The rule should be followed in any case in new code submitted.
474
475 * - `Rule 13.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_13_06.c>`_
476 - Required
477 - The operand of the sizeof operator shall not contain any
478 expression which has potential side effects
479 - In addition to sizeof, we also want to apply the rule to typeof
480 and alignof
481
482 * - `Rule 14.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_01.c>`_
483 - Required
484 - A loop counter shall not have essentially floating type
485 -
486
487 * - `Rule 14.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_03.c>`_
488 - Required
489 - Controlling expressions shall not be invariant
490 - Due to the extensive usage of IS_ENABLED, sizeof compile-time
491 checks, and other constructs that are detected as errors by MISRA
492 C scanners, managing the configuration of a MISRA C scanner for
493 this rule would be unmanageable. Thus, this rule is adopted with
494 a project-wide deviation on if, ?:, switch(sizeof(...)), and
495 switch(offsetof(...)) statements.
496
497 while(0) and while(1) and alike are allowed.
498
499 * - `Rule 14.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_04.c>`_
500 - Required
501 - The controlling expression of an if-statement and the controlling
502 expression of an iteration-statement shall have essentially
503 Boolean type
504 - Automatic conversions of integer types to bool are permitted.
505 Automatic conversions of pointer types to bool are permitted.
506 This rule still applies to enum types.
507
508 * - `Rule 16.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_03.c>`_
509 - Required
510 - An unconditional break statement shall terminate every switch-clause_
511 - In addition to break, also other unconditional flow control statements
512 such as continue, return, goto are allowed.
513
514 * - `Rule 16.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_04.c>`_
515 - Required
516 - Every switch statement shall have a default label
517 - Switch statements with enums as controlling expression don't need
518 a default label as gcc -Wall enables -Wswitch which warns (and
519 breaks the build as we use -Werror) if one of the enum labels is
520 missing from the switch.
521
522 Switch statements with integer types as controlling expression
523 should have a default label:
524
525 - if the switch is expected to handle all possible cases
526 explicitly, then a default label shall be added to handle
527 unexpected error conditions, using BUG(), ASSERT(), WARN(),
528 domain_crash(), or other appropriate methods;
529
530 - if the switch is expected to handle a subset of all possible
531 cases, then an empty default label shall be added with an
532 in-code comment on top of the default label with a reason and
533 when possible a more detailed explanation. Example::
534
535 default:
536 /* Notifier pattern */
537 break;
538
539 * - `Rule 16.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_02.c>`_
540 - Required
541 - A switch label shall only be used when the most closely-enclosing
542 compound statement is the body of a switch statement
543 - The x86 emulator (xen/arch/x86/x86_emulate*) is exempt from
544 compliance with this rule. Efforts to make the x86 emulator
545 adhere to Rule 16.2 would result in increased complexity and
546 maintenance difficulty, and could potentially introduce bugs.
547
548 * - `Rule 16.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_06.c>`_
549 - Required
550 - Every switch statement shall have at least two switch-clauses
551 - Single-clause switches are allowed when they do not involve a
552 default label.
553
554 * - `Rule 16.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_07.c>`_
555 - Required
556 - A switch-expression shall not have essentially Boolean type
557 -
558
559 * - `Rule 17.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_17_01.c>`_
560 - Required
561 - The features of <stdarg.h> shall not be used
562 -
563
564 * - `Rule 17.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_17_02.c>`_
565 - Required
566 - Functions shall not call themselves, either directly or indirectly
567 - Limited forms of recursion are allowed if the recursion is bound
568 (there is an upper limit and the upper limit is enforced.) The
569 bounding should be explained in a comment or in a deviation.
570
571 * - `Rule 17.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_17_03.c>`_
572 - Mandatory
573 - A function shall not be declared implicitly
574 -
575
576 * - `Rule 17.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_17_04.c>`_
577 - Mandatory
578 - All exit paths from a function with non-void return type shall
579 have an explicit return statement with an expression
580 -
581
582 * - `Rule 17.5 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_17_05.c>`_
583 - Advisory
584 - The function argument corresponding to a parameter declared to
585 have an array type shall have an appropriate number of elements
586 -
587
588 * - `Rule 17.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_17_06.c>`_
589 - Mandatory
590 - The declaration of an array parameter shall not contain the
591 static keyword between the [ ]
592 -
593
594 * - `Rule 17.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_17_07.c>`_
595 - Required
596 - The value returned by a function having non-void return type
597 shall be used
598 - Please beware that this rule has many violations in the Xen
599 codebase today, and its adoption is aspirational. However, when
600 submitting new patches please try to decrease the number of
601 violations when possible.
602
603 * - `Rule 18.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_18_01.c>`_
604 - Required
605 - A pointer resulting from arithmetic on a pointer operand shall
606 address an element of the same array as that pointer operand
607 -
608
609 * - `Rule 18.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_18_02.c>`_
610 - Required
611 - Subtraction between pointers shall only be applied to pointers
612 that address elements of the same array
613 - Be aware that the static analysis tool Eclair might report
614 several findings for Rule 18.2 of type "caution". These are
615 instances where Eclair is unable to verify that the code is valid
616 in regard to Rule 18.2. Caution reports might not be violations.
617 The rule should be followed in any case in new code submitted.
618
619 * - `Rule 18.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_18_03.c>`_
620 - Required
621 - The relational operators > >= < and <= shall not be applied to objects of pointer type except where they point into the same object
622 -
623
624 * - `Rule 18.8 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_18_08.c>`_
625 - Required
626 - Variable-length array types shall not be used
627 -
628
629 * - `Rule 18.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_18_06_1.c>`_
630 - Required
631 - The address of an object with automatic storage shall not be
632 copied to another object that persists after the first object has
633 ceased to exist
634 -
635
636 * - `Rule 19.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_19_01.c>`_
637 - Mandatory
638 - An object shall not be assigned or copied to an overlapping
639 object
640 - Be aware that the static analysis tool Eclair might report
641 several findings for Rule 19.1 of type "caution". These are
642 instances where Eclair is unable to verify that the code is valid
643 in regard to Rule 19.1. Caution reports are not violations.
644
645 * - `Rule 20.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_20_02.c>`_
646 - Required
647 - The ', " or \ characters and the /* or // character sequences
648 shall not occur in a header file name
649 -
650
651 * - `Rule 20.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_20_03.c>`_
652 - Required
653 - The #include directive shall be followed by either a <filename>
654 or "filename" sequence
655 -
656
657 * - `Rule 20.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_20_04.c>`_
658 - Required
659 - A macro shall not be defined with the same name as a keyword
660 -
661
662 * - `Rule 20.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_20_06.c>`_
663 - Required
664 - Tokens that look like a preprocessing directive shall not occur
665 within a macro argument
666 -
667
668 * - `Rule 20.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_20_07.c>`_
669 - Required
670 - Expressions resulting from the expansion of macro parameters
671 shall be enclosed in parentheses
672 - Extra parentheses are not required when macro parameters are used
673 as function arguments, as macro arguments, array indices, lhs in
674 assignments or as initializers in initalizer lists. In addition,
675 the use of a named variable argument in a macro that would constitute
676 a violation of the rule is allowed by ECLAIR as an extension of the
677 MISRA guideline, since it may not always be possible to parenthesize
678 such argument and the feature is non-standard::
679
680 #define M(args...) args
681 #if M(1) + 0
682
683 * - `Rule 20.9 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_20_09.c>`_
684 - Required
685 - All identifiers used in the controlling expression of #if or
686 #elif preprocessing directives shall be #define'd before
687 evaluation
688 -
689
690 * - `Rule 20.11 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_20_11.c>`_
691 - Required
692 - A macro parameter immediately following a # operator shall not
693 immediately be followed by a ## operator
694 -
695
696 * - `Rule 20.12 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_20_12.c>`_
697 - Required
698 - A macro parameter used as an operand to the # or ## operators,
699 which is itself subject to further macro replacement, shall only
700 be used as an operand to these operators
701 - Variadic macros are allowed to violate the rule.
702
703 * - `Rule 20.13 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_20_13.c>`_
704 - Required
705 - A line whose first token is # shall be a valid preprocessing
706 directive
707 -
708
709 * - `Rule 20.14 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_20_14.c>`_
710 - Required
711 - All #else #elif and #endif preprocessor directives shall reside
712 in the same file as the #if #ifdef or #ifndef directive to which
713 they are related
714 -
715
716 * - `Rule 21.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_01.c>`_
717 - Required
718 - #define and #undef shall not be used on a reserved identifier or
719 reserved macro name
720 - Identifiers starting with an underscore followed by another underscore
721 or an upper-case letter are reserved. Today Xen uses many, such as
722 header guards and bitwise manipulation functions. Upon analysis it turns
723 out Xen identifiers do not clash with the identifiers used by modern
724 GCC, but that is not a guarantee that there won't be a naming clash in
725 the future or with another compiler. For these reasons we discourage
726 the introduction of new reserved identifiers in Xen, and we see it as
727 positive the reduction of reserved identifiers. At the same time,
728 certain identifiers starting with two underscores are also commonly used
729 in Linux (e.g. __set_bit) and we don't think it would be an improvement
730 to rename them.
731
732 * - `Rule 21.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_02.c>`_
733 - Required
734 - A reserved identifier or reserved macro name shall not be
735 declared
736 - See comment for Rule 21.1
737
738 * - `Rule 21.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_03.c>`_
739 - Required
740 - The memory allocation and deallocation functions of <stdlib.h>
741 shall not be used
742 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
743
744 * - `Rule 21.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_04.c>`_
745 - Required
746 - The standard header file <setjmp.h> shall not be used
747 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
748
749 * - `Rule 21.5 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_05.c>`_
750 - Required
751 - The standard header file <signal.h> shall not be used
752 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
753
754 * - `Rule 21.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_06.c>`_
755 - Required
756 - The Standard Library input/output routines shall not be used
757 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
758
759 * - `Rule 21.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_07.c>`_
760 - Required
761 - The Standard Library functions atof, atoi, atol and atoll of
762 <stdlib.h> shall not be used
763 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
764
765 * - `Rule 21.8 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_08.c>`_
766 - Required
767 - The Standard Library functions abort, exit and system of
768 <stdlib.h> shall not be used
769 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
770
771 * - `Rule 21.9 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_09.c>`_
772 - Required
773 - The library functions bsearch and qsort of <stdlib.h> shall not be used
774 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
775
776 * - `Rule 21.10 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_10.c>`_
777 - Required
778 - The Standard Library time and date routines shall not be used
779 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
780
781 * - `Rule 21.11 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_11.c>`_
782 - Required
783 - The standard header file <tgmath.h> shall not be used
784 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
785
786 * - `Rule 21.12 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_12.c>`_
787 - Advisory
788 - The exception handling features of <fenv.h> should not be used
789 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
790
791 * - `Rule 21.13 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_13.c>`_
792 - Mandatory
793 - Any value passed to a function in <ctype.h> shall be representable as an
794 unsigned char or be the value EOF
795 -
796
797 * - `Rule 21.14 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_14.c>`_
798 - Required
799 - The Standard Library function memcmp shall not be used to compare
800 null terminated strings
801 -
802
803 * - `Rule 21.15 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_15.c>`_
804 - Required
805 - The pointer arguments to the Standard Library functions memcpy,
806 memmove and memcmp shall be pointers to qualified or unqualified
807 versions of compatible types
808 -
809
810 * - `Rule 21.16 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_16.c>`_
811 - Required
812 - The pointer arguments to the Standard Library function memcmp
813 shall point to either a pointer type, an essentially signed type,
814 an essentially unsigned type, an essentially Boolean type or an
815 essentially enum type
816 - void* arguments are allowed
817
818 * - `Rule 21.17 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_17.c>`_
819 - Mandatory
820 - Use of the string handling functions from <string.h> shall not result in
821 accesses beyond the bounds of the objects referenced by their pointer
822 parameters
823 -
824
825 * - `Rule 21.18 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_18.c>`_
826 - Mandatory
827 - The size_t argument passed to any function in <string.h> shall have an
828 appropriate value
829 -
830
831 * - `Rule 21.19 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_19.c>`_
832 - Mandatory
833 - The pointers returned by the Standard Library functions localeconv,
834 getenv, setlocale or, strerror shall only be used as if they have
835 pointer to const-qualified type
836 -
837
838 * - `Rule 21.20 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_21_20.c>`_
839 - Mandatory
840 - The pointer returned by the Standard Library functions asctime ctime
841 gmtime localtime localeconv getenv setlocale or strerror shall not be
842 used following a subsequent call to the same function
843 -
844
845 * - `Rule 21.21 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/>`_
846 - Required
847 - The Standard Library function system of <stdlib.h> shall not be used
848 -
849
850 * - `Rule 22.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_22_01.c>`_
851 - Required
852 - All resources obtained dynamically by means of Standard Library
853 functions shall be explicitly released
854 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
855
856 * - `Rule 22.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_22_02.c>`_
857 - Mandatory
858 - A block of memory shall only be freed if it was allocated by means of a
859 Standard Library function
860 -
861
862 * - `Rule 22.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_22_03.c>`_
863 - Required
864 - The same file shall not be open for read and write access at the
865 same time on different streams
866 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
867
868 * - `Rule 22.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_22_04.c>`_
869 - Mandatory
870 - There shall be no attempt to write to a stream which has been opened as
871 read-only
872 -
873
874 * - `Rule 22.5 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_22_05.c>`_
875 - Mandatory
876 - A pointer to a FILE object shall not be dereferenced
877 -
878
879 * - `Rule 22.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_22_06.c>`_
880 - Mandatory
881 - The value of a pointer to a FILE shall not be used after the associated
882 stream has been closed
883 -
884
885 * - `Rule 22.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_22_07.c>`_
886 - Required
887 - The macro EOF shall only be compared with the unmodified return
888 value from any Standard Library function capable of returning EOF
889 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
890
891 * - `Rule 22.8 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_22_08.c>`_
892 - Required
893 - The value of errno shall be set to zero prior to a call to an
894 errno-setting-function
895 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
896
897 * - `Rule 22.9 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_22_09.c>`_
898 - Required
899 - The value of errno shall be tested against zero after calling an
900 errno-setting-function
901 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
902
903 * - `Rule 22.10 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_22_10.c>`_
904 - Required
905 - The value of errno shall only be tested when the last function to
906 be called was an errno-setting-function
907 - Xen doesn't provide, use, or link against a Standard Library [#xen-stdlib]_
908
909
910 Terms & Definitions
911 -------------------
912
913 .. _switch-clause:
914
915 A *switch clause* can be defined as:
916 "the non-empty list of statements which follows a non-empty list of
917 case/default labels".
918 A formal definition is available within the amplification of MISRA C:2012
919 Rule 16.1.
920
921 .. rubric:: Footnotes
922
923 .. [#xen-stdlib] Xen implements itself a few functions with names that match
924 the corresponding function names of the Standard Library for developers'
925 convenience. These functions are part of the Xen code and subject to
926 analysis.