AutosarC++18_03-A12.8.3

Moved-from object shall not be read-accessed

Required inputs: IR

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

moving_functions

moving_functions : set[bauhaus.analysis.config.QualifiedName] = set()

Set of functions considered as putting any parameter passed to them into a moved-from state.
 

read_move_function_exceptions

read_move_function_exceptions

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'}

Names of functions that are considered to leave the moved from objects in a well-specified state and are therefore except.
 

read_move_type_exceptions

read_move_type_exceptions : set[bauhaus.analysis.config.QualifiedName] = {'std::basic_ios'}

Names of types that are considered to be left well-specified state after a move and are therefore except.
 

restore_subobjects_with_enclosing_objects

restore_subobjects_with_enclosing_objects : bool = True

If an object is reset to a not-moved state (e.g., by replacing the moved one with a freshly created one), reset all subobjects (e.g., members) to the not-moved state as well. This is quite costly as objects have to be traversed and their fields have to be identified. Disabling may cause false positives but increases performance.
 

restoring_functions

restoring_functions

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'}

Names of functions that are considered to restore an object to a well-specified state.
 

use_static_semantic_analysis

use_static_semantic_analysis : bool = True

Whether the rule should use the results of the StaticSemanticAnalysis to check object identities. This could lead to additional findings, as additional pairs of moved-from objects and read objects can be detected as referring to the same object. This will not enforce StaticSemanticAnalysis to be enabled, but also will not produce any additional results if it is not.