[systemd-devel] [PATCH 2/5] shared/json: JSON parser + number tokenizer bugfix

Pavel Odvody podvody at redhat.com
Thu May 7 08:47:15 PDT 2015


Signed-off-by: Pavel Odvody <podvody at redhat.com>
---
 src/shared/json.c | 437 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 src/shared/json.h |  36 +++++
 2 files changed, 463 insertions(+), 10 deletions(-)

diff --git a/src/shared/json.c b/src/shared/json.c
index 45c8ece..00d5fce 100644
--- a/src/shared/json.c
+++ b/src/shared/json.c
@@ -21,17 +21,173 @@
 
 #include <sys/types.h>
 #include <math.h>
-
 #include "macro.h"
-#include "util.h"
 #include "utf8.h"
 #include "json.h"
 
-enum {
-        STATE_NULL,
-        STATE_VALUE,
-        STATE_VALUE_POST,
-};
+int json_variant_new(json_variant **ret, int type) {
+        json_variant *v;
+        assert(!*ret);
+        v = new0(json_variant, 1);
+        if (!v)
+                return -ENOMEM;
+        v->type = type;
+        v->size = 0;
+        v->obj  = NULL;
+        *ret = v;
+        return 0;
+}
+
+static int json_variant_deep_copy(json_variant *ret, json_variant *variant) {
+        assert(ret);
+        assert(variant);
+
+        ret->type = variant->type;
+        ret->size = variant->size;
+
+        if (variant->type == JSON_VARIANT_STRING) {
+                ret->string = strndup(variant->string, variant->size);
+                if (!ret->string)
+                        return -ENOMEM;
+        } else if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT) {
+                ret->obj = new0(json_variant, variant->size);
+                if (!ret->obj)
+                        return -ENOMEM;
+
+                for (unsigned i = 0; i < variant->size; ++i) {
+                        int r;
+                        r = json_variant_deep_copy(&ret->obj[i], &variant->obj[i]);
+                        if (r < 0)
+                                return r;
+                }
+        }
+        else
+                ret->value = variant->value;
+
+        return 0;
+}
+
+static json_variant *json_object_unref(json_variant *variant);
+
+static json_variant *json_variant_unref_inner(json_variant *variant) {
+        if (!variant)
+                return NULL;
+
+        if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT)
+                return json_object_unref(variant);
+
+        else if (variant->type == JSON_VARIANT_STRING)
+                free(variant->string);
+
+        return NULL;
+}
+
+static json_variant *json_raw_unref(json_variant *variant, size_t size) {
+        if (!variant)
+                return NULL;
+
+        for (size_t i = 0; i < size; ++i)
+                json_variant_unref_inner(&variant[i]);
+
+        free(variant);
+        return NULL;
+}
+
+static json_variant *json_object_unref(json_variant *variant) {
+        assert(variant);
+        if (!variant->obj)
+                return NULL;
+
+        for (unsigned i = 0; i < variant->size; ++i)
+                json_variant_unref_inner(&variant->obj[i]);
+
+        free(variant->obj);
+        return NULL;
+}
+
+static json_variant **json_variant_array_unref(json_variant **variant) {
+        size_t i = 0;
+        json_variant *p = NULL;
+
+        if (!variant)
+                return NULL;
+
+        while((p = (variant[i++])) != NULL) {
+                if (p->type == JSON_VARIANT_STRING)
+                       free(p->string);
+                free(p);
+        }
+
+        free(variant);
+
+        return NULL;
+}
+DEFINE_TRIVIAL_CLEANUP_FUNC(json_variant **, json_variant_array_unref);
+
+json_variant *json_variant_unref(json_variant *variant) {
+        if (!variant)
+                return NULL;
+
+        if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT)
+                return json_object_unref(variant);
+
+        else if (variant->type == JSON_VARIANT_STRING)
+                free(variant->string);
+
+        free(variant);
+
+        return NULL;
+}
+
+char *json_variant_string(json_variant *variant){
+        assert(variant);
+        assert(variant->type == JSON_VARIANT_STRING);
+
+        return variant->string;
+}
+
+bool json_variant_bool(json_variant *variant) {
+        assert(variant);
+        assert(variant->type == JSON_VARIANT_BOOLEAN);
+
+        return variant->value.boolean;
+}
+
+intmax_t json_variant_integer(json_variant *variant) {
+        assert(variant);
+        assert(variant->type == JSON_VARIANT_INTEGER);
+
+        return variant->value.integer;
+}
+
+double json_variant_real(json_variant *variant) {
+        assert(variant);
+        assert(variant->type == JSON_VARIANT_REAL);
+
+        return variant->value.real;
+}
+
+json_variant *json_variant_element(json_variant *variant, unsigned index) {
+        assert(variant);
+        assert(variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT);
+        assert(index < variant->size);
+        assert(variant->obj);
+
+        return &variant->obj[index];
+}
+
+json_variant *json_variant_value(json_variant *variant, const char *key) {
+        assert(variant);
+        assert(variant->type == JSON_VARIANT_OBJECT);
+
+        for (unsigned i = 0; i < variant->size; i += 2) {
+                json_variant *p = &variant->obj[i];
+                if (p->type == JSON_VARIANT_STRING && streq(key, p->string))
+                        return &variant->obj[i + 1];
+        }
+
+        return NULL;
+}
 
 static void inc_lines(unsigned *line, const char *s, size_t n) {
         const char *p = s;
@@ -285,9 +441,6 @@ static int json_parse_number(const char **p, union json_value *ret) {
                 } while (strchr("0123456789", *c) && *c != 0);
         }
 
-        if (*c != 0)
-                return -EINVAL;
-
         *p = c;
 
         if (is_double) {
@@ -310,6 +463,12 @@ int json_tokenize(
         int t;
         int r;
 
+        enum {
+                STATE_NULL,
+                STATE_VALUE,
+                STATE_VALUE_POST,
+        };
+
         assert(p);
         assert(*p);
         assert(ret_string);
@@ -443,3 +602,261 @@ int json_tokenize(
 
         }
 }
+
+static bool json_is_value(json_variant *var) {
+        assert(var);
+
+        return var->type != JSON_VARIANT_CONTROL;
+}
+
+static int json_scoped_parse(json_variant **tokens, size_t *i, size_t n, json_variant *scope) {
+        void *e = NULL;
+        bool arr = scope->type == JSON_VARIANT_ARRAY;
+        int terminator = arr ? JSON_ARRAY_CLOSE : JSON_OBJECT_CLOSE;
+        size_t allocated = 0, size = 0;
+        json_variant *key = NULL, *value = NULL;
+        json_variant *items = NULL;
+        enum {
+                STATE_KEY,
+                STATE_COLON,
+                STATE_COMMA,
+                STATE_VALUE
+        } state = arr ? STATE_VALUE : STATE_KEY;
+
+        assert(tokens);
+        assert(i);
+        assert(scope);
+
+        while((e = *i < n ? tokens[(*i)++] : NULL) != NULL) {
+                json_variant *var = (json_variant *)e;
+                bool stopper = !json_is_value(var) && var->value.integer == terminator;
+                int r;
+
+                if (stopper) {
+                        if (state != STATE_COMMA && size > 0)
+                                goto error;
+
+                        goto out;
+                }
+
+                if (state == STATE_KEY) {
+                        if (var->type != JSON_VARIANT_STRING)
+                                goto error;
+                        else {
+                                key = var;
+                                state = STATE_COLON;
+                        }
+                }
+                else if (state == STATE_COLON) {
+                        if (key == NULL)
+                                goto error;
+
+                        if (json_is_value(var))
+                                goto error;
+
+                        if (var->value.integer != JSON_COLON)
+                                goto error;
+
+                        state = STATE_VALUE;
+                }
+                else if (state == STATE_VALUE) {
+                        _cleanup_jsonunref_ json_variant* v = NULL;
+                        size_t toadd = arr ? 1 : 2;
+
+                        if (!json_is_value(var)) {
+                                int type = (var->value.integer == JSON_ARRAY_OPEN) ? JSON_VARIANT_ARRAY : JSON_VARIANT_OBJECT;
+
+                                r = json_variant_new(&v, type);
+                                if (r < 0)
+                                        goto error;
+                                r = json_scoped_parse(tokens, i, n, v);
+                                if (r < 0)
+                                        goto error;
+
+                                value = v;
+                        }
+                        else
+                                value = var;
+
+                        if(!GREEDY_REALLOC(items, allocated, size + toadd))
+                                goto error;
+
+                        if (arr) {
+                                r = json_variant_deep_copy(&items[size], value);
+                                if (r < 0)
+                                        goto error;
+                        } else {
+                                r = json_variant_deep_copy(&items[size], key);
+                                if (r < 0)
+                                        goto error;
+                                r = json_variant_deep_copy(&items[size+1], value);
+                                if (r < 0)
+                                        goto error;
+                        }
+
+                        size += toadd;
+                        state = STATE_COMMA;
+                }
+                else if (state == STATE_COMMA) {
+                        if (json_is_value(var))
+                                goto error;
+
+                        if (var->value.integer != JSON_COMMA)
+                                goto error;
+
+                        key = NULL;
+                        value = NULL;
+
+                        state = arr ? STATE_VALUE : STATE_KEY;
+                }
+        }
+
+error:
+        json_raw_unref(items, size);
+        return -EBADMSG;
+
+out:
+        scope->size = size;
+        scope->obj = items;
+
+        return scope->type;
+}
+
+static int json_parse_tokens(json_variant **tokens, size_t ntokens, json_variant **rv) {
+        size_t it = 0;
+        int r;
+        json_variant *e;
+        _cleanup_jsonunref_ json_variant *p;
+
+        assert(tokens);
+        assert(*rv == NULL);
+        assert(ntokens);
+
+        e = tokens[it++];
+        r = json_variant_new(&p, JSON_VARIANT_OBJECT);
+        if (r < 0)
+                return r;
+
+        if (e->type != JSON_VARIANT_CONTROL && e->value.integer != JSON_OBJECT_OPEN)
+                return -EBADMSG;
+
+        r = json_scoped_parse(tokens, &it, ntokens, p);
+        if (r < 0)
+                return r;
+
+        *rv = p;
+        p = NULL;
+
+        return 0;
+}
+
+static int json_tokens(const char *string, size_t size, json_variant ***tokens, size_t *n) {
+        _cleanup_free_ char *buf = NULL;
+        _cleanup_(json_variant_array_unrefp) json_variant **items = NULL;
+        union json_value v = {};
+        void *json_state = NULL;
+        const char *p;
+        int t, r;
+        size_t allocated = 0, s = 0;
+
+        assert(string);
+        assert(n);
+
+        if (size <= 0)
+                return -EBADMSG;
+
+        buf = strndup(string, size);
+        if (!buf)
+                return -ENOMEM;
+
+        p = buf;
+        for (;;) {
+                _cleanup_free_ char *rstr = NULL;
+                _cleanup_jsonunref_ json_variant *var = NULL;
+
+                t = json_tokenize(&p, &rstr, &v, &json_state, NULL);
+
+                if (t < 0)
+                        return t;
+                else if (t == JSON_END)
+                        break;
+
+                if (t <= JSON_ARRAY_CLOSE) {
+                        r = json_variant_new(&var, JSON_VARIANT_CONTROL);
+                        if (r < 0)
+                                return r;
+                        var->value.integer = t;
+                } else {
+                        switch (t) {
+                        case JSON_STRING:
+                                r = json_variant_new(&var, JSON_VARIANT_STRING);
+                                if (r < 0)
+                                        return r;
+                                var->size = strlen(rstr);
+                                var->string = strdup(rstr);
+                                if (!var->string) {
+                                        return -ENOMEM;
+                                }
+                                break;
+                        case JSON_INTEGER:
+                                r = json_variant_new(&var, JSON_VARIANT_INTEGER);
+                                if (r < 0)
+                                        return r;
+                                var->value = v;
+                                break;
+                        case JSON_REAL:
+                                r = json_variant_new(&var, JSON_VARIANT_REAL);
+                                if (r < 0)
+                                        return r;
+                                var->value = v;
+                                break;
+                        case JSON_BOOLEAN:
+                                r = json_variant_new(&var, JSON_VARIANT_BOOLEAN);
+                                if (r < 0)
+                                        return r;
+                                var->value = v;
+                                break;
+                        case JSON_NULL:
+                                r = json_variant_new(&var, JSON_VARIANT_NULL);
+                                if (r < 0)
+                                        return r;
+                                break;
+                        }
+                }
+
+                if (!GREEDY_REALLOC(items, allocated, s+2))
+                        return -ENOMEM;
+
+                items[s++] = var;
+                items[s] = NULL;
+                var = NULL;
+        }
+
+        *n = s;
+        *tokens = items;
+        items = NULL;
+
+        return 0;
+}
+
+
+int json_parse(const char *string, json_variant **rv) {
+        _cleanup_(json_variant_array_unrefp) json_variant **s = NULL;
+        json_variant *v = NULL;
+        size_t n = 0;
+        int r;
+
+        assert(string);
+        assert(!*rv);
+
+        r = json_tokens(string, strlen(string), &s, &n);
+        if (r < 0)
+                return r;
+
+        r = json_parse_tokens(s, n, &v);
+        if (r < 0)
+                return r;
+
+        *rv = v;
+        return 0;
+}
diff --git a/src/shared/json.h b/src/shared/json.h
index 55976d5..4f0a564 100644
--- a/src/shared/json.h
+++ b/src/shared/json.h
@@ -22,6 +22,7 @@
 ***/
 
 #include <stdbool.h>
+#include "util.h"
 
 enum {
         JSON_END,
@@ -38,12 +39,47 @@ enum {
         JSON_NULL,
 };
 
+enum {
+        JSON_VARIANT_CONTROL,
+        JSON_VARIANT_STRING,
+        JSON_VARIANT_INTEGER,
+        JSON_VARIANT_BOOLEAN,
+        JSON_VARIANT_REAL,
+        JSON_VARIANT_ARRAY,
+        JSON_VARIANT_OBJECT,
+        JSON_VARIANT_NULL
+};
+
 union json_value {
         bool boolean;
         double real;
         intmax_t integer;
 };
 
+typedef struct json_variant {
+        union {
+                char *string;
+                struct json_variant *obj;
+                union json_value value;
+        };
+        int type;
+        unsigned size;
+} json_variant;
+
+int json_variant_new(json_variant **ret, int json_type);
+json_variant *json_variant_unref(json_variant *);
+DEFINE_TRIVIAL_CLEANUP_FUNC(json_variant *, json_variant_unref);
+#define _cleanup_jsonunref_ _cleanup_(json_variant_unrefp)
+
+char *json_variant_string(json_variant *);
+bool json_variant_bool(json_variant *);
+intmax_t json_variant_integer(json_variant *);
+double json_variant_real(json_variant *);
+
+json_variant *json_variant_element(json_variant *, unsigned index);
+json_variant *json_variant_value(json_variant *, const char *key);
+
 #define JSON_VALUE_NULL ((union json_value) {})
 
 int json_tokenize(const char **p, char **ret_string, union json_value *ret_value, void **state, unsigned *line);
+int json_parse(const char *string, json_variant **rv);
-- 
2.1.0

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://lists.freedesktop.org/archives/systemd-devel/attachments/20150507/e36fbf0c/attachment-0001.sig>


More information about the systemd-devel mailing list