b948b0
From 57295ef0e85640adcc3b85f08b12f09d54aad2d2 Mon Sep 17 00:00:00 2001
b948b0
From: Tomi Salminen <tsalminen@forcepoint.com>
b948b0
Date: Tue, 16 Apr 2019 13:55:39 +0300
b948b0
Subject: [PATCH] Crash on SIGHUP when config file removed.
b948b0
b948b0
Reading config zeroed the returnable configuration only when the
b948b0
config file was opened successfully. If not, the previously read
b948b0
in configuration was returned, which was already freed.
b948b0
b948b0
Moved configuration zeroing to start of readin_config and added
b948b0
unit test to test zero return.
b948b0
---
b948b0
 gram.y      |  2 +-
b948b0
 test/util.c | 15 +++++++++++++++
b948b0
 2 files changed, 16 insertions(+), 1 deletion(-)
b948b0
b948b0
diff --git a/gram.y b/gram.y
b948b0
index 20af2f3..4115390 100644
b948b0
--- a/gram.y
b948b0
+++ b/gram.y
b948b0
@@ -947,10 +947,10 @@ static void cleanup(void)
b948b0
 
b948b0
 struct Interface * readin_config(char const *path)
b948b0
 {
b948b0
+	IfaceList = 0;
b948b0
 	FILE * in = fopen(path, "r");
b948b0
 	if (in) {
b948b0
 		filename = path;
b948b0
-		IfaceList = 0;
b948b0
 		num_lines = 1;
b948b0
 		iface = 0;
b948b0
 
b948b0
diff --git a/test/util.c b/test/util.c
b948b0
index b74b301..7124475 100644
b948b0
--- a/test/util.c
b948b0
+++ b/test/util.c
b948b0
@@ -259,6 +259,20 @@ START_TEST(test_rand_between)
b948b0
 }
b948b0
 END_TEST
b948b0
 
b948b0
+START_TEST(test_cfg_removal_with_sighup)
b948b0
+{
b948b0
+  struct Interface *tmpIface = NULL;
b948b0
+
b948b0
+  tmpIface = readin_config("test/test1.conf");
b948b0
+  ck_assert(tmpIface);
b948b0
+
b948b0
+  free_ifaces(tmpIface);
b948b0
+
b948b0
+  tmpIface = readin_config("test/file_that_should_not_exists.conf");
b948b0
+  ck_assert(!tmpIface);
b948b0
+}
b948b0
+END_TEST
b948b0
+
b948b0
 Suite *util_suite(void)
b948b0
 {
b948b0
 	TCase *tc_safe_buffer = tcase_create("safe_buffer");
b948b0
@@ -288,6 +302,7 @@ Suite *util_suite(void)
b948b0
 
b948b0
 	TCase *tc_misc = tcase_create("misc");
b948b0
 	tcase_add_test(tc_misc, test_rand_between);
b948b0
+	tcase_add_test(tc_misc, test_cfg_removal_with_sighup);
b948b0
 
b948b0
 	Suite *s = suite_create("util");
b948b0
 	suite_add_tcase(s, tc_safe_buffer);