diff options
| author | David Kalnischkies <david@kalnischkies.de> | 2021-02-03 17:40:05 +0100 |
|---|---|---|
| committer | David Kalnischkies <david@kalnischkies.de> | 2021-02-03 17:43:13 +0100 |
| commit | c4da2ff42da55ffc38c77a9170dc151216d75962 (patch) | |
| tree | e909e1268b8e56e84a5cebbfda81440ad523c1fe /apt-pkg/contrib/string_view.h | |
| parent | e0743a85c5f5f2f83d91c305450e8ba192194cd8 (diff) | |
Avoid overstepping bounds in config file parsing
Our configuration files are not security relevant, but having a parser
which avoids crashing on them even if they are seriously messed up is
not a bad idea anyway. It is also a good opportunity to brush up the
code a bit avoiding a few small string copies with our string_view.
Diffstat (limited to 'apt-pkg/contrib/string_view.h')
| -rw-r--r-- | apt-pkg/contrib/string_view.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/apt-pkg/contrib/string_view.h b/apt-pkg/contrib/string_view.h index 05aad3327..04f6ff115 100644 --- a/apt-pkg/contrib/string_view.h +++ b/apt-pkg/contrib/string_view.h @@ -39,6 +39,10 @@ public: StringView(const char *data) : data_(data), size_(strlen(data)) {} StringView(std::string const & str): data_(str.data()), size_(str.size()) {} + /* Modifiers */ + void remove_prefix(size_t n) { data_ += n; size_ -= n; } + void remove_suffix(size_t n) { size_ -= n; } + void clear() { size_ = 0; } /* Viewers */ constexpr StringView substr(size_t pos, size_t n = npos) const { @@ -76,6 +80,28 @@ public: return found - data_; } + size_t find(APT::StringView const needle) const { + if (needle.empty()) + return npos; + if (needle.length() == 1) + return find(*needle.data()); + size_t found = 0; + while ((found = find(*needle.data(), found)) != npos) { + if (compare(found, needle.length(), needle) == 0) + return found; + ++found; + } + return found; + } + size_t find(APT::StringView const needle, size_t pos) const { + if (pos == 0) + return find(needle); + size_t const found = substr(pos).find(needle); + if (found == npos) + return npos; + return pos + found; + } + /* Conversions */ std::string to_string() const { return std::string(data_, size_); |
