From bb880a2826f57c1b91fc9f689d8cebcad86a60a4 Mon Sep 17 00:00:00 2001
From: Nick Kralevich <nnk@google.com>
Date: Wed, 26 Sep 2018 14:28:44 -0700
Subject: [PATCH] gracefully handle hashtab_search failures

Otherwise we crash on a null pointer exception.

Test: Reference a non-existent type in test code and make sure
    the test doesn't crash.

Change-Id: I5839d2740d51c0df7a8909a3f8430850ae1b2675
---
 tests/sepol_wrap.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tests/sepol_wrap.cpp b/tests/sepol_wrap.cpp
index 41f13bf7f..15f3e7079 100644
--- a/tests/sepol_wrap.cpp
+++ b/tests/sepol_wrap.cpp
@@ -116,11 +116,16 @@ void *init_type_iter(void *policydbp, const char *type, bool is_attr)
     } else {
         out->alltypes = TYPE_ITER_LOOKUP;
         out->d = static_cast<type_datum *>(hashtab_search(db->p_types.table, type));
+        if (out->d == nullptr) {
+            std::cerr << "\"" << type << "\" does not exist" << std::endl;
+            free(out);
+            return nullptr;
+        }
         if (is_attr && out->d->flavor != TYPE_ATTRIB) {
             std::cerr << "\"" << type << "\" MUST be an attribute in the policy" << std::endl;
             free(out);
             return nullptr;
-        } else if (!is_attr && out->d->flavor !=TYPE_TYPE) {
+        } else if (!is_attr && out->d->flavor != TYPE_TYPE) {
             std::cerr << "\"" << type << "\" MUST be a type in the policy" << std::endl;
             free(out);
             return nullptr;
-- 
GitLab