boostorg / boostorg/local_function

an example/test uses a dangling reference

Offen
#8 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
C++
Sterne
11
Forks
28
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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();
}
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Start with libs/local_function/test/return_assign.cpp and run the existing test to compare the temporary and local-variable cases. Trace how the returned boost::function retains the bound reference, then determine the intended safe test behavior; done means the example no longer relies on a dangling reference and the relevant lifetime case is correctly covered.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
cpp
Bereich
testing
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.