From patchwork Tue Sep 4 16:43:01 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 181624 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from chlorine.canonical.com (chlorine.canonical.com [91.189.94.204]) by ozlabs.org (Postfix) with ESMTP id 61A7E2C008A for ; Wed, 5 Sep 2012 02:43:05 +1000 (EST) Received: from localhost ([127.0.0.1] helo=chlorine.canonical.com) by chlorine.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1T8wCJ-0002qC-HU; Tue, 04 Sep 2012 16:41:55 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by chlorine.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1T8wCI-0002q7-3D for fwts-devel@lists.ubuntu.com; Tue, 04 Sep 2012 16:41:54 +0000 Received: from cpc3-craw6-2-0-cust180.croy.cable.virginmedia.com ([77.100.248.181] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1T8wDO-00043V-3b for fwts-devel@lists.ubuntu.com; Tue, 04 Sep 2012 16:43:02 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH] lib: fwts_klog: sanity check json klog data is readable Date: Tue, 4 Sep 2012 17:43:01 +0100 Message-Id: <1346776981-12756-1-git-send-email-colin.king@canonical.com> X-Mailer: git-send-email 1.7.10.4 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.13 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: fwts-devel-bounces@lists.ubuntu.com Errors-To: fwts-devel-bounces@lists.ubuntu.com 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;