boostorg / boostorg/circular_buffer
undefined behavior in do_fill_uninitialized_memory()
- Lenguaje dominante
- C++
- Estrellas
- 65
- Forks
- 64
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
Using gcc 9.1 with -fsanitize=undefined and -DBOOST_CB_ENABLE_DEBUG=1, the following code results in a ubsan error: "boost/circular_buffer/debug.hpp:37:16: runtime error: null pointer passed as argument 1, which is declared to never be null".
```
using Q = boost::circular_buffer;
Q q;
const Q q2(q);
```
The ubsan error is a result of the following code in circular_buffer/debug.h:
```
template
inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) BOOST_NOEXCEPT {
std::memset(static_cast(data), UNINITIALIZED, size_in_bytes);
}
```
During copy construction, the function gets called with data == nullptr and size_in_bytes == 0. I believe that passing a null pointer to memset is technically undefined behavior even if the size is 0.
Changing the above function as follows avoids the ubsan error:
```
template
inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) BOOST_NOEXCEPT {
if (size_in_bytes != 0u) {
std::memset(static_cast(data), UNINITIALIZED, size_in_bytes);
}
}
```
I had originally written it to check for data != nullptr, but since this is debug code I thought it seemed desirable to know if the function is ever called with data == null and size_in_bytes != 0. In any case, either way will prevent the ubsan error.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Línea de trabajo
Comienza en boost/circular_buffer/debug.h, en do_fill_uninitialized_memory(), y revisa la ruta de construcción por copia para un circular_buffer vacío. Reproduce el ejemplo con GCC 9.1, -fsanitize=undefined y -DBOOST_CB_ENABLE_DEBUG=1; el trabajo estará terminado cuando el caso de copia vacía ya no informe del error de puntero nulo, mientras que los llenados con valores distintos de cero sigan comprobándose.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- cpp
- Área
- backend
- Tipo de issue
- Error
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 52/100