HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd/sd-json/sd-json.c

216,944 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: dea16611a22e2975d723e14e171d670c48aede44

Lines 1–1000 of 6,031

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <locale.h>
#include <stdlib.h>
 
#include "sd-json.h"
#include "sd-messages.h"
 
#include "alloc-util.h"
#include "ansi-color.h"
#include "env-util.h"
#include "errno-util.h"
#include "escape.h"
#include "ether-addr-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "float.h"
#include "hexdecoct.h"
#include "in-addr-util.h"
#include "json-internal.h"
#include "json-util.h"
#include "log.h"
#include "math-util.h"
#include "memory-util.h"
#include "memstream-util.h"
#include "ordered-set.h"
#include "parse-util.h"
#include "pidref.h"
#include "ratelimit.h"
#include "signal-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "time-util.h"
#include "user-util.h"
#include "utf8.h"
 
/* Refuse putting together variants with a larger depth than 2K by default (as a protection against overflowing stacks
 * if code processes JSON objects recursively. Note that we store the depth in an uint16_t, hence make sure this
 * remains under 2^16.
 *
 * The value first was 16k, but it was discovered to be too high on llvm/x86-64. See also:
 * https://github.com/systemd/systemd/issues/10738
 *
 * The value then was 4k, but it was discovered to be too high on s390x/aarch64. See also:
 * https://github.com/systemd/systemd/issues/14396 */
 
#define DEPTH_MAX (2U*1024U)
assert_cc(DEPTH_MAX <= UINT16_MAX);
 
typedef struct JsonSource {
        /* When we parse from a file or similar, encodes the filename, to indicate the source of a json variant */
        unsigned n_ref;
        unsigned max_line;
        unsigned max_column;
        char name[];
} JsonSource;
 
/* On x86-64 this whole structure should have a size of 5 * 64 bit = 40 bytes */
struct sd_json_variant {
        union {
                /* We either maintain a reference counter for this variant itself, or we are embedded into an
                 * array/object, in which case only that surrounding object is ref-counted. (If 'embedded' is false,
                 * see below.) */
                unsigned n_ref;
 
                /* If this sd_json_variant is part of an array/object, then this field points to the surrounding
                 * JSON_VARIANT_ARRAY/JSON_VARIANT_OBJECT object. (If 'embedded' is true, see below.) */
                sd_json_variant *parent;
        };
 
        /* If this was parsed from some file or buffer, this stores where from, as well as the source line/column */
        JsonSource *source;
        unsigned line, column;
 
        /* The current 'depth' of the sd_json_variant, i.e. how many levels of member variants this has */
        uint16_t depth;
 
        signed int type:8; /* actually sd_json_variant_type_t, but reduced to 8bit size (we used "signed int"
                            * rather than just "int", since apparently compilers are free to pick signedness
                            * otherwise. We don't really care about the signedess, we care more about whether
                            * the enum can be converted to this, but we never use the negative values of
                            * sd_json_variant_type_t here.) */
 
        /* A marker whether this variant is embedded into in array/object or not. If true, the 'parent' pointer above
         * is valid. If false, the 'n_ref' field above is valid instead. */
        bool is_embedded:1;
 
        /* In some conditions (for example, if this object is part of an array of strings or objects), we don't store
         * any data inline, but instead simply reference an external object and act as surrogate of it. In that case
         * this bool is set, and the external object is referenced through the .reference field below. */
        bool is_reference:1;
 
        /* While comparing two arrays, we use this for marking what we already have seen */
        bool is_marked:1;
 
        /* Erase from memory when freeing */
        bool sensitive:1;
 
        /* True if we know that any referenced json object is marked sensitive */
        bool recursive_sensitive:1;
 
        /* If this is an object the fields are strictly ordered by name */
        bool sorted:1;
 
        /* If in addition to this object all objects referenced by it are also ordered strictly by name */
        bool normalized:1;
 
        union {
                /* For simple types we store the value in-line. */
                JsonValue value;
 
                /* For objects and arrays we store the number of elements immediately following */
                size_t n_elements;
 
                /* If is_reference as indicated above is set, this is where the reference object is actually stored. */
                sd_json_variant *reference;
 
                /* Strings are placed immediately after the structure. Note that when this is a sd_json_variant
                 * embedded into an array we might encode strings up to INLINE_STRING_LENGTH characters
                 * directly inside the element, while longer strings are stored as references. When this
                 * object is not embedded into an array, but stand-alone, we allocate the right size for the
                 * whole structure, i.e. the array might be much larger than INLINE_STRING_LENGTH. */
                DECLARE_FLEX_ARRAY(char, string);
        };
};
 
/* Inside string arrays we have a series of sd_json_variant structures one after the other. In this case, strings longer
 * than INLINE_STRING_MAX are stored as references, and all shorter ones inline. (This means — on x86-64 — strings up
 * to 7 chars are stored within the array elements, and all others in separate allocations) */
#define INLINE_STRING_MAX (sizeof(sd_json_variant) - offsetof(sd_json_variant, string) - 1U)
 
/* Let's make sure this structure isn't increased in size accidentally. This check is only for our most relevant arch
 * (x86-64). */
#if defined(__x86_64__) && __SIZEOF_POINTER__ == 8 && !defined(__EDG__)
assert_cc(sizeof(sd_json_variant) == 40U);
assert_cc(INLINE_STRING_MAX == 7U);
#endif
 
static JsonSource* json_source_new(const char *name) {
        JsonSource *s;
 
        assert(name);
 
        s = malloc(offsetof(JsonSource, name) + strlen(name) + 1);
        if (!s)
                return NULL;
 
        *s = (JsonSource) {
                .n_ref = 1,
        };
        strcpy(s->name, name);
 
        return s;
}
 
DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(JsonSource, json_source, mfree);
 
static bool json_source_equal(JsonSource *a, JsonSource *b) {
        if (a == b)
                return true;
 
        if (!a || !b)
                return false;
 
        return streq(a->name, b->name);
}
 
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonSource*, json_source_unref);
 
/* There are four kind of sd_json_variant* pointers:
 *
 *    1. NULL
 *    2. A 'regular' one, i.e. pointing to malloc() memory
 *    3. A 'magic' one, i.e. one of the special JSON_VARIANT_MAGIC_XYZ values, that encode a few very basic values directly in the pointer.
 *    4. A 'const string' one, i.e. a pointer to a const string.
 *
 * The four kinds of pointers can be discerned like this:
 *
 *    Detecting #1 is easy, just compare with NULL. Detecting #3 is similarly easy: all magic pointers are below
 *    _JSON_VARIANT_MAGIC_MAX (which is pretty low, within the first memory page, which is special on Linux and other
 *    OSes, as it is a faulting page). In order to discern #2 and #4 we check the lowest bit. If it's off it's #2,
 *    otherwise #4. This makes use of the fact that malloc() will return "maximum aligned" memory, which definitely
 *    means the pointer is even. This means we can use the uneven pointers to reference static strings, as long as we
 *    make sure that all static strings used like this are aligned to 2 (or higher), and that we mask the bit on
 *    access. The JSON_VARIANT_STRING_CONST() macro encodes strings as sd_json_variant* pointers, with the bit set. */
 
static bool json_variant_is_magic(const sd_json_variant *v) {
        if (!v)
                return false;
 
        return v < _JSON_VARIANT_MAGIC_MAX;
}
 
static bool json_variant_is_const_string(const sd_json_variant *v) {
 
        if (v < _JSON_VARIANT_MAGIC_MAX)
                return false;
 
        /* A proper sd_json_variant is aligned to whatever malloc() aligns things too, which is definitely not uneven. We
         * hence use all uneven pointers as indicators for const strings. */
 
        return (((uintptr_t) v) & 1) != 0;
}
 
static bool json_variant_is_regular(const sd_json_variant *v) {
 
        if (v < _JSON_VARIANT_MAGIC_MAX)
                return false;
 
        return (((uintptr_t) v) & 1) == 0;
}
 
static sd_json_variant* json_variant_dereference(sd_json_variant *v) {
 
        /* Recursively dereference variants that are references to other variants */
 
        if (!v)
                return NULL;
 
        if (!json_variant_is_regular(v))
                return v;
 
        if (!v->is_reference)
                return v;
 
        return json_variant_dereference(v->reference);
}
 
static uint16_t json_variant_depth(sd_json_variant *v) {
 
        v = json_variant_dereference(v);
        if (!v)
                return 0;
 
        if (!json_variant_is_regular(v))
                return 0;
 
        return v->depth;
}
 
static sd_json_variant* json_variant_formalize(sd_json_variant *v) {
 
        /* Converts json variant pointers to their normalized form, i.e. fully dereferenced and wherever
         * possible converted to the "magic" version if there is one */
 
        if (!v)
                return NULL;
 
        v = json_variant_dereference(v);
 
        switch (sd_json_variant_type(v)) {
 
        case SD_JSON_VARIANT_BOOLEAN:
                return sd_json_variant_boolean(v) ? JSON_VARIANT_MAGIC_TRUE : JSON_VARIANT_MAGIC_FALSE;
 
        case SD_JSON_VARIANT_NULL:
                return JSON_VARIANT_MAGIC_NULL;
 
        case SD_JSON_VARIANT_INTEGER:
                return sd_json_variant_integer(v) == 0 ? JSON_VARIANT_MAGIC_ZERO_INTEGER : v;
 
        case SD_JSON_VARIANT_UNSIGNED:
                return sd_json_variant_unsigned(v) == 0 ? JSON_VARIANT_MAGIC_ZERO_UNSIGNED : v;
 
        case SD_JSON_VARIANT_REAL:
                return iszero_safe(sd_json_variant_real(v)) ? JSON_VARIANT_MAGIC_ZERO_REAL : v;
 
        case SD_JSON_VARIANT_STRING:
                return isempty(sd_json_variant_string(v)) ? JSON_VARIANT_MAGIC_EMPTY_STRING : v;
 
        case SD_JSON_VARIANT_ARRAY:
                return sd_json_variant_elements(v) == 0 ? JSON_VARIANT_MAGIC_EMPTY_ARRAY : v;
 
        case SD_JSON_VARIANT_OBJECT:
                return sd_json_variant_elements(v) == 0 ? JSON_VARIANT_MAGIC_EMPTY_OBJECT : v;
 
        default:
                return v;
        }
}
 
static sd_json_variant* json_variant_conservative_formalize(sd_json_variant *v) {
 
        /* Much like json_variant_formalize(), but won't simplify if the variant has a source/line location
         * attached to it, in order not to lose context */
 
        if (!v)
                return NULL;
 
        if (!json_variant_is_regular(v))
                return v;
 
        if (v->source || v->line > 0 || v->column > 0)
                return v;
 
        return json_variant_formalize(v);
}
 
static int json_variant_new(sd_json_variant **ret, sd_json_variant_type_t type, size_t space) {
        sd_json_variant *v;
 
        assert_return(ret, -EINVAL);
 
        v = malloc0(MAX(sizeof(sd_json_variant),
                        offsetof(sd_json_variant, value) + space));
        if (!v)
                return -ENOMEM;
 
        v->n_ref = 1;
        v->type = type;
 
        *ret = v;
        return 0;
}
 
_public_ int sd_json_variant_new_integer(sd_json_variant **ret, int64_t i) {
        sd_json_variant *v;
        int r;
 
        assert_return(ret, -EINVAL);
 
        if (i == 0) {
                *ret = JSON_VARIANT_MAGIC_ZERO_INTEGER;
                return 0;
        }
 
        r = json_variant_new(&v, SD_JSON_VARIANT_INTEGER, sizeof(i));
        if (r < 0)
                return r;
 
        v->value.integer = i;
        *ret = v;
 
        return 0;
}
 
_public_ int sd_json_variant_new_unsigned(sd_json_variant **ret, uint64_t u) {
        sd_json_variant *v;
        int r;
 
        assert_return(ret, -EINVAL);
        if (u == 0) {
                *ret = JSON_VARIANT_MAGIC_ZERO_UNSIGNED;
                return 0;
        }
 
        r = json_variant_new(&v, SD_JSON_VARIANT_UNSIGNED, sizeof(u));
        if (r < 0)
                return r;
 
        v->value.unsig = u;
        *ret = v;
 
        return 0;
}
 
_public_ int sd_json_variant_new_real(sd_json_variant **ret, double d) {
        sd_json_variant *v;
        int r;
 
        assert_return(ret, -EINVAL);
 
        r = fpclassify(d);
        switch (r) {
        case FP_NAN:
        case FP_INFINITE:
                /* JSON doesn't know NaN, +Infinity or -Infinity. Let's silently convert to 'null'. */
                *ret = JSON_VARIANT_MAGIC_NULL;
                return 0;
 
        case FP_ZERO:
                *ret = JSON_VARIANT_MAGIC_ZERO_REAL;
                return 0;
        }
 
        r = json_variant_new(&v, SD_JSON_VARIANT_REAL, sizeof(d));
        if (r < 0)
                return r;
 
        v->value.real = d;
        *ret = v;
 
        return 0;
}
 
_public_ int sd_json_variant_new_boolean(sd_json_variant **ret, int b) {
        assert_return(ret, -EINVAL);
 
        if (b)
                *ret = JSON_VARIANT_MAGIC_TRUE;
        else
                *ret = JSON_VARIANT_MAGIC_FALSE;
 
        return 0;
}
 
_public_ int sd_json_variant_new_null(sd_json_variant **ret) {
        assert_return(ret, -EINVAL);
 
        *ret = JSON_VARIANT_MAGIC_NULL;
        return 0;
}
 
_public_ int sd_json_variant_new_stringn(sd_json_variant **ret, const char *s, size_t n) {
        sd_json_variant *v;
        int r;
 
        assert_return(ret, -EINVAL);
        if (!s) {
                assert_return(IN_SET(n, 0, SIZE_MAX), -EINVAL);
                return sd_json_variant_new_null(ret);
        }
        if (n == SIZE_MAX) /* determine length automatically */
                n = strlen(s);
        else if (memchr(s, 0, n)) /* don't allow embedded NUL, as we can't express that in JSON */
                return -EINVAL;
        if (n == 0) {
                *ret = JSON_VARIANT_MAGIC_EMPTY_STRING;
                return 0;
        }
 
        if (!utf8_is_valid_n(s, n)) /* JSON strings must be valid UTF-8 */
                return -EUCLEAN;
 
        r = json_variant_new(&v, SD_JSON_VARIANT_STRING, n + 1);
        if (r < 0)
                return r;
 
        memcpy(v->string, s, n);
        v->string[n] = 0;
 
        *ret = v;
        return 0;
}
 
_public_ int sd_json_variant_new_string(sd_json_variant **ret, const char *s) {
        return sd_json_variant_new_stringn(ret, s, SIZE_MAX);
}
 
_public_ int sd_json_variant_new_base64(sd_json_variant **ret, const void *p, size_t n) {
        _cleanup_free_ char *s = NULL;
        ssize_t k;
 
        assert_return(ret, -EINVAL);
        assert_return(n == 0 || p, -EINVAL);
 
        k = base64mem(p, n, &s);
        if (k < 0)
                return k;
 
        return sd_json_variant_new_stringn(ret, s, k);
}
 
_public_ int sd_json_variant_new_base32hex(sd_json_variant **ret, const void *p, size_t n) {
        _cleanup_free_ char *s = NULL;
 
        assert_return(ret, -EINVAL);
        assert_return(n == 0 || p, -EINVAL);
 
        s = base32hexmem(p, n, false);
        if (!s)
                return -ENOMEM;
 
        return sd_json_variant_new_string(ret, s);
}
 
_public_ int sd_json_variant_new_hex(sd_json_variant **ret, const void *p, size_t n) {
        _cleanup_free_ char *s = NULL;
 
        assert_return(ret, -EINVAL);
        assert_return(n == 0 || p, -EINVAL);
 
        s = hexmem(p, n);
        if (!s)
                return -ENOMEM;
 
        return sd_json_variant_new_stringn(ret, s, n*2);
}
 
_public_ int sd_json_variant_new_octescape(sd_json_variant **ret, const void *p, size_t n) {
        _cleanup_free_ char *s = NULL;
 
        assert_return(ret, -EINVAL);
        assert_return(n == 0 || p, -EINVAL);
 
        s = octescape(p, n);
        if (!s)
                return -ENOMEM;
 
        return sd_json_variant_new_string(ret, s);
}
 
_public_ int sd_json_variant_new_id128(sd_json_variant **ret, sd_id128_t id) {
        return sd_json_variant_new_string(ret, SD_ID128_TO_STRING(id));
}
 
_public_ int sd_json_variant_new_uuid(sd_json_variant **ret, sd_id128_t id) {
        return sd_json_variant_new_string(ret, SD_ID128_TO_UUID_STRING(id));
}
 
static void json_variant_set(sd_json_variant *a, sd_json_variant *b) {
        assert(a);
 
        b = json_variant_dereference(b);
        if (!b) {
                a->type = SD_JSON_VARIANT_NULL;
                return;
        }
 
        a->type = sd_json_variant_type(b);
        switch (a->type) {
 
        case SD_JSON_VARIANT_INTEGER:
                a->value.integer = sd_json_variant_integer(b);
                break;
 
        case SD_JSON_VARIANT_UNSIGNED:
                a->value.unsig = sd_json_variant_unsigned(b);
                break;
 
        case SD_JSON_VARIANT_REAL:
                a->value.real = sd_json_variant_real(b);
                break;
 
        case SD_JSON_VARIANT_BOOLEAN:
                a->value.boolean = sd_json_variant_boolean(b);
                break;
 
        case SD_JSON_VARIANT_STRING: {
                const char *s;
 
                assert_se(s = sd_json_variant_string(b));
 
                /* Short strings we can store inline */
                if (strnlen(s, INLINE_STRING_MAX+1) <= INLINE_STRING_MAX) {
                        strcpy(a->string, s);
                        break;
                }
 
                /* For longer strings, use a reference… */
                _fallthrough_;
        }
 
        case SD_JSON_VARIANT_ARRAY:
        case SD_JSON_VARIANT_OBJECT:
                a->is_reference = true;
                a->reference = sd_json_variant_ref(json_variant_conservative_formalize(b));
                break;
 
        case SD_JSON_VARIANT_NULL:
                break;
 
        default:
                assert_not_reached();
        }
}
 
static void json_variant_copy_source(sd_json_variant *v, sd_json_variant *from) {
        assert(v);
 
        if (!json_variant_is_regular(from))
                return;
 
        v->line = from->line;
        v->column = from->column;
        v->source = json_source_ref(from->source);
}
 
static int json_variant_array_put_element(sd_json_variant *array, sd_json_variant *element) {
        assert(array);
        sd_json_variant *w = array + 1 + array->n_elements;
 
        uint16_t d = json_variant_depth(element);
        if (d >= DEPTH_MAX) /* Refuse too deep nesting */
                return -ELNRNG;
        if (d >= array->depth)
                array->depth = d + 1;
        array->n_elements++;
 
        *w = (sd_json_variant) {
                .is_embedded = true,
                .parent = array,
        };
 
        json_variant_set(w, element);
        json_variant_copy_source(w, element);
 
        if (!sd_json_variant_is_normalized(element))
                array->normalized = false;
 
        return 0;
}
 
_public_ int sd_json_variant_new_array(sd_json_variant **ret, sd_json_variant **array, size_t n) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
        int r;
 
        assert_return(ret, -EINVAL);
        if (n == 0) {
                *ret = JSON_VARIANT_MAGIC_EMPTY_ARRAY;
                return 0;
        }
        assert_return(array, -EINVAL);
 
        v = new(sd_json_variant, n + 1);
        if (!v)
                return -ENOMEM;
 
        *v = (sd_json_variant) {
                .n_ref = 1,
                .type = SD_JSON_VARIANT_ARRAY,
                .normalized = true,
        };
 
        while (v->n_elements < n) {
                r = json_variant_array_put_element(v, array[v->n_elements]);
                if (r < 0)
                        return r;
        }
 
        *ret = TAKE_PTR(v);
        return 0;
}
 
_public_ int sd_json_variant_new_array_bytes(sd_json_variant **ret, const void *p, size_t n) {
        assert_return(ret, -EINVAL);
        if (n == 0) {
                *ret = JSON_VARIANT_MAGIC_EMPTY_ARRAY;
                return 0;
        }
        assert_return(p, -EINVAL);
 
        sd_json_variant *v = new(sd_json_variant, n + 1);
        if (!v)
                return -ENOMEM;
 
        *v = (sd_json_variant) {
                .n_ref = 1,
                .type = SD_JSON_VARIANT_ARRAY,
                .n_elements = n,
                .depth = 1,
        };
 
        for (size_t i = 0; i < n; i++) {
                sd_json_variant *w = v + 1 + i;
 
                *w = (sd_json_variant) {
                        .is_embedded = true,
                        .parent = v,
                        .type = SD_JSON_VARIANT_UNSIGNED,
                        .value.unsig = ((const uint8_t*) p)[i],
                };
        }
 
        v->normalized = true;
 
        *ret = v;
        return 0;
}
 
_public_ int sd_json_variant_new_array_strv(sd_json_variant **ret, char **l) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
        size_t n;
        int r;
 
        assert_return(ret, -EINVAL);
 
        n = strv_length(l);
        if (n == 0) {
                *ret = JSON_VARIANT_MAGIC_EMPTY_ARRAY;
                return 0;
        }
 
        v = new(sd_json_variant, n + 1);
        if (!v)
                return -ENOMEM;
 
        *v = (sd_json_variant) {
                .n_ref = 1,
                .type = SD_JSON_VARIANT_ARRAY,
                .depth = 1,
        };
 
        for (v->n_elements = 0; v->n_elements < n; v->n_elements++) {
                sd_json_variant *w = v + 1 + v->n_elements;
                size_t k;
 
                *w = (sd_json_variant) {
                        .is_embedded = true,
                        .parent = v,
                        .type = SD_JSON_VARIANT_STRING,
                };
 
                k = strlen(l[v->n_elements]);
 
                if (k > INLINE_STRING_MAX) {
                        /* If string is too long, store it as reference. */
 
                        r = sd_json_variant_new_string(&w->reference, l[v->n_elements]);
                        if (r < 0)
                                return r;
 
                        w->is_reference = true;
                } else {
                        if (!utf8_is_valid_n(l[v->n_elements], k)) /* JSON strings must be valid UTF-8 */
                                return -EUCLEAN;
 
                        memcpy(w->string, l[v->n_elements], k+1);
                }
        }
 
        v->normalized = true;
 
        *ret = TAKE_PTR(v);
        return 0;
}
 
_public_ int sd_json_variant_new_object(sd_json_variant **ret, sd_json_variant **array, size_t n) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
        const char *prev = NULL;
        bool sorted = true, normalized = true;
 
        assert_return(ret, -EINVAL);
        if (n == 0) {
                *ret = JSON_VARIANT_MAGIC_EMPTY_OBJECT;
                return 0;
        }
        assert_return(array, -EINVAL);
        assert_return(n % 2 == 0, -EINVAL);
 
        v = new(sd_json_variant, n + 1);
        if (!v)
                return -ENOMEM;
 
        *v = (sd_json_variant) {
                .n_ref = 1,
                .type = SD_JSON_VARIANT_OBJECT,
        };
 
        for (v->n_elements = 0; v->n_elements < n; v->n_elements++) {
                sd_json_variant *w = v + 1 + v->n_elements,
                            *c = array[v->n_elements];
                uint16_t d;
 
                if ((v->n_elements & 1) == 0) {
                        const char *k;
 
                        if (!sd_json_variant_is_string(c))
                                return -EINVAL; /* Every second one needs to be a string, as it is the key name */
 
                        assert_se(k = sd_json_variant_string(c));
 
                        if (prev && strcmp(k, prev) <= 0)
                                sorted = normalized = false;
 
                        prev = k;
                } else if (!sd_json_variant_is_normalized(c))
                        normalized = false;
 
                d = json_variant_depth(c);
                if (d >= DEPTH_MAX) /* Refuse too deep nesting */
                        return -ELNRNG;
                if (d >= v->depth)
                        v->depth = d + 1;
 
                *w = (sd_json_variant) {
                        .is_embedded = true,
                        .parent = v,
                };
 
                json_variant_set(w, c);
                json_variant_copy_source(w, c);
        }
 
        v->normalized = normalized;
        v->sorted = sorted;
 
        *ret = TAKE_PTR(v);
        return 0;
}
 
static size_t json_variant_size(sd_json_variant* v) {
        if (!json_variant_is_regular(v))
                return 0;
 
        if (v->is_reference)
                return offsetof(sd_json_variant, reference) + sizeof(sd_json_variant*);
 
        switch (v->type) {
 
        case SD_JSON_VARIANT_STRING:
                return offsetof(sd_json_variant, string) + strlen(v->string) + 1;
 
        case SD_JSON_VARIANT_REAL:
                return offsetof(sd_json_variant, value) + sizeof(double);
 
        case SD_JSON_VARIANT_UNSIGNED:
                return offsetof(sd_json_variant, value) + sizeof(uint64_t);
 
        case SD_JSON_VARIANT_INTEGER:
                return offsetof(sd_json_variant, value) + sizeof(int64_t);
 
        case SD_JSON_VARIANT_BOOLEAN:
                return offsetof(sd_json_variant, value) + sizeof(bool);
 
        case SD_JSON_VARIANT_ARRAY:
        case SD_JSON_VARIANT_OBJECT:
                return offsetof(sd_json_variant, n_elements) + sizeof(size_t);
 
        case SD_JSON_VARIANT_NULL:
                return offsetof(sd_json_variant, value);
 
        default:
                assert_not_reached();
        }
}
 
static void json_variant_free_inner(sd_json_variant *v, bool force_sensitive) {
        bool sensitive;
 
        assert(v);
 
        if (!json_variant_is_regular(v))
                return;
 
        json_source_unref(v->source);
 
        sensitive = v->sensitive || force_sensitive;
 
        if (v->is_reference) {
                if (sensitive)
                        sd_json_variant_sensitive(v->reference);
 
                sd_json_variant_unref(v->reference);
                return;
        }
 
        if (IN_SET(v->type, SD_JSON_VARIANT_ARRAY, SD_JSON_VARIANT_OBJECT))
                for (size_t i = 0; i < v->n_elements; i++)
                        json_variant_free_inner(v + 1 + i, sensitive);
 
        if (sensitive)
                explicit_bzero_safe(v, json_variant_size(v));
}
 
static unsigned json_variant_n_ref(const sd_json_variant *v) {
        /* Return the number of references to v.
         * 0  => NULL or not a regular object or embedded.
         * >0 => number of references
         */
 
        if (!v || !json_variant_is_regular(v) || v->is_embedded)
                return 0;
 
        assert(v->n_ref > 0);
        return v->n_ref;
}
 
_public_ sd_json_variant *sd_json_variant_ref(sd_json_variant *v) {
        if (!v)
                return NULL;
        if (!json_variant_is_regular(v))
                return v;
 
        if (v->is_embedded)
                sd_json_variant_ref(v->parent); /* ref the compounding variant instead */
        else {
                assert(v->n_ref > 0);
                v->n_ref++;
        }
 
        return v;
}
 
_public_ sd_json_variant *sd_json_variant_unref(sd_json_variant *v) {
        if (!v)
                return NULL;
        if (!json_variant_is_regular(v))
                return NULL;
 
        if (v->is_embedded)
                sd_json_variant_unref(v->parent);
        else {
                assert(v->n_ref > 0);
                v->n_ref--;
 
                if (v->n_ref == 0) {
                        json_variant_free_inner(v, false);
                        free(v);
                }
        }
 
        return NULL;
}
 
_public_ void sd_json_variant_unref_many(sd_json_variant **array, size_t n) {
        FOREACH_ARRAY(v, array, n)
                sd_json_variant_unref(*v);
 
        free(array);
}
 
_public_ const char* sd_json_variant_string(sd_json_variant *v) {
        if (!v)
                return NULL;
        if (v == JSON_VARIANT_MAGIC_EMPTY_STRING)
                return "";
        if (json_variant_is_magic(v))
                goto mismatch;
        if (json_variant_is_const_string(v)) {
                uintptr_t p = (uintptr_t) v;
 
                assert((p & 1) != 0);
                return (const char*) (p ^ 1U);
        }
 
        if (v->is_reference)
                return sd_json_variant_string(v->reference);
        if (v->type != SD_JSON_VARIANT_STRING)
                goto mismatch;
 
        return v->string;
 
mismatch:
        log_debug("Non-string JSON variant requested as string, returning NULL.");
        return NULL;
}
 
_public_ int sd_json_variant_boolean(sd_json_variant *v) {
        if (!v)
                goto mismatch;
        if (v == JSON_VARIANT_MAGIC_TRUE)
                return true;
        if (v == JSON_VARIANT_MAGIC_FALSE)
                return false;
        if (!json_variant_is_regular(v))
                goto mismatch;
        if (v->type != SD_JSON_VARIANT_BOOLEAN)
                goto mismatch;
        if (v->is_reference)
                return sd_json_variant_boolean(v->reference);
 
        return v->value.boolean;
 
mismatch:
        log_debug("Non-boolean JSON variant requested as boolean, returning false.");
        return false;
}
 
_public_ int64_t sd_json_variant_integer(sd_json_variant *v) {
        if (!v)
                goto mismatch;
        if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||
            v == JSON_VARIANT_MAGIC_ZERO_UNSIGNED ||
            v == JSON_VARIANT_MAGIC_ZERO_REAL)
                return 0;
        if (!json_variant_is_regular(v))
                goto mismatch;
        if (v->is_reference)
                return sd_json_variant_integer(v->reference);
 
        switch (v->type) {
 
        case SD_JSON_VARIANT_INTEGER:
                return v->value.integer;
 
        case SD_JSON_VARIANT_UNSIGNED:
                if (v->value.unsig <= INT64_MAX)
                        return (int64_t) v->value.unsig;
 
                log_debug("Unsigned integer %" PRIu64 " requested as signed integer and out of range, returning 0.", v->value.unsig);
                return 0;
 
        case SD_JSON_VARIANT_REAL: {
                int64_t converted;
 
                converted = (int64_t) v->value.real;
 
                if (fp_equal((double) converted, v->value.real))
                        return converted;
 
                log_debug("Real %g requested as integer, and cannot be converted losslessly, returning 0.", v->value.real);
                return 0;
        }
 
        default:
                ;
        }
 
mismatch:
        log_debug("Non-integer JSON variant requested as integer, returning 0.");
        return 0;
}
 
_public_ uint64_t sd_json_variant_unsigned(sd_json_variant *v) {
        if (!v)
                goto mismatch;
        if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||