Skip to content

Commit

Permalink
gndtruth_extractor: Fix segfault on error opening file
Browse files Browse the repository at this point in the history
  • Loading branch information
cassava committed Apr 20, 2023
1 parent 5ba236e commit 0821655
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 4 deletions.
13 changes: 9 additions & 4 deletions plugins/gndtruth_extractor/src/gndtruth_extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class GndTruthMsgPackSerializer
class GndTruthSerializer {
public:
virtual ~GndTruthSerializer() = 0;
virtual void open_file(const std::string& filename) = 0;
virtual bool open_file(const std::string& filename) = 0;
virtual void serialize(const Sync& sync, const GndTruth& gt) = 0;
virtual void close_file() = 0;

Expand All @@ -103,10 +103,12 @@ class GndTruthSerializerImpl
GndTruthSerializerImpl(Logger logger) : base1(logger), GndTruthSerializer() {}
virtual ~GndTruthSerializerImpl() override;
using base1::open_file;
virtual void open_file(const std::string& filename) override {

[[nodiscard]]
virtual bool open_file(const std::string& filename) override {
std::string default_name = this->outputstream_.make_default_filename(
this->serializer_.make_default_filename(default_filename));
base1::open_file(filename, default_name);
return base1::open_file(filename, default_name);
}
virtual void serialize(const Sync& sync, const GndTruth& gt) override {
base1::serialize(sync, gt);
Expand Down Expand Up @@ -219,7 +221,10 @@ class GndTruthExtractor : public Controller {
private:
void open_file() {
if (serializer_) {
serializer_->open_file(config_.output_file);
bool ok = serializer_->open_file(config_.output_file);
if (!ok) {
throw cloe::ModelAbort("cannot open file: {}", config_.output_file);
}
}
}
void close_file() {
Expand Down
32 changes: 32 additions & 0 deletions plugins/gndtruth_extractor/tests/conanfile_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pathlib import Path
from conan import ConanFile


class CloeTest(ConanFile):
python_requires = "cloe-launch-profile/[~=0.19.0]@cloe/develop"
python_requires_extend = "cloe-launch-profile.Base"

default_options = {
"cloe-engine:server": False,
}

@property
def cloe_launch_env(self):
return {
"CLOE_LOG_LEVEL": "debug",
"CLOE_STRICT_MODE": "1",
"CLOE_WRITE_OUTPUT": "0",
"CLOE_ROOT": Path(self.recipe_folder) / "../../..",
}

def set_version(self):
self.version = self.project_version("../../../VERSION")

def requirements(self):
self.requires(f"cloe-engine/{self.version}@cloe/develop")
self.requires(f"cloe-plugin-basic/{self.version}@cloe/develop")
self.requires(f"cloe-plugin-minimator/{self.version}@cloe/develop")
self.requires(f"cloe-plugin-gndtruth-extractor/{self.version}@cloe/develop")

if self.options["cloe-engine"].server:
self.requires("boost/[<1.70]", override=True)
28 changes: 28 additions & 0 deletions plugins/gndtruth_extractor/tests/config_minimator_smoketest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"version": "4",
"include": [
"${CLOE_ROOT}/tests/controller_basic.json"
],
"simulators": [
{
"binding": "minimator"
}
],
"vehicles": [
{
"name": "default",
"from": {
"simulator": "minimator",
"index": 0
}
}
],
"server": {
"listen": false,
"listen_port": 23456
},
"triggers": [
{"event": "start", "action": "realtime_factor=-1"},
{"event": "time=60", "action": "succeed"}
]
}
13 changes: 13 additions & 0 deletions plugins/gndtruth_extractor/tests/test_gndtruth.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bats

cd "${BATS_TEST_DIRNAME}"
export CLOE_ROOT="${BATS_TEST_DIRNAME}/../../.."
load "${CLOE_ROOT}/tests/setup_bats.bash"
load "${CLOE_ROOT}/tests/setup_testname.bash"

@test "$(testname 'Expect run failure' 'test_gndtruth_invalid_file.json' '29e8ff3b-86f3-4320-ba69-05600df1f836')" {
cloe-engine check test_gndtruth_invalid_file.json
run cloe-engine run test_gndtruth_invalid_file.json
assert_exit_failure $status
test $status -eq $CLOE_EXIT_ABORTED
}
15 changes: 15 additions & 0 deletions plugins/gndtruth_extractor/tests/test_gndtruth_invalid_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "4",
"include": [
"config_minimator_smoketest.json"
],
"controllers": [
{
"binding": "gndtruth_extractor",
"vehicle": "default",
"args": {
"output_file": "/tmp/nonexistant/file/that/will/not/be/created.json.gz"
}
}
]
}

0 comments on commit 0821655

Please sign in to comment.