AutosarC++17_10-A12.8.3¶
Moved-from object shall not be read-accessed
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | partially automated |
Rationale
Except in rare circumstances, an object will be left in an unspecified state after its values has been moved into another object. Accessing data members of such object may result in abnormal behavior and portability concerns.
Exceptions
It is permitted to access internals of a moved-from object if it is guaranteed to be left in a well-specified state.
The following Standard Template Library functions are guaranteed to leave the moved-from object in a well-specified state:
- move construction, move assignment, “converting” move construction and “converting” move assignment of std::unique_ptr type
- move construction, move assignment, “converting” move construction, “converting” move assignment of std::shared_ptr type
- move construction and move assignment from a std::unique_ptr of std::shared_ptr type
- move construction, move assignment, “converting” move construction and “converting” move assignment of std::weak_ptr type
- std::move() of std::basic_ios type
- move constructor and move assignment of std::basic_filebuf type
- move constructor and move assignment of std::thread type
- move constructor and move assignment of std::unique_lock type
- move constructor and move assignment of std::shared_lock type
- move constructor and move assignment of std::promise type
- move constructor and move assignment of std::future type
- move construction, move assignment, “converting” move construction and “converting” move assignment of std::shared_future type
- move constructor and move assignment of std::packaged_task type
Example
// $Id: A12-8-3.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
void F1() {
std::string s1{"string"};
std::string s2{std::move(s1)};
// ...
std::cout << s1 << "\n"; // Non-compliant - s1 does not contain "string"
// value after move operation
}
void F2() {
std::unique_ptr<std::int32_t> ptr1 = std::make_unique<std::int32_t>(0);
std::unique_ptr<std::int32_t> ptr2{std::move(ptr1)};
std::cout << ptr1.get() << std::endl; // Compliant by exception - move
// construction of std::unique_ptr
// leaves moved-from object in a
// well-specified state
}
See Also
- SEI CERT C++ [9]: EXP63-CPP Do not rely on the value of a moved-from object.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
aliased_reference_arguments |
Don’t pass the same object as both a const-reference and an rvalue-reference argument |
None |
False |
moved_from_read |
Don’t read-access a moved-from object |
None |
False |
order_dependent_moved_from_read |
Don’t read-access a moved-from object in an unsequenced argument |
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
moving_functions¶
moving_functions : set[bauhaus.analysis.config.QualifiedName] = set()
read_move_function_exceptions¶
read_move_function_exceptions
Names of functions that are considered to leave the moved from objects in a well-specified state and are therefore except.Type: set[bauhaus.analysis.config.QualifiedName]
Default:
{'std::basic_filebuf::basic_filebuf', 'std::basic_filebuf::operator=', 'std::future::future', 'std::future::operator=', 'std::get', 'std::make_shared', 'std::make_unique', 'std::packaged_task::operator=', 'std::packaged_task::packaged_task', 'std::promise::operator=', 'std::promise::promise', 'std::shared_future::operator=', 'std::shared_future::shared_future', 'std::shared_lock::operator=', 'std::shared_lock::shared_lock', 'std::shared_ptr::operator=', 'std::shared_ptr::shared_ptr', 'std::thread::operator=', 'std::thread::thread', 'std::unique_lock::operator=', 'std::unique_lock::unique_lock', 'std::unique_ptr::operator=', 'std::unique_ptr::unique_ptr', 'std::weak_ptr::operator=', 'std::weak_ptr::weak_ptr'}
read_move_type_exceptions¶
read_move_type_exceptions : set[bauhaus.analysis.config.QualifiedName] = {'std::basic_ios'}
restore_subobjects_with_enclosing_objects¶
restore_subobjects_with_enclosing_objects : bool = True
restoring_functions¶
restoring_functions
Names of functions that are considered to restore an object to a well-specified state.Type: set[bauhaus.analysis.config.QualifiedName]
Default:
{'std::basic_string::clear', 'std::deque::clear', 'std::forward_list::clear', 'std::list::clear', 'std::map::clear', 'std::multimap::clear', 'std::multiset::clear', 'std::set::clear', 'std::unordered_map::clear', 'std::unordered_multimap::clear', 'std::unordered_multiset::clear', 'std::unordered_set::clear', 'std::vector::clear'}
use_static_semantic_analysis¶
use_static_semantic_analysis : bool = True
StaticSemanticAnalysis to be enabled, but also
will not produce any additional results if it is not.