optional<const string &>::value_or returns reference to temp
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- cpp
- Domain
- backend-api-design
Research direction
Start in boost/optional.hpp and reproduce the issue with the supplied program using Boost 1.69 and GCC 8.2. Trace value_or for boost::optional<const std::string&>, then verify that the shown call no longer returns a reference to a temporary or crashes when the optional is populated.
Written by the indexing model from the issue text.
Description
I'm using boost::optional<const string &> and found some pitfalls when using value_or(char *).
Basically since value_or takes the "or" party as a template type the conversion from char * to string happens inside the function call and then a reference to that temporary is returned, leading to UB and in my case a crash.
Furthermore, even if the optional is populated and the alternative isn't use, it seems that some sort of copy of the value is still required because of the use of the ?: and then a similar crash occurs.
I think a reasonable workaround would be to have the value_or for references take the alternative value (r) only as const T & instead of as a type param. This would require the implicit conversion to happen at the call site and the function would not create a temporary.
This doesn't avoid all possible dangling references but it improves things.
Here is a short reproduction. I'm using boost 1.69, gcc 8.2:
#include <iostream>
#include <cstdlib>
#include <boost/optional.hpp>
int main()
{
const std::string input = "a good string";
boost::optional<const std::string &> opt = input;
std::string output = opt.value_or("alternative"); // segfault here
std::cout << output << std::endl;
}
Note I do get a warning about returning a reference to a temporary. But usually we disable warnings within boost because there's so much noise.
- Dominant language
- C++
- Stars
- 60
- Forks
- 75
- 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/optional
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
CMake tests fail Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
All issues in boostorg/optional
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 ·