boostorg / boostorg/program_options

basic_command_line_parser::options() may store address of temporary

Aperta
#73 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
C++
Stelle
136
Fork
117
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Hi,

I've stambled accross a bug in my application about unrecognized options. After several minutes and checks I've realized it comes from Boost Program_Options because the `basic_command_line_parser::options` function store the address of a const-reference passed as argument. And since temporaries can bind to const-reference the library did use a dangling pointer.

It is not mentioned in the [documentation](https://www.boost.org/doc/libs/1_69_0/doc/html/boost/program_options/basic_command_line_parser.html) that user must specify a valid reference.

I propose that options() and positional() make a copy (as it's backward compatible) or to take a non-const reference as it explains correctly the ownership to the caller (but not backward compatible)

Example of code that may reproduce the bug:

```cpp
#include

namespace po = boost::program_options;

po::options_description create()
{
po::options_description desc;

desc.add_options()
("verbose", po::bool_switch());

return desc;
}

int main(int argc, char** argv)
{
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(create()).run(), vm);
po::notify(vm);
}
```

Offending code https://github.com/boostorg/program_options/blob/develop/include/boost/program_options/detail/parsers.hpp#L37

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Start with include/boost/program_options/detail/parsers.hpp at the cited line and run the provided example, which passes a temporary from create() to options(). Compare the proposed copy and non-const-reference approaches for options() and positional(), then verify that the chosen behavior no longer permits a dangling reference or clearly documents the required lifetime.

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

Valutazione

Stack tecnologico
cpp
Ambito
cli
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.