AutosarC++18_03-A12.0.2¶
Bitwise operations and operations that assume data representation in memory shall not be performed on objects
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | partially automated |
Rationale
Object representations may consist of more than only the declared fields (unless the objects are standard-layout or trivially copyable). Performing bitwise operations on objects may access bits that are not part of the value representation, which may lead to undefined behavior. Operations on objects (e.g. initialization, copying, comparing, setting, accessing) shall be done by dedicated constructors, overloaded operators, accessors or mutators.
Example
// $Id: A12-0-2.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
//
#include <cstdint>
#include <cstring>
class A {
public:
A() = default;
A(uint8_t c, uint32_t i, int8_t d, int32_t h) : c(c), i(i), d(d), h(h) {}
bool operator==(const A &rhs) const noexcept {
return c == rhs.c && i == rhs.i && d == rhs.d && h == rhs.h;
}
private:
uint8_t c;
uint32_t i;
int8_t d;
int32_t h;
};
int main(void) {
A noninit;
// setting field c
std::memset(&noninit, 3, 1); // non-compliant
// setting field i
std::memset(((uint8_t *)&noninit) + sizeof(uint8_t) + 3, 5,
1); // non-compliant
A init(3, 5, 7, 9); // compliant
if (noninit == init) // compliant
{
}
if (0 == std::memcmp(&noninit, &init,
sizeof(init))) { // non-compliant, includes padding bytes
}
return 0;
}
See Also
- JSF December 2005 [7]: AV Rule 156: All the members of a structure (or class) shall be named and shall only be accessed via their names.
- JSF December 2005 [7]: AV Rule 210: Algorithms shall not make assumptions concerning how data is represented in memory (e.g. big endian vs. little endian, base class subobject ordering in derived classes, nonstatic data member ordering across access specifiers, etc.)
- JSF December 2005 [7]: AV Rule 210.1: Algorithms shall not make assumptions concerning the order of allocation of nonstatic data members separated by an access specifier.
- JSF December 2005 [7]: AV Rule 211: Algorithms shall not assume that shorts, ints, longs, floats, doubles or long doubles begin at particular addresses.
- SEI CERT C++ Coding Standard [9]: EXP42-C: Do not compare padding data
- SEI CERT C++ Coding Standard [9]: EXP62-C: Do not access the bits of an object representation that are not part of the object’s value representation
- SEI CERT C++ Coding Standard [9]: OOP57-CPP: Prefer special member functions and overloaded operators to C Standard Library functions
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
disallowed_memcmp_pointer_arg |
Disallowed type of pointer argument. |
None |
False |
invalid_memset |
memset shall not be used with non-trivially default constructible types. |
None |
False |
memcmp_char_pointer_arg |
memcmp shall not be used with char pointer argument, use strncmp instead. |
None |
False |
memcmp_float |
memcmp shall not be used to compare floats as the same value may be stored using different representations. |
None |
False |
memcmp_padding |
memcmp shall not be used to compare structs with padding. |
None |
False |
memcmp_struct_pointer_arg |
memcmp shall not be used with struct pointer argument as it would compare padding as well. |
None |
False |
memcmp_union_pointer_arg |
memcmp shall not be used with union pointer argument as it would compare padding and different kinds of representation. |
None |
False |
partial_memory_operation |
Memory operation shall not use a size smaller than sizeof the object type. |
None |
False |
pointer_arithmetic_on_object |
Memory operation shall not be performed on pointer arithmetic applied to object addresses. |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allow_aggregates_with_nsdmi¶
allow_aggregates_with_nsdmi : bool = True
memset usage with aggregates that are not trivially
default constructible only because of a default non-static data member initializer
(NSDMI).
allow_char¶
allow_char : bool = True
memcmp on char type.
allow_composites_without_padding¶
allow_composites_without_padding : bool = True
memcmp on structs and unions that have no
padding bytes.
allow_float¶
allow_float : bool = False
memcmp on floating point types.
functions¶
functions : set[bauhaus.analysis.config.QualifiedName] = {'memcmp'}
ignore_calls_in_functions¶
ignore_calls_in_functions : set[bauhaus.analysis.config.QualifiedName] = set()
relevant_memory_functions¶
relevant_memory_functions : set[bauhaus.analysis.config.QualifiedName] = {'memcmp', 'memcpy', 'memmove', 'memset'}
relevant_memset_functions¶
relevant_memset_functions : set[bauhaus.analysis.config.QualifiedName] = {'memset'}