c – 叮当静态分析仪是否通过从unique_ptrs列表中弹出前端而感到困惑?

前端之家收集整理的这篇文章主要介绍了c – 叮当静态分析仪是否通过从unique_ptrs列表中弹出前端而感到困惑?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
以下C11代码是我认为在cl声中触发假阳性的最小例证:
  1. #include <iostream>
  2. #include <list>
  3. #include <memory>
  4.  
  5. class ElementType {};
  6.  
  7. int main(int argc,const char * argv[]) {
  8. std::list<std::unique_ptr<ElementType>> theList(5);
  9.  
  10. theList.pop_front();
  11.  
  12. for (const auto &element: theList) { // (*)
  13. std::cout << "This should be fine." << std::endl;
  14. }
  15.  
  16. return 0;
  17. }

在由星号(*)标记的行上,cl声分析仪声称

…filePath…/main.cpp:21:29: Use of memory after it is freed (within a call to ‘begin’)

就我的解释而言,这段代码是无害的,但是cl ang不满,std :: list< T> :: pop_front()不仅调用其元素的析构函数,而且还会移动std :: list< ; T> ::开始().通过pop_back替换对pop_front的调用,使分析仪警告消失,甚至通过erase(theList.begin())替换它使它无警告地出现. 我错过了一些事情,还是绊倒了一个错过的案子? 以供参考:
这些结果来自Mac OS X 10.9.2上的XCode 5.1.1(5B1008)

  1. $clang --version
  2. Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
  3. Target: x86_64-apple-darwin13.1.0
  4. Thread model: posix

解决方法

代码看起来不错,

我检查代码from libc++(相关部分),我相信它只是混淆了静态分析仪.

更多细节:

  1. template <class _Tp,class _Alloc>
  2. void list<_Tp,_Alloc>::pop_front()
  3. {
  4. _LIBCPP_ASSERT(!empty(),"list::pop_front() called with empty list");
  5. __node_allocator& __na = base::__node_alloc();
  6. __node_pointer __n = base::__end_.__next_;
  7. base::__unlink_nodes(__n,__n);
  8. --base::__sz();
  9. __node_alloc_traits::destroy(__na,_VSTD::addressof(__n->__value_));
  10. __node_alloc_traits::deallocate(__na,__n,1);
  11. }

列表被实现为循环列表,基于__end_(它是最终指针),因此要获得第一个元素,代码将转到__end _.__ next_.

__unlink_nodes的实现是:

  1. // Unlink nodes [__f,__l]
  2. template <class _Tp,class _Alloc>
  3. inline void __list_imp<_Tp,_Alloc>::__unlink_nodes(__node_pointer __f,__node_pointer __l) noexcept
  4. {
  5. __f->__prev_->__next_ = __l->__next_;
  6. __l->__next_->__prev_ = __f->__prev_;
  7. }

我们可以用一些简单的ASCII艺术来轻松理解它:

  1. Z A B C
  2. +---------+ +---------+ +---------+ +---------+
  3. --| __prev_ |<--| __prev_ |<--| __prev_ |<--| __prev_ |<-
  4. ->| __next_ |-->| __next_ |-->| __next_ |-->| __next_ |--
  5. +---------+ +---------+ +---------+ +---------+

要从此列表中删除范围A-B:

> Z .__ next_必须指向C
> C .__ prev_必须指向Z

因此,调用__unlink_nodes(A,B)将:

>取A .__ prev _.__ next_(即Z .__ next_),并将其指向B .__ next_(即C)
>取B .__下一个_.__ prev_(即C .__ prev_),并将它指向A .__ prev_(即Z)

这很简单,甚至在使用单个元素范围(这里是这种情况)调用时也可以使用).

然而,现在请注意,如果列表为空,那根本就不行! __list_node_base的默认构造函数是:

  1. __list_node_base()
  2. : __prev_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this))),__next_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this)))
  3. {}

也就是说它是指自己.在这种情况下,__unlink_nodes用& __ end_(两次)调用,不会更改__end _.__ prev _.__ next_ = __end _.__ next_是幂等的(因为__end_.prev是__end_本身).

可能是:

>分析仪考虑到一个空列表的情况(_LIBCPP_ASSERT正在编译出来)
>并得出结论,在这种情况下,__end _.__ next_(由begin()使用)在pop_front()中的deallocate()调用留下悬挂,

或者也许这是指针跳舞的其他东西…希望Clang团队能够补丁.

猜你在找的C&C++相关文章