[PATCH] lib: fwts_klog: sanity check json klog data is readable

Colin King colin.king at canonical.com
Tue Sep 4 16:43:01 UTC 2012


From: Colin Ian King <colin.king at canonical.com>

Unfortunately the json library is quite flakey and sometimes
loading the klog json data from a non-existant file causes the
json library to segfault.  This patch is a workaround that
sanity checks to see if the data file can be opened for reads
before attempting to call json_object_from_file().

Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
 src/lib/src/fwts_klog.c |   14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/lib/src/fwts_klog.c b/src/lib/src/fwts_klog.c
index ea713e8..7787599 100644
--- a/src/lib/src/fwts_klog.c
+++ b/src/lib/src/fwts_klog.c
@@ -24,6 +24,9 @@
 #include <pcre.h>
 #include <json/json.h>
 #include <ctype.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 #include "fwts.h"
 
@@ -301,6 +304,7 @@ static int fwts_klog_check(fwts_framework *fw,
 	int ret = FWTS_ERROR;
 	int n;
 	int i;
+	int fd;
 	json_object *klog_objs;
 	json_object *klog_table;
 	fwts_klog_pattern *patterns;
@@ -308,6 +312,16 @@ static int fwts_klog_check(fwts_framework *fw,
 
 	snprintf(json_data_path, sizeof(json_data_path), "%s/%s", fw->json_data_path, KLOG_DATA_JSON_FILE);
 
+	/*
+	 * json_object_from_file() can fail when files aren't readable
+	 * so check if we can open for read before calling json_object_from_file()
+	 */
+	if ((fd = open(json_data_path, O_RDONLY)) < 0) {
+		fwts_log_error(fw, "Cannot read file %s.", json_data_path);
+		return FWTS_ERROR;
+	}
+	close(fd);
+
 	if ((klog_objs = json_object_from_file(json_data_path)) == JSON_ERROR) {
 		fwts_log_error(fw, "Cannot load klog data from %s.", json_data_path);
 		return FWTS_ERROR;
-- 
1.7.10.4




More information about the fwts-devel mailing list