iceoryx icon indicating copy to clipboard operation
iceoryx copied to clipboard

Undefined behavior in all classes with `getMembers` method

Open elfenpiff opened this issue 2 years ago • 1 comments

Required information

Nearly all classes with the method getMembers() have undefined behavior after move since the move constructor and assignment operator have default specifiers which do not invalidate the underlying data pointer.

(Maybe incomplete list) of all affected classes

  • InterfacePort
  • ServerPortUser
  • PublisherPortUser
  • PublisherPortRoudi
  • ServerPortRoudi
  • ApplicationPort
  • SubscriberPortUser
  • ClientPortUser
  • ClientPortRoudi
  • BasePort
  • ChunkQueuePopper
  • ChunkSender
  • ChunkQueuePusher
  • ChunkReceiver
  • ConditionListener
  • ConditionNotifier
  • ChunkDistributor

Solution

  1. For every class an explicit move constructor and move assignment operator has to be implemented which invalidates the pointer to the members. But please pay attention to call the parent move constructor/assignment operator first when there is inheritance involved!
Parent::operator=(std::move(*this));
  1. getMembers must return a reference/const reference to the underlying members since the members should be never nullptr.

elfenpiff avatar Jan 01 '22 20:01 elfenpiff

Maybe we should think of using something like the std::reference_wrapper instead of implementing all the move constructors and assignment operators in the classes itself. The only problem is that the classes are not memcopyable anymore. Not sure if they are now or if it's important.

elBoberido avatar Jan 02 '22 16:01 elBoberido