Possible invalid operation on boolean operand
Operation can exceed precision of Boolean operand or result in arbitrary value
Description
This defect occurs when you use a Boolean operand in an arithmetic, relational, or bitwise operation and:
The Boolean operand has a trap representation. The size of a Boolean type in memory is at least one addressable unit (size of
char
). A Boolean type requires only one bit to represent the valuetrue (1)
orfalse (0)
. The representation of a Boolean operand in memory contains padding bits. The memory representation can result in values that are nottrue
orfalse
, a trap representation.The result of the operation can exceed the precision of the Boolean operand.
For example, in this code snippet:
bool_v >> 2
If the value of
bool_v
istrue (1)
orfalse (0)
, the bitwise shift exceeds the one-bit precision ofbool_v
and always results in0
.If
bool_v
has a trap representation, the result of the operation is an arbitrary value.
Possible invalid operation on boolean operand raises no defect when:
The operation does not result in a precision overflow. For instance, bitwise
&
or|
operations with0x01
or0x00
.The Boolean operand cannot have a trap representation. For instance, a constant expression that results in
0
or1
, or a comparison evaluated totrue
orfalse
.
Risk
Arithmetic, relational, or bitwise operations on a Boolean operand can exceed the operand precision and cause unexpected results when used as a Boolean value. Operations on Boolean operands with trap representations can return arbitrary values.
Fix
Avoid performing operations on Boolean operands other than these operations:
Assignment operation (
=
).Equality operations (
==
or!=
).Logical operations (
&&
,||
, or!
).
Examples
Result Information
Group: Numerical |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
INVALID_OPERATION_ON_BOOLEAN |
Impact: Low |
Version History
Introduced in R2018b
See Also
Bitwise and arithmetic
operation on the same data
| Bitwise operation on negative
value
| Integer conversion
overflow
| Integer
overflow
| Integer precision exceeded
| Shift of a negative
value
| Right operand of shift operation outside allowed
bounds
| Unsigned integer conversion
overflow
| Unsigned integer
overflow
| MISRA C:2004 Rule 12.6
| MISRA C:2012 Rule
10.1
| MISRA C:2012 Rule
12.2
| MISRA C++:2008 Rule 4-5-2
| Find defects
(-checkers)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)