Indexing operator of iterator_range<counting_iterator<...>> produces undefined behavior
还没有人认领这个 Issue。
评估
调研方向
首先编译来自 test_counting_iterator_range_indexing.cpp 的复现程序,并检查 boost/range/iterator_range_core.hpp 中 iterator_range_base::operator[] 附近的代码。确认 counting_iterator 的警告和悬空引用行为,然后添加一个回归测试,展示 range[0] 在不产生未定义行为的情况下得到 0。
由索引模型根据 Issue 内容生成。
描述
Example:
#include <boost/iterator/counting_iterator.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
#include <type_traits>
int main(int, char **)
{
using iterator = boost::counting_iterator<int>;
using iterator_range = boost::iterator_range<iterator>;
iterator first(0), last(3);
iterator_range range(first, last);
std::cout << "iterator::reference is a reference type: " << std::is_reference<iterator::reference>::value << std::endl;
std::cout << "iterator_range::reference is a reference type: " << std::is_reference<iterator_range::reference>::value << std::endl;
std::cout << "first[0] returns a reference type: " << std::is_reference<decltype(first[0])>::value << std::endl;
std::cout << "range[0] returns a reference type: " << std::is_reference<decltype(range[0])>::value << std::endl;
std::cout << "first[0] == " << first[0] << std::endl;
std::cout << "range[0] == " << range[0] << std::endl; // <-- segfault
}
Output:
iterator::reference is a reference type: 1
iterator_range::reference is a reference type: 1
first[0] returns a reference type: 0
range[0] returns a reference type: 1
first[0] == 0
Segmentation fault (core dumped)
Expected behavior: range[0] returns 0.
If Boost headers are copied to ./boost and the program is compiled with g++ -I. option, the compiler produces the following warning:
In file included from ./boost/range/iterator_range.hpp:13,
from test_counting_iterator_range_indexing.cpp:2:
./boost/range/iterator_range_core.hpp: In instantiation of ‘boost::iterator_range_detail::iterator_range_base<IteratorT, boost::iterators::random_access_traversal_tag>::reference boost::iterator_range_detail::iterator_range_base<IteratorT, boost::iterators::random_access_traversal_tag>::operator[](boost::iterator_range_detail::iterator_range_base<IteratorT, boost::iterators::random_access_traversal_tag>::difference_type) const [with IteratorT = boost::iterators::counting_iterator<int>; boost::iterator_range_detail::iterator_range_base<IteratorT, boost::iterators::random_access_traversal_tag>::reference = const int&; boost::iterator_range_detail::iterator_range_base<IteratorT, boost::iterators::random_access_traversal_tag>::difference_type = long int]’:
test_counting_iterator_range_indexing.cpp:23:43: required from here
./boost/range/iterator_range_core.hpp:391:32: warning: returning reference to temporary [-Wreturn-local-addr]
return this->m_Begin[at];
^
The reason for the segmentation fault is that iterator_range::operator[] returns a reference to a stack-allocated object returned by counting_iterator::operator[].
- 主要语言
- C++
- 星标
- 45
- 派生
- 104
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
boostorg/range 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 65/100
-
难度 1/5 1 小时以内 新手友好度 70/100
-
难度 2/5 1-3 小时 新手友好度 35/100
-
难度 3/5 1-2 天 新手友好度 45/100
-
难度 3/5 1-2 天 新手友好度 38/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 86/100
ArthurSonzogni/FTXUI#1363 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
-
难度 1/5 1 小时以内 新手友好度 90/100
ginkgo-project/ginkgo#2108 ·
-
bug build
难度 1/5 1 小时以内 新手友好度 91/100
facebookincubator/velox#19194 ·
-
JIT-compiled number -> Decimal conversion silently overflows instead of raising DECIMAL_OVERFLOW 未关闭fuzz
难度 2/5 1-3 小时 新手友好度 82/100
ClickHouse/ClickHouse#122114 ·