Skip to content

Commit

Permalink
Fix bad string trimming, and safer trimming of temporary strings
Browse files Browse the repository at this point in the history
  • Loading branch information
vector-of-bool committed Nov 9, 2019
1 parent 0e71d14 commit 404738a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
21 changes: 11 additions & 10 deletions src/dds/deps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ dependency dependency::parse_depends_string(std::string_view str) {
++str_iter;
}

auto name = trim(std::string_view(str_begin, str_iter - str_begin));
auto version_str = trim(std::string_view(str_iter, str_end - str_iter));
auto name = trim_view(std::string_view(str_begin, str_iter - str_begin));
auto version_str = trim_view(std::string_view(str_iter, str_end - str_iter));

semver::version version;
try {
Expand All @@ -54,24 +54,25 @@ auto tie_sdist(const sdist& sd) {
return std::tuple(sd.manifest.name, sd.manifest.version.to_string());
}

auto sdist_compare = [](const sdist& lhs, const sdist& rhs) {
return tie_sdist(lhs) < tie_sdist(rhs);
};
auto sdist_compare
= [](const sdist& lhs, const sdist& rhs) { return tie_sdist(lhs) < tie_sdist(rhs); };

void detail::sort_sdists(std::vector<sdist>& sd) { std::sort(sd.begin(), sd.end(), sdist_compare); }

namespace {

const sdist* get_sdist(const std::vector<sdist>& sorted_sds, std::string_view name, std::string_view version) {
auto found = std::partition_point(sorted_sds.begin(), sorted_sds.end(), [&](const auto& candidate) {
return tie_sdist(candidate) < std::tie(name, version);
});
const sdist*
get_sdist(const std::vector<sdist>& sorted_sds, std::string_view name, std::string_view version) {
auto found
= std::partition_point(sorted_sds.begin(), sorted_sds.end(), [&](const auto& candidate) {
return tie_sdist(candidate) < std::tie(name, version);
});
if (found->manifest.name == name && found->manifest.version.to_string() == version) {
return &*found;
}
return nullptr;
}
}
} // namespace

void detail::do_find_deps(const std::vector<sdist>& sdists,
const dependency& dep,
Expand Down
9 changes: 4 additions & 5 deletions src/dds/util/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ inline namespace string_utils {

inline std::string_view sview(std::string_view::const_iterator beg,
std::string_view::const_iterator end) {
if (beg == end) {
return "";
}
return std::string_view(&*beg, static_cast<std::size_t>(std::distance(beg, end)));
}

inline std::string_view trim(std::string_view s) {
inline std::string_view trim_view(std::string_view s) {
auto iter = s.begin();
auto end = s.end();
while (iter != end && std::isspace(*iter)) {
Expand All @@ -29,10 +32,6 @@ inline std::string_view trim(std::string_view s) {
return sview(iter, new_end);
}

inline std::string_view trim(const char* str) { return trim(std::string_view(str)); }

inline std::string trim(std::string&& s) { return std::string(trim(s)); }

inline bool ends_with(std::string_view s, std::string_view key) {
auto found = s.rfind(key);
return found != s.npos && found == s.size() - key.size();
Expand Down
6 changes: 3 additions & 3 deletions src/dds/util/string.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ void test_ends_with() {
}

void test_trim() {
CHECK(trim("foo") == "foo");
CHECK(trim("foo ") == "foo");
CHECK(trim(" ").size() == 0);
CHECK(trim_view("foo") == "foo");
CHECK(trim_view("foo ") == "foo");
CHECK(trim_view(" ").size() == 0);
}

void test_contains() {
Expand Down
4 changes: 3 additions & 1 deletion src/libman/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ lm::index index::from_file(path_ref fpath) {

for (const auto& pkg_line : package_lines) {
auto items = dds::split(pkg_line, ";");
std::transform(items.begin(), items.end(), items.begin(), [](auto s) { return trim(s); });
std::transform(items.begin(), items.end(), items.begin(), [](auto s) {
return std::string(trim_view(s));
});
if (items.size() != 2) {
throw std::runtime_error(
fmt::format("Invalid 'Package' field in index file ({}): 'Package: {}'",
Expand Down
6 changes: 3 additions & 3 deletions src/libman/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using namespace lm;
namespace {

void parse_line(std::vector<pair>& pairs, const std::string_view whole_line) {
const auto line = trim(whole_line);
const auto line = trim_view(whole_line);
if (line.empty() || line[0] == '#') {
return;
}
Expand Down Expand Up @@ -46,8 +46,8 @@ void parse_line(std::vector<pair>& pairs, const std::string_view whole_line) {
// `iter` now points to the space between the key and value
auto key = sview(begin, iter - 1); // -1 to trim the colon in the key
auto value = sview(iter, end);
key = trim(key);
value = trim(value);
key = trim_view(key);
value = trim_view(value);
pairs.emplace_back(key, value);
}

Expand Down

0 comments on commit 404738a

Please sign in to comment.