Skip to content

Commit

Permalink
Merge pull request #328 from rouault/make_unsigned-integer-overflow_h…
Browse files Browse the repository at this point in the history
…appy

Make clang -fsanitize=unsigned-integer-overflow happy
  • Loading branch information
p-ranav authored Mar 12, 2024
2 parents 1b3abd9 + 907b942 commit 20f0196
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,9 @@ std::size_t get_levenshtein_distance(const StringType &s1,
}

template <typename ValueType>
std::string_view
get_most_similar_string(const std::map<std::string_view, ValueType> &map,
const std::string_view input) {
std::string_view most_similar{};
std::string get_most_similar_string(const std::map<std::string, ValueType> &map,
const std::string &input) {
std::string most_similar{};
std::size_t min_distance = std::numeric_limits<std::size_t>::max();

for (const auto &entry : map) {
Expand Down Expand Up @@ -1586,11 +1585,12 @@ class ArgumentParser {
if constexpr (std::is_same_v<T, Argument>) {
return (*this)[name];
} else {
auto subparser_it = m_subparser_map.find(name);
std::string str_name(name);
auto subparser_it = m_subparser_map.find(str_name);
if (subparser_it != m_subparser_map.end()) {
return subparser_it->second->get();
}
throw std::logic_error("No such subparser: " + std::string(name));
throw std::logic_error("No such subparser: " + str_name);
}
}

Expand Down Expand Up @@ -1720,7 +1720,7 @@ class ArgumentParser {
/* Getter that returns true if a subcommand is used.
*/
auto is_subcommand_used(std::string_view subcommand_name) const {
return m_subparser_used.at(subcommand_name);
return m_subparser_used.at(std::string(subcommand_name));
}

/* Getter that returns true if a subcommand is used.
Expand All @@ -1734,12 +1734,12 @@ class ArgumentParser {
* @throws std::logic_error in case of an invalid argument name
*/
Argument &operator[](std::string_view arg_name) const {
auto it = m_argument_map.find(arg_name);
std::string name(arg_name);
auto it = m_argument_map.find(name);
if (it != m_argument_map.end()) {
return *(it->second);
}
if (!is_valid_prefix_char(arg_name.front())) {
std::string name(arg_name);
const auto legal_prefix_char = get_any_valid_prefix_char();
const auto prefix = std::string(1, legal_prefix_char);

Expand Down Expand Up @@ -1972,10 +1972,8 @@ class ArgumentParser {
if (Argument::is_positional(current_argument, m_prefix_chars)) {
if (positional_argument_it == std::end(m_positional_arguments)) {

std::string_view maybe_command = current_argument;

// Check sub-parsers
auto subparser_it = m_subparser_map.find(maybe_command);
auto subparser_it = m_subparser_map.find(current_argument);
if (subparser_it != m_subparser_map.end()) {

// build list of remaining args
Expand All @@ -1984,7 +1982,7 @@ class ArgumentParser {

// invoke subparser
m_is_parsed = true;
m_subparser_used[maybe_command] = true;
m_subparser_used[current_argument] = true;
return subparser_it->second->get().parse_args(
unprocessed_arguments);
}
Expand Down Expand Up @@ -2075,10 +2073,8 @@ class ArgumentParser {
if (Argument::is_positional(current_argument, m_prefix_chars)) {
if (positional_argument_it == std::end(m_positional_arguments)) {

std::string_view maybe_command = current_argument;

// Check sub-parsers
auto subparser_it = m_subparser_map.find(maybe_command);
auto subparser_it = m_subparser_map.find(current_argument);
if (subparser_it != m_subparser_map.end()) {

// build list of remaining args
Expand All @@ -2087,7 +2083,7 @@ class ArgumentParser {

// invoke subparser
m_is_parsed = true;
m_subparser_used[maybe_command] = true;
m_subparser_used[current_argument] = true;
return subparser_it->second->get().parse_known_args_internal(
unprocessed_arguments);
}
Expand Down Expand Up @@ -2172,11 +2168,11 @@ class ArgumentParser {
bool m_is_parsed = false;
std::list<Argument> m_positional_arguments;
std::list<Argument> m_optional_arguments;
std::map<std::string_view, argument_it> m_argument_map;
std::map<std::string, argument_it> m_argument_map;
std::string m_parser_path;
std::list<std::reference_wrapper<ArgumentParser>> m_subparsers;
std::map<std::string_view, argument_parser_it> m_subparser_map;
std::map<std::string_view, bool> m_subparser_used;
std::map<std::string, argument_parser_it> m_subparser_map;
std::map<std::string, bool> m_subparser_used;
std::vector<MutuallyExclusiveGroup> m_mutually_exclusive_groups;
bool m_suppress = false;
};
Expand Down

0 comments on commit 20f0196

Please sign in to comment.