diff-match-patch-cpp-stl icon indicating copy to clipboard operation
diff-match-patch-cpp-stl copied to clipboard

Incorrect value of char_t in diff_linesToCharsMunge() when hash value is larger than max value of char_t

Open hermannnav opened this issue 7 months ago • 0 comments

when inserting a line with hash > 255. Then the conversion to char_t will have overflow issue since (char_t)256 == (char_t)0.

  static string_t diff_linesToCharsMunge(const string_t &text, std::map<LinePtr, size_t> &lineHash) {
    string_t chars;
    // Walk the text, pulling out a substring for each line.
    // text.split('\n') would would temporarily double our memory footprint.
    // Modifying text would create many large strings to garbage collect.
    typename string_t::size_type lineLen;
    for (typename string_t::const_pointer lineStart = text.c_str(), textEnd = lineStart + text.size(); lineStart < textEnd; lineStart += lineLen + 1) {
      lineLen = next_token(text, traits::from_wchar(L'\n'), lineStart);
      if (lineStart + lineLen == textEnd) --lineLen;
      chars += (char_t)(*lineHash.insert(std::make_pair(LinePtr(lineStart, lineLen + 1), lineHash.size() + 1)).first).second; <=== The issue
    }
    return chars;
  }

hermannnav avatar Jul 19 '24 03:07 hermannnav