basic_command_line_parser::options() may store address of temporary
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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 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:
#include <boost/program_options.hpp>
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
- Dominant language
- C++
- Stars
- 136
- Forks
- 117
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/program_options
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
boostorg/program_options#147 ·
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
boostorg/program_options#146 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
boostorg/program_options#144 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
boostorg/program_options#142 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 30/100
boostorg/program_options#139 ·
All issues in boostorg/program_options
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
games-on-whales/wolf#509 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
bug-unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
NVIDIA/cuda-samples#453 ·