boostorg / boostorg/program_options
basic_command_line_parser::options() may store address of temporary
- 主要语言
- C++
- 星标
- 136
- 派生
- 117
- PR 合并指标
- 30 天内没有已合并 PR
描述
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
贡献指南
这个仓库没有索引到贡献指南
调研方向
从引用的行开始查看 include/boost/program_options/detail/parsers.hpp,并运行提供的示例,该示例将一个临时对象从 create() 传递给 options()。比较 options() 和 positional() 的复制方案与非 const 引用方案,然后验证所选择的行为不再允许悬空引用,或清楚地记录所需的生命周期。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- cpp
- 领域
- cli
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 48/100