boostorg / boostorg/local_function
an example/test uses a dangling reference
- Lenguaje dominante
- C++
- Estrellas
- 11
- Forks
- 28
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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();
}
```
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Línea de trabajo
Comienza con libs/local_function/test/return_assign.cpp y ejecuta el test existente para comparar los casos de un objeto temporal y de una variable local. Rastrea cómo la boost::function devuelta conserva la referencia vinculada y, a continuación, determina cuál debe ser el comportamiento seguro del test; el trabajo estará terminado cuando el ejemplo ya no dependa de una dangling reference y el caso de Lifetime relevante esté cubierto correctamente.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- cpp
- Área
- testing
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 35/100