From patchwork Tue Sep 4 16:43:01 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: lib: fwts_klog: sanity check json klog data is readable X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 181624 Message-Id: <1346776981-12756-1-git-send-email-colin.king@canonical.com> To: fwts-devel@lists.ubuntu.com Date: Tue, 4 Sep 2012 17:43:01 +0100 From: Colin King List-Id: Firmware Test Suite Development From: Colin Ian King 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 Acked-by: Keng-Yu Lin Acked-by: Alex Hung --- 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 #include #include +#include +#include +#include #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;