Pau*_* II 7 c++ stl language-lawyer
当我编译该程序时:
#include <list>
int main() {
std::list<int> l = {1, 2};
l.remove(l.front());
}
Run Code Online (Sandbox Code Playgroud)
使用ASAN和调试进行clang:
clang++-8 -fno-omit-frame-pointer -g -fsanitize=address -D_GLIBCXX_DEBUG -std=c++11 list-remove.cpp
Run Code Online (Sandbox Code Playgroud)
我得到了heap-use-after-free:
==31868==ERROR: AddressSanitizer: heap-use-after-free on address 0x603000000020 at pc 0x0000004fa1ae bp 0x7fff52cc5630 sp 0x7fff52cc5628
READ of size 4 at 0x603000000020 thread T0
#0 0x4fa1ad in std::__debug::list<int, std::allocator<int> >::remove(int const&) /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/debug/list:649:18
#1 0x4f990f in main /tmp/list-remove.cpp:5:7
#2 0x7ff27d974b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310
#3 0x41b879 in _start (/tmp/list-remove+0x41b879)
Run Code Online (Sandbox Code Playgroud)
似乎当removefinds x与第一个元素匹配时,它将从列表中删除该元素并将其删除。当检查第二个元素时,它将使用x已删除的元素来比较该元素。
根据C ++标准,这是正确的实现吗?最好先将元素移到末尾再删除它们。这样可以避免heap-use-after-free错误,但是也许不需要这样的实现。
从cppreference出发,没有提到value不能是容器中元素的别名。
这是我正在使用的C ++版本:
$ /usr/bin/c++ --version
c++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |