C++ vector的漏洞可导致double free

在《STL 源码剖析 -- 侯捷》书籍分析的 tass-sgi-stl-2.91.57-source 源码中。

对 vector 的 erase(); 函数有疑问:

  1. iterator erase(iterator position) {
  2. if (position + 1 != end())
  3. {
  4. //把从 position+1 到 finish 之间的元素一个一个复制到从 position 指向
  5. //的空间,这样,就把 position 原来指向的元素个覆盖了
  6. copy(position + 1, finish, position);
  7. }
  8. --finish;
  9. destroy(finish);
  10. return position;
  11. }



  //=====================================================================
  //该函数把 position 指向的对象删除
  //但是,注意(2013-6-24 wkf):
  //自己认为该函数有一个漏洞,就是: 该函数没有真正地销毁 position 指向的对象,
  //没有执行它的析构函数。
  //假设有如下的一个列表,其内存空间表示如下:
  //---------------------------------------------------------
  //| 1 | 2 | 3 | 4 | 5 |  |  |
  //---------------------------------------------------------
  //           |
  //                  finish
  //现在,要删除第二个元素 2 这个对象。
  //所以,执行 copy 之后,把 3, 4, 5 对象往前移动,其内存空间如下:
  //---------------------------------------------------------
  //| 1 | 3 | 4 | 5 | 5 |  |  |
  //---------------------------------------------------------
  //           |
  //                 finish
  //注意,第 5 个元素的内存空间还是原来的对象 5 这个对象。
  //然后,执行 --finish; 操作,移动指针,如下:
  //---------------------------------------------------------
  //| 1 | 3 | 4 | 5 | 5 |  |  |
  //---------------------------------------------------------
  //         |
  //               finish  
  //然后,执行 destroy(finish); 操作,执行的是第 5 个元素的析构函数,而
  //我们删除的第2个对象,并没有执行其析构函数,只是把它在内存空间给覆盖了。
  //如果一个对象中有 动态分配 的内存空间,就不会被释放。
  //因为,我们都习惯于在 构造函数中使用 new 来分配内存,在析构函数中使用 delete 来
  //释放内存。
  //=====================================================================

如下是一个测试的的例子:

  1. class test
  2. {
  3. public:
  4. int i;
  5. public:
  6. test(int a)
  7. {
  8. i = a;
  9. cout << "construct i = " << i << endl;
  10. }
  11. test(const test &a)
  12. {
  13. i = a.i;
  14. cout << "copy construct i = " << i << endl;
  15. }
  16. ~test()
  17. {
  18. cout << "=== destruct i = " << i << endl;
  19. }
  20. };
  21. void show(vector<test>& num)
  22. {
  23. vector<test>::iterator index;
  24. index = num.begin();
  25. while(num.end() != index)
  26. {
  27. cout << (*index).i << " ";
  28. index++;
  29. }
  30. cout << endl;
  31. }
  32. void main()
  33. {
  34. vector<test> num;
  35. for(int i = 0; i < 6; i++)
  36. {
  37. num.push_back(test(i));
  38. }
  39. show(num);
  40. num.erase(num.begin()+1);
  41. show(num);
  42. num.erase(num.begin()+1);
  43. show(num);
  44. num.erase(num.begin()+1);
  45. show(num);
  46. printf("hehe.....\n");
  47. getchar();
  48. return;
  49. }

运行的结果如下:

construct i = 0
copy construct i = 0
=== destruct i = 0
construct i = 1
copy construct i = 0
copy construct i = 1
=== destruct i = 0
=== destruct i = 1
construct i = 2
copy construct i = 0
copy construct i = 1
copy construct i = 2
=== destruct i = 0
=== destruct i = 1
=== destruct i = 2
construct i = 3
copy construct i = 0
copy construct i = 1
copy construct i = 2
copy construct i = 3
=== destruct i = 0
=== destruct i = 1
=== destruct i = 2
=== destruct i = 3
construct i = 4
copy construct i = 0
copy construct i = 1
copy construct i = 2
copy construct i = 3
copy construct i = 4
=== destruct i = 0
=== destruct i = 1
=== destruct i = 2
=== destruct i = 3
=== destruct i = 4
construct i = 5
copy construct i = 5
=== destruct i = 5
0  1  2  3  4  5
=== destruct i = 5
0  2  3  4  5
=== destruct i = 5
0  3  4  5
=== destruct i = 5
0  4  5
hehe.....

重点是后面的输出:

=== destruct i = 5
0  1  2  3  4  5
=== destruct i = 5
0  2  3  4  5
=== destruct i = 5
0  3  4  5
=== destruct i = 5
0  4  5

执行的析构函数并不是需要删除的那个对象。

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章