Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2472,7 +2472,14 @@ def get_category(essential_type):
rhs_category = get_category(rhs)
if lhs_category and rhs_category and lhs_category != rhs_category and rhs_category not in ('signed','unsigned'):
self.reportError(tok, 10, 3)
if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs) and (lhs != "bool" or tok.astOperand2.str not in ('0','1')):
find_std = cfg.standards.c if cfg.standards and cfg.standards.c else self.stdversion
allow_bool_literal_0_1 = (
find_std == "c89" and
lhs == "bool" and
tok.astOperand2 and
tok.astOperand2.str in ('0', '1')
)
if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs) and not allow_bool_literal_0_1:
self.reportError(tok, 10, 3)

def misra_10_4(self, data):
Expand Down
11 changes: 11 additions & 0 deletions addons/test/misra/misra-test-c11.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ~/cppcheck/cppcheck --dump misra/misra-test-c11.c --std=c11
// ~/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

#include <stdbool.h>
#include <stdint.h>

typedef unsigned int UINT_TYPEDEF;
Expand All @@ -23,3 +24,13 @@ static void misra6_1_fn(void) {
struct_with_bitfields s;
s.h = 61;
}

static void misra_10_3_c11(void) {
bool b = false;
bool b0 = 0; // 10.3
bool b1 = 1; // 10.3
b = 0; // 10.3
b = 1; // 10.3
b = false; // no-warning
b = true; // no-warning
}
4 changes: 4 additions & 0 deletions addons/test/misra/misra-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,10 @@ static void misra_10_3(uint32_t u32a, uint32_t u32b) {
const char c = '0'; // no-warning
bool b = true; // no-warning
uint32_t u = UINT32_C(10); // no-warning
bool b0 = 0; // no-warning
bool b1 = 1; // no-warning
b = 0; // no-warning
b = 1; // no-warning
}

static void misra_10_4(u32 x, s32 y) {
Expand Down
2 changes: 1 addition & 1 deletion releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Major bug fixes & crashes:
-

New checks:
-
- 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.

C/C++ support:
-
Expand Down
Loading