boostorg / boostorg/circular_buffer

undefined behavior in do_fill_uninitialized_memory()

Aperta
#39 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
C++
Stelle
65
Fork
64
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Inizia in boost/circular_buffer/debug.h, in do_fill_uninitialized_memory(), e rivedi il percorso di costruzione per copia per un circular_buffer vuoto. Riproduci l’esempio con GCC 9.1, -fsanitize=undefined e -DBOOST_CB_ENABLE_DEBUG=1; il lavoro è completato quando il caso di copia vuota non segnala più l’errore di puntatore nullo, mentre i riempimenti con valori diversi da zero continuano a essere verificati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
cpp
Ambito
backend
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.