Opened 14 years ago

Closed 12 years ago

#2777 closed Bugs (fixed)

find_format_all calls formatter with invalid match

Reported by: Éric Malenfant <eric.malenfant@…> Owned by: Pavol Droba
Milestone: Boost 1.39.0 Component: string_algo
Version: Boost 1.35.0 Severity: Problem
Keywords: Cc:

Description

find_format_all() calls the formatter with an invalid match when the end of the input is reached.

Example:

// A Boost.StringAlgo Formatter expecting 
// to be called with matches to the regex:
// (\d+\.)(\d+) 
struct NextVersionNumber {
  template<class RegexMatchT>
  std::string operator()(const RegexMatchT& M)const
  {
    // Without this check, we end up with 
    // a bad_lexical_cast exception,
    // because operator() ends up being called twice:
    // First with a good match, and then with an emtpy one
    if (!Match){
      return "";
    }

    using boost::lexical_cast;
    return M.match_results()[1] +
      lexical_cast<std::string>(
        lexical_cast<unsigned int>(M.match_results()[2]) + 1
      );
  }
};

int main()
{
  namespace bsa = boost::algorithm;
  const std::string s("version is 1.0");
    
  // Outputs:
  //   version is: 1.1
  std::cout <<
      bsa::find_format_all_copy(
          s, bsa::regex_finder(boost::regex("(\\d+\\.)(\\d+)")),
          NextVersionNumber()
      );
  return 0;
}

Change History (2)

comment:1 by Pavol Droba, 14 years ago

Status: newassigned

comment:2 by Steven Watanabe, 12 years ago

Resolution: fixed
Status: assignedclosed

(In [62695]) Avoid calling the formatter with an invalid match. Fixes #2777

Note: See TracTickets for help on using tickets.