Skip to content

Commit

Permalink
Fix path separator portability; quick changes to prevent fuzzer failures
Browse files Browse the repository at this point in the history
  • Loading branch information
fhackett committed Jul 19, 2024
1 parent 040f780 commit 73a3012
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
5 changes: 4 additions & 1 deletion parsers/yaml/event_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,10 @@ namespace trieste
auto current = node->at(i)->location().view();
auto next = node->at(i + 1)->location().view();
os << escape_chars(current, escape);
if (!std::isspace(current.front()) && !std::isspace(next.front()))
// an empty string view does not start with a space
if (
(current.empty() || !std::isspace(current.front())) &&
(next.empty() || !std::isspace(next.front())))
{
os << " ";
}
Expand Down
2 changes: 1 addition & 1 deletion parsers/yaml/to_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ namespace
[](Match& _) {
Location loc = _(Key)->location();
auto view = loc.view();
if (view.front() == '"' && view.back() == '"')
if (!view.empty() && view.front() == '"' && view.back() == '"')
{
loc.pos += 1;
loc.len -= 2;
Expand Down
16 changes: 13 additions & 3 deletions samples/infix/infix_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ using namespace std::string_view_literals;
int main(int argc, char** argv)
{
CLI::App app;
app.require_subcommand(1); // not giving a subcommand is an error

std::filesystem::path test_dir;
std::optional<std::filesystem::path> debug_path;
// dir mode, scan a directory and check all examples
Expand Down Expand Up @@ -273,6 +275,14 @@ int main(int argc, char** argv)
.debug_enabled(bool(debug_path))
.debug_path(debug_path ? *debug_path / "read" : "");

// Tricky point: std::filesystem::path uses platform-specific
// separators, even in this context where we only care about
// Trieste's synthetic in-memory output. So, we need to build our
// paths relative to "." using operator/ specifically. If we just
// write "./calculate_output", this code will fail on Windows
// because the key we're looking up becomes ".\calculate_output"
// instead. For convenience, we use this synth_root as a base.
std::filesystem::path synth_root = ".";
trieste::ProcessResult result;
std::string actual_str;
if (selected_proc == "parse_only")
Expand All @@ -295,7 +305,7 @@ int main(int argc, char** argv)

if (result.ok)
{
actual_str = dest->files().at("./calculate_output");
actual_str = dest->file(synth_root / "calculate_output");
}
}
else if (selected_proc == "infix")
Expand All @@ -305,7 +315,7 @@ int main(int argc, char** argv)

if (result.ok)
{
actual_str = dest->files().at("./infix");
actual_str = dest->file(synth_root / "infix");
}
}
else if (selected_proc == "postfix")
Expand All @@ -315,7 +325,7 @@ int main(int argc, char** argv)

if (result.ok)
{
actual_str = dest->files().at("./postfix");
actual_str = dest->file(synth_root / "postfix");
}
}
else
Expand Down

0 comments on commit 73a3012

Please sign in to comment.