Skip to content

Commit

Permalink
Replace json-c library with minimal replacement
Browse files Browse the repository at this point in the history
FWTS uses a subset of json-c functionality, so replace it with
our own implementation that supports this subset. This also
removes the dependency of two flavours of json libraries that
fwts has been using and also allows us to perform deeper memory
allocation and leaking tracking with static analysis tools.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
Acked-by: Alex Hung <alex.hung@canonical.com>
Acked-by: Ivan Hu <ivan.hu@canonical.com>
  • Loading branch information
Colin Ian King authored and Alex Hung committed Aug 24, 2020
1 parent b4f1e48 commit 1a663dd
Show file tree
Hide file tree
Showing 7 changed files with 971 additions and 21 deletions.
3 changes: 0 additions & 3 deletions configure.ac
Expand Up @@ -9,8 +9,6 @@
AC_PROG_LIBTOOL
AC_C_INLINE
AM_PROG_CC_C_O
AC_SEARCH_LIBS([json_object_from_file], [json json-c], [], [ AC_MSG_ERROR([no available json library]) ])
AC_SEARCH_LIBS([json_object_object_get_ex], [json json-c], [ AC_DEFINE([JSON_HAS_GET_EX], [1], [Define if we have json_object_object_get_ex])], [])
AC_CHECK_FUNCS([localtime_r])
AC_CHECK_FUNCS([dup2])
AC_CHECK_FUNCS([getcwd])
Expand Down Expand Up @@ -61,7 +59,6 @@
AC_CHECK_HEADERS([time.h])
AC_CHECK_HEADERS([sys/ioctl.h])
AC_CHECK_HEADERS([sys/time.h])
AC_CHECK_HEADERS([json/json.h])
AC_CHECK_HEADERS([glib.h])
AC_CHECK_HEADERS([gio/gio.h])
AC_CHECK_HEADERS([asm/opal-prd.h])
Expand Down
2 changes: 2 additions & 0 deletions src/lib/include/fwts.h
Expand Up @@ -153,6 +153,8 @@

#define FWTS_JSON_DATA_PATH DATAROOTDIR "/fwts"

#include <inttypes.h>

#include "fwts_version.h"
#include "fwts_backtrace.h"
#include "fwts_types.h"
Expand Down
61 changes: 46 additions & 15 deletions src/lib/include/fwts_json.h
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2020 Canonical
* Copyright (C) 2010-2020 Canonical
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -16,26 +16,15 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#ifndef __FWTS_JSON_H__
#define __FWTS_JSON_H__

#include <json.h>

#define __FWTS_JSON_ERR_PTR__ ((json_object*) -1)
/*
* Older versions of json-c may return an error in an
* object as a ((json_object*)-1), where as newer
* versions return NULL, so check for these. Sigh.
* Minimal subset of json for fwts
*/
#define FWTS_JSON_ERROR(ptr) \
( (ptr == NULL) || ((const json_object *)ptr == __FWTS_JSON_ERR_PTR__) )

/*
* json-c 0.13.99 does not define TRUE/FALSE anymore
* the json-c maintainers replaced them with pure 1/0
* https://github.com/json-c/json-c/commit/0992aac61f8b
*/
#define FWTS_JSON_ERROR(ptr) (!ptr)

#ifndef FALSE
#define FALSE 0
#endif
Expand All @@ -44,4 +33,46 @@
#define TRUE 1
#endif

/*
* json types supported
*/
typedef enum {
type_null,
type_int,
type_string,
type_object,
type_array,
} json_type;

/*
* json object information
*/
typedef struct json_object {
char *key; /* Null if undefined */
int length; /* Length of a collection of objects */
json_type type; /* Object type */
union {
void *ptr; /* string or object array pointer */
int intval; /* integer value */
} u;
} json_object;

/*
* minimal json c library functions as required by fwts
*/
json_object *json_object_from_file(const char *filename);
json_object *json_object_object_get(json_object *obj, const char *key);
int json_object_array_length(json_object *obj);
json_object *json_object_array_get_idx(json_object *obj, int index);
const char *json_object_get_string(json_object *obj);
json_object *json_object_new_int(int);
void json_object_object_add(json_object *obj, const char *key, json_object *value);

json_object *json_object_new_object(void);
json_object *json_object_new_array(void);
char *json_object_to_json_string(json_object *obj);
void json_object_put(json_object *obj);
json_object *json_object_new_string(const char *str);
int json_object_array_add(json_object *obj, json_object *item);

#endif
1 change: 1 addition & 0 deletions src/lib/src/Makefile.am
Expand Up @@ -82,6 +82,7 @@ libfwts_la_SOURCES = \
fwts_interactive.c \
fwts_ioport.c \
fwts_ipmi.c \
fwts_json.c \
fwts_keymap.c \
fwts_klog.c \
fwts_olog.c \
Expand Down

0 comments on commit 1a663dd

Please sign in to comment.