boostorg / boostorg/local_function
an example/test uses a dangling reference
- 主要语言
- C++
- 星标
- 11
- 派生
- 28
- PR 合并指标
- 30 天内没有已合并 PR
描述
libs/local_function/test/return_assign.cpp uses a dangling reference:
```c++
#include
#include
#include
#include
//[return_assign
void call1(boost::function f) { BOOST_TEST(f(1) == 5); }
void call0(boost::function f) { BOOST_TEST(f() == 5); }
boost::function linear(const int& slope) {
int BOOST_LOCAL_FUNCTION(const bind& slope,
int x, default 1, int y, default 2) {
return x + slope * y;
} BOOST_LOCAL_FUNCTION_NAME(lin)
boost::function f = lin; // Assign to local variable.
BOOST_TEST(f(1, 2) == 5);
call1(lin); // Pass to other functions.
call0(lin);
return lin; // Return.
}
void call(void) {
boost::function f = linear(2); // create temporary, bind reference to temporary
BOOST_TEST(f(1, 2) == 5); // !!!!!!!!!!!!!!!!!!!!!!!! use the reference
}
//]
int main(void) {
call();
return boost::report_errors();
}
```
changing the target from temporary to local variable can let the test fail:
```c++
#include
#include
#include
#include
//[return_assign
void call1(boost::function f) { BOOST_TEST(f(1) == 5); }
void call0(boost::function f) { BOOST_TEST(f() == 5); }
boost::function linear(const int& slope) {
int BOOST_LOCAL_FUNCTION(const bind & slope,
int x, default 1, int y, default 2) {
return x + slope * y;
} BOOST_LOCAL_FUNCTION_NAME(lin)
boost::function f = lin; // Assign to local variable.
BOOST_TEST(f(1, 2) == 5);
call1(lin); // Pass to other functions.
call0(lin);
return lin; // Return.
}
void call1(void) {
boost::function f = linear(2);
BOOST_TEST(f(1, 2) == 5); // !!!!!!!!!!!!!!!!!!!!!!!! test passed, although dangling reference
}
void call2(void) {
int a = 2;
boost::function f = linear(a);
a = 3;
BOOST_TEST(f(1, 2) == 5); // !!!!!!!!!!!!!!!!!!!!!!!! test failed
}
//]
int main(void) {
call1();
call2();
return boost::report_errors();
}
```
贡献指南
这个仓库没有索引到贡献指南
调研方向
从 libs/local_function/test/return_assign.cpp 开始,运行现有测试,以比较临时对象和局部变量这两种情况。跟踪返回的 boost::function 如何保留绑定的引用,然后确定预期的安全测试行为;完成的标准是该示例不再依赖 dangling reference,并且相关的 Lifetime 情况得到了正确覆盖。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- cpp
- 领域
- testing
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 35/100