Skip to content

Commit d8415af

Browse files
committed
Ensure PATH components are handled consistently
1 parent 0b92736 commit d8415af

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

src/filter.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,8 @@ Exact::Exact(std::string match, bool case_sensitive) {
8181
};
8282
} else {
8383
predicate_ = [m = std::move(match)](std::string_view line) {
84-
if (line.size() != m.size()) {
85-
return false;
86-
}
87-
88-
return strncasecmp(line.data(), m.data(), m.size()) == 0;
84+
return line.size() == m.size() &&
85+
strncasecmp(line.data(), m.data(), m.size()) == 0;
8986
};
9087
}
9188
}

src/filter.hh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <functional>
66
#include <memory>
7+
#include <span>
78
#include <string>
89
#include <string_view>
910

@@ -52,15 +53,15 @@ class Directory : public Filter {
5253

5354
class Bin : public Filter {
5455
public:
55-
explicit Bin(const std::vector<std::string>& bins) : bins_(bins) {}
56+
explicit Bin(std::span<const std::string> bins) : bins_(bins) {}
5657

5758
bool Matches(std::string_view line) const override;
5859

5960
private:
6061
// We use this as an optimization for throwing out things early, i.e.
6162
// directories can't be binaries.
6263
Directory directory_filter_;
63-
const std::vector<std::string>& bins_;
64+
std::span<const std::string> bins_;
6465
};
6566

6667
class Regex : public Filter {

src/pkgfile.cc

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <format>
1111
#include <iostream>
12+
#include <map>
1213
#include <optional>
1314
#include <set>
1415
#include <vector>
@@ -38,6 +39,44 @@ std::string WeaklyCanonicalizeBin(std::string_view path) {
3839
return fs::weakly_canonical(canonical);
3940
}
4041

42+
bool IsValidBin(std::string_view path) {
43+
if (path.empty() || path.starts_with("/home") || !path.starts_with('/')) {
44+
return false;
45+
}
46+
47+
return true;
48+
}
49+
50+
std::vector<std::string> ParsePathVarToBins(const char* var) {
51+
if (var == nullptr) {
52+
return {};
53+
}
54+
55+
std::set<std::string> bins;
56+
std::string_view psv(var);
57+
58+
while (!psv.empty()) {
59+
auto pos = psv.find(':');
60+
if (pos == psv.npos) {
61+
if (IsValidBin(psv)) {
62+
// then the remainder goes in the vector
63+
bins.emplace(WeaklyCanonicalizeBin(psv));
64+
}
65+
break;
66+
}
67+
68+
std::string_view component(psv.data(), pos);
69+
if (IsValidBin(component)) {
70+
bins.emplace(WeaklyCanonicalizeBin(component));
71+
}
72+
73+
psv.remove_prefix(pos + 1);
74+
}
75+
76+
return {std::make_move_iterator(bins.begin()),
77+
std::make_move_iterator(bins.end())};
78+
}
79+
4180
} // namespace
4281

4382
Pkgfile::Pkgfile(Options options) : options_(options) {
@@ -64,32 +103,7 @@ Pkgfile::Pkgfile(Options options) : options_(options) {
64103
break;
65104
}
66105

67-
if (const char* p = getenv("PATH"); p) {
68-
std::string_view psv(p);
69-
70-
while (!psv.empty()) {
71-
auto pos = psv.find(':');
72-
if (pos == psv.npos) {
73-
// then the remainder goes in the vector
74-
bins_.emplace_back(WeaklyCanonicalizeBin(psv));
75-
break;
76-
}
77-
78-
// Remove any trailing slashes from the PATH component and normalize it.
79-
std::string_view component = {psv.data(), pos};
80-
while (!component.empty() && component.ends_with('/')) {
81-
component.remove_suffix(1);
82-
}
83-
84-
// No relative paths
85-
if (!component.empty() && component.starts_with('/') &&
86-
!component.starts_with("/home")) {
87-
bins_.emplace_back(WeaklyCanonicalizeBin(component));
88-
}
89-
90-
psv.remove_prefix(pos + 1);
91-
}
92-
}
106+
bins_ = ParsePathVarToBins(getenv("PATH"));
93107
}
94108

95109
std::string Pkgfile::FormatSearchResult(const std::string& repo,

src/pkgfile.hh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#pragma once
22

33
#include <filesystem>
4-
#include <map>
5-
#include <optional>
64

75
#include "archive_reader.hh"
86
#include "db.hh"
@@ -63,8 +61,6 @@ class Pkgfile {
6361
std::string_view version;
6462
};
6563

66-
using RepoMap = std::multimap<std::string, std::filesystem::path>;
67-
6864
using ArchiveEntryCallback = std::function<int(
6965
const std::string& repo, const filter::Filter& filter,
7066
const ParsedPkgname& pkg, Result* result, ArchiveReader* reader)>;

0 commit comments

Comments
 (0)