From 856b1777b08ee426fe80964c9748e89b827d77e8 Mon Sep 17 00:00:00 2001 From: Brenden Blanco Date: Fri, 5 Feb 2016 14:49:10 -0800 Subject: [PATCH] Fix segfault in ~BPFModule on syntax error ~BPFModule was segfaulting because tables_ was an empty pointer. The pointer is valid only for valid compilations. Add a test as well. Signed-off-by: Brenden Blanco --- src/cc/bpf_module.cc | 8 +++++--- tests/cc/test_clang.py | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/cc/bpf_module.cc b/src/cc/bpf_module.cc index 682890917a8c..7e1a409d6e97 100644 --- a/src/cc/bpf_module.cc +++ b/src/cc/bpf_module.cc @@ -114,9 +114,11 @@ BPFModule::~BPFModule() { engine_.reset(); rw_engine_.reset(); ctx_.reset(); - for (auto table : *tables_) { - if (table.is_shared) - SharedTables::instance()->remove_fd(table.name); + if (tables_) { + for (auto table : *tables_) { + if (table.is_shared) + SharedTables::instance()->remove_fd(table.name); + } } } diff --git a/tests/cc/test_clang.py b/tests/cc/test_clang.py index 5811c2274943..87f2bd4e9154 100755 --- a/tests/cc/test_clang.py +++ b/tests/cc/test_clang.py @@ -299,5 +299,9 @@ def test_exported_maps(self): b1 = BPF(text="""BPF_TABLE_PUBLIC("hash", int, int, table1, 10);""") b2 = BPF(text="""BPF_TABLE("extern", int, int, table1, 10);""") + def test_syntax_error(self): + with self.assertRaises(Exception): + b = BPF(text="""int failure(void *ctx) { if (); return 0; }""") + if __name__ == "__main__": main()