diff mbox

lib: fwts_klog: sanity check json klog data is readable

Message ID 1346776981-12756-1-git-send-email-colin.king@canonical.com
State Accepted
Headers show

Commit Message

Colin Ian King Sept. 4, 2012, 4:43 p.m. UTC
From: Colin Ian King <colin.king@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@canonical.com>
---
 src/lib/src/fwts_klog.c |   14 ++++++++++++++
 1 file changed, 14 insertions(+)

Comments

Keng-Yu Lin Sept. 5, 2012, 3:41 a.m. UTC | #1
On Wed, Sep 5, 2012 at 12:43 AM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@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@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
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Alex Hung Sept. 5, 2012, 6:16 a.m. UTC | #2
On 09/05/2012 12:43 AM, Colin King wrote:
> From: Colin Ian King <colin.king@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@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;
>

Acked-by: Alex Hung <alex.hung@canonical.com>
diff mbox

Patch

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;