Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#10723 closed Bugs (fixed)

UNINIT_CTOR(boost::​multi_index::​detail::​bidir_node_iterator<boost::​multi_index::​detail::​ordered_index_node<boost::​multi_index::​detail::​ordered_index_node<boost::​multi_index does not get initialised)

Reported by: anup kumar <anup.shrivastava@…> Owned by: Joaquín M López Muñoz
Milestone: To Be Determined Component: multi_index
Version: Boost 1.54.0 Severity: Cosmetic
Keywords: UNINIT_CTOR Cc:

Description

In default constructor for the class "bidir_node_iterator " the private members are not initialized hence it shows a code quality error UNINIT_CTOR() as defined below :-

template<typename Node> class bidir_node_iterator:

public bidirectional_iterator_helper< bidir_node_iterator<Node>, typename Node::value_type, std::ptrdiff_t, const typename Node::value_type*, const typename Node::value_type&> {

public:

Uninitialized pointer field (UNINIT_CTOR)2. uninit_member: Non-static class member "node" is not initialized in this constructor nor in any functions that it calls.

bidir_node_iterator(){}

* * private:

Node* node;

}

* This Private member has to be initialized in default constructor *

Change History (5)

comment:1 by Joaquín M López Muñoz, 8 years ago

Severity: ProblemCosmetic

Added annotation at

https://github.com/boostorg/multi_index/commit/08fe6a360acf99e1732a54ef8b3114455f59116b

Could you please verify this shuts Coverity down?

comment:2 by Joaquín M López Muñoz, 8 years ago

Resolution: fixed
Status: newclosed

comment:3 by anonymous, 7 years ago

This isn't cosmetic: if the default constructor is used to create an instance, then the member variable "node" isn't initialized via the constructor => this can result in dereferencing an invalid pointer.

comment:4 by anonymous, 7 years ago

Either initialize "node" to NULL, or delete the constructor.

comment:5 by Joaquín M López Muñoz, 7 years ago

Hi anonymous,

The default constructor is needed because it is permissible (and very useful) to default initialize an iterator, so we can't just omit this constructor. When default-initialized, the iterator has a singular value according to the C++ standard terminology in [iterator.requirements.general]/5. Dereferencing a singular value is undefined behavior, so the programmer is not allowed to do it regardless of whether the value of the pointer is null or not. My reluctance to initialize to null comes from the fact that we're introducing a run-time penalty in default construction (even if a negligible one) that, in the best of cases, will only expose undefined behavior more nicely.

Anyway, is perhaps the annotation commited in

https://github.com/boostorg/multi_index/commit/08fe6a360acf99e1732a54ef8b3114455f59116b

not silencing the Coverity warning?

Note: See TracTickets for help on using tickets.