Skip to content

use of string_view in as<T> method #1187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

phlptp
Copy link
Collaborator

@phlptp phlptp commented Jul 26, 2025

Address Issue #881, allowing use of string_view in the as method on options.

@phlptp phlptp requested a review from henryiii July 29, 2025 12:16
@@ -319,7 +319,7 @@ class Option : public OptionBase<Option> {
/// complete Results of parsing
results_t results_{};
/// results after reduction
results_t proc_results_{};
mutable results_t proc_results_{};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why mutable? Generally that means this is thread safe, while results_t won't necessarily be thread safe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key issue is the results and related as<T> method. These methods are const as normally they should not result in changes to the data structure. This converts the results to whatever is requested. But in cases where the results are requested as a string_view and there was no data given in the parsing, and there was a default value given. This can lead to undefined behavior in the way we were doing it, as the string_view could end up pointing to a temporary. Some compilers handled this gracefully others didn't. The simplest lowest impact solution to this was to make proc_results which is where processed results are stored in the mutable object so it can be modified in a const method in those few select cases and prevent undefined behavior. The other options were to just ban string_view but would not solve the underlying problem, just the most common exemplar of it. Make some more complicated storage buffers, or force processing for all options to pre fill the results structures. My assessment was that this was the least intrusive and simplest option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants