当我编译这个程序时:
#include <list>
int main() {
std::list<int> l = {1, 2};
l.remove(l.front());
}使用clang使用ASAN和debug:
clang++-8 -fno-omit-frame-pointer -g -fsanitize=address -D_GLIBCXX_DEBUG -std=c++11 list-remove.cpp我得到了一个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)当remove找到与第一个元素匹配的x时,它似乎会从列表中删除该元素并删除它。当它检查第二个元素时,它使用已经被删除的x来比较该元素。
按照C++标准,这是一个正确的实现吗?似乎最好先将元素移到末尾,然后删除它们。这将避免heap-use-after-free错误,但可能不需要这样的实现。
在优先选择中,它没有提到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.发布于 2019-09-20 19:38:56
https://stackoverflow.com/questions/58033075
复制相似问题