summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/utils/checkutils.h24
-rw-r--r--src/utils/checkutils_unittest.cc27
2 files changed, 51 insertions, 0 deletions
diff --git a/src/utils/checkutils.h b/src/utils/checkutils.h
index 7b76da8f1..52545e5cb 100644
--- a/src/utils/checkutils.h
+++ b/src/utils/checkutils.h
@@ -73,6 +73,22 @@
return ret; \
}
+#define returnNullptrV(val) \
+ if ((val) == nullptr) \
+ { \
+ reportStack(__FILE__, __LINE__, __func__, \
+ "Detected null value", #val); \
+ return; \
+ }
+
+#define returnNullptr(ret, val) \
+ if ((val) == nullptr) \
+ { \
+ reportStack(__FILE__, __LINE__, __func__, \
+ "Detected null value", #val); \
+ return ret; \
+ }
+
void reportStack(const char *const file,
const unsigned int line,
const char *const func,
@@ -102,5 +118,13 @@ void reportStack(const char *const file,
if (val) \
return ret;
+#define returnNullptrV(val) \
+ if ((val) == nullptr) \
+ return; \
+
+#define returnNullptr(ret, val) \
+ if ((val) == nullptr) \
+ return ret; \
+
#endif // ENABLE_ASSERTS
#endif // UTILS_CHECKUTILS_H
diff --git a/src/utils/checkutils_unittest.cc b/src/utils/checkutils_unittest.cc
index 4cce96725..981e08f72 100644
--- a/src/utils/checkutils_unittest.cc
+++ b/src/utils/checkutils_unittest.cc
@@ -58,6 +58,19 @@ static int testReturnTrue(const bool val)
return 1;
}
+static int testReturnNullptr(void *val)
+{
+ returnNullptr(0, val);
+ return 1;
+}
+
+static void testReturnNullptrV(void *val)
+{
+ flag = false;
+ returnNullptrV(val);
+ flag = true;
+}
+
TEST_CASE("CheckUtils")
{
logger = new Logger;
@@ -114,5 +127,19 @@ TEST_CASE("CheckUtils")
REQUIRE(testReturnTrue(true) == 0);
}
+ SECTION("returnNullptr")
+ {
+ REQUIRE(testReturnNullptr(nullptr) == 0);
+ REQUIRE(testReturnNullptr((void*)1) == 1);
+ }
+
+ SECTION("returnNullptrV")
+ {
+ testReturnNullptrV(nullptr);
+ REQUIRE(flag == false);
+ testReturnNullptrV((void*)1);
+ REQUIRE(flag == true);
+ }
+
delete2(logger);
}