# # # patch "key_store.cc" # from [6ea38c00f7da18dd8136f0a4080b2fb7a8cbedba] # to [30d4b56d5274333246ece56ac4f1fd2b2e86c33a] # # patch "packet.cc" # from [36b0f681ca92776c2e60e16529ef7cd7d826a03f] # to [7a0c4e9c49cba8d651e2ba8a69c8eee80769bc47] # ============================================================ --- key_store.cc 6ea38c00f7da18dd8136f0a4080b2fb7a8cbedba +++ key_store.cc 30d4b56d5274333246ece56ac4f1fd2b2e86c33a @@ -238,7 +238,8 @@ key_store_state::maybe_read_key_dir() data dat; read_data(*i, dat); istringstream is(dat()); - read_packets(is, kr); + if (read_packets(is, kr) == 0) + W(F("ignored invalid key file ('%s') in key store") % (*i) ); } } ============================================================ --- packet.cc 36b0f681ca92776c2e60e16529ef7cd7d826a03f +++ packet.cc 7a0c4e9c49cba8d651e2ba8a69c8eee80769bc47 @@ -369,6 +369,22 @@ extract_packets(string const & s, packet return count; } +// this is same as rfind, but search area is haystack[start:] (from start to end of string) +// haystack is searched, needle is pattern +static size_t +rfind_in_substr(std::string const& haystack, size_t start, std::string const& needle) +{ + I(start <= haystack.size()); + const std::string::const_iterator result = + std::find_end(haystack.begin() + start, haystack.end(), + needle.begin(), needle.end()); + + if (result == haystack.end()) + return std::string::npos; + else + return distance(haystack.begin(), result); +} + size_t read_packets(istream & in, packet_consumer & cons) { @@ -379,10 +395,12 @@ read_packets(istream & in, packet_consum static string const end("[end]"); while(in) { + size_t const next_search_pos = (accum.size() >= end.size()) + ? accum.size() - end.size() : 0; in.read(buf, bufsz); accum.append(buf, in.gcount()); string::size_type endpos = string::npos; - endpos = accum.rfind(end); + endpos = rfind_in_substr(accum, next_search_pos, end); if (endpos != string::npos) { endpos += end.size();