Skip to content

Commit b70e34d

Browse files
Fix #14607: MISRA: make rule 10.3 warn on bool <- 0/1 in C99 and later (#8340)
Fix #14607. This updates MISRA rule 10.3 in `addons/misra.py` so that assignments of integer literals `0` and `1` to `bool` are only exempted in C89. Behavior after this change: - C89: keep the existing behavior and allow `bool <- 0/1` - C99/C11/C17/C18/C23: report MISRA 10.3 for `bool <- 0/1` The motivation is that in C99 and later, `bool`/`true`/`false` are available, so the current unconditional exemption for `0` and `1` causes a MISRA 10.3 false negative. Changes: - update `addons/misra.py` rule 10.3 to make the `bool <- 0/1` exemption standard-dependent - add C89-side coverage in `addons/test/misra/misra-test.c` - add C11-side coverage in `addons/test/misra/misra-test-c11.c` - add a release notes entry Related Context: - Earlier discussion: #7110 - Downstream issue: commaai/panda#2105
1 parent 18492b0 commit b70e34d

4 files changed

Lines changed: 24 additions & 2 deletions

File tree

addons/misra.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,14 @@ def get_category(essential_type):
24722472
rhs_category = get_category(rhs)
24732473
if lhs_category and rhs_category and lhs_category != rhs_category and rhs_category not in ('signed','unsigned'):
24742474
self.reportError(tok, 10, 3)
2475-
if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs) and (lhs != "bool" or tok.astOperand2.str not in ('0','1')):
2475+
find_std = cfg.standards.c if cfg.standards and cfg.standards.c else self.stdversion
2476+
allow_bool_literal_0_1 = (
2477+
find_std == "c89" and
2478+
lhs == "bool" and
2479+
tok.astOperand2 and
2480+
tok.astOperand2.str in ('0', '1')
2481+
)
2482+
if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs) and not allow_bool_literal_0_1:
24762483
self.reportError(tok, 10, 3)
24772484

24782485
def misra_10_4(self, data):

addons/test/misra/misra-test-c11.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// ~/cppcheck/cppcheck --dump misra/misra-test-c11.c --std=c11
33
// ~/cppcheck/cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra/misra-test-c11.c --std=c11 --platform=unix64 && python3 ../misra.py -verify misra/misra-test-c11.c.dump
44

5+
#include <stdbool.h>
56
#include <stdint.h>
67

78
typedef unsigned int UINT_TYPEDEF;
@@ -23,3 +24,13 @@ static void misra6_1_fn(void) {
2324
struct_with_bitfields s;
2425
s.h = 61;
2526
}
27+
28+
static void misra_10_3_c11(void) {
29+
bool b = false;
30+
bool b0 = 0; // 10.3
31+
bool b1 = 1; // 10.3
32+
b = 0; // 10.3
33+
b = 1; // 10.3
34+
b = false; // no-warning
35+
b = true; // no-warning
36+
}

addons/test/misra/misra-test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,10 @@ static void misra_10_3(uint32_t u32a, uint32_t u32b) {
726726
const char c = '0'; // no-warning
727727
bool b = true; // no-warning
728728
uint32_t u = UINT32_C(10); // no-warning
729+
bool b0 = 0; // no-warning
730+
bool b1 = 1; // no-warning
731+
b = 0; // no-warning
732+
b = 1; // no-warning
729733
}
730734

731735
static void misra_10_4(u32 x, s32 y) {

releasenotes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Major bug fixes & crashes:
55
-
66

77
New checks:
8-
-
8+
- MISRA C 2012 rule 10.3 now warns on assigning integer literals 0 and 1 to bool in C99 and later while preserving the existing C89 behavior.
99

1010
C/C++ support:
1111
-

0 commit comments

Comments
 (0)