boostorg / boostorg/local_function

an example/test uses a dangling reference

オープン
#8 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
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

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。