mirror of
https://github.com/systemd/systemd
synced 2024-09-06 16:56:43 +00:00
Merge pull request #21424 from keszybz/json-double
Use double and int64_t types in json
This commit is contained in:
commit
26b2832992
|
@ -200,7 +200,7 @@ _public_ int cryptsetup_token_validate(
|
|||
}
|
||||
|
||||
JSON_VARIANT_ARRAY_FOREACH(e, w) {
|
||||
uintmax_t u;
|
||||
uint64_t u;
|
||||
|
||||
if (!json_variant_is_number(e)) {
|
||||
crypt_log_debug(cd, "TPM2 PCR is not a number.");
|
||||
|
|
|
@ -78,7 +78,7 @@ int parse_luks2_tpm2_data(
|
|||
return -EINVAL;
|
||||
|
||||
JSON_VARIANT_ARRAY_FOREACH(e, w) {
|
||||
uintmax_t u;
|
||||
uint64_t u;
|
||||
|
||||
if (!json_variant_is_number(e))
|
||||
return -EINVAL;
|
||||
|
|
|
@ -116,7 +116,7 @@ int find_tpm2_auto_data(
|
|||
|
||||
assert(pcr_mask == 0);
|
||||
JSON_VARIANT_ARRAY_FOREACH(e, w) {
|
||||
uintmax_t u;
|
||||
uint64_t u;
|
||||
|
||||
if (!json_variant_is_number(e))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
|
|
|
@ -837,7 +837,7 @@ static int update_last_change(JsonVariant **v, bool with_password, bool override
|
|||
|
||||
c = json_variant_by_key(*v, "lastChangeUSec");
|
||||
if (c) {
|
||||
uintmax_t u;
|
||||
uint64_t u;
|
||||
|
||||
if (!override)
|
||||
goto update_password;
|
||||
|
@ -860,7 +860,7 @@ update_password:
|
|||
|
||||
c = json_variant_by_key(*v, "lastPasswordChangeUSec");
|
||||
if (c) {
|
||||
uintmax_t u;
|
||||
uint64_t u;
|
||||
|
||||
if (!override)
|
||||
return 0;
|
||||
|
|
|
@ -133,9 +133,9 @@ static int dump_link_description(char **patterns) {
|
|||
return log_oom();
|
||||
|
||||
JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(v, "Interfaces")) {
|
||||
char ifindex_str[DECIMAL_STR_MAX(intmax_t)];
|
||||
char ifindex_str[DECIMAL_STR_MAX(int64_t)];
|
||||
const char *name;
|
||||
intmax_t index;
|
||||
int64_t index;
|
||||
size_t pos;
|
||||
|
||||
name = json_variant_string(json_variant_by_key(i, "Name"));
|
||||
|
|
|
@ -94,7 +94,7 @@ int link_build_json(Link *link, JsonVariant **ret) {
|
|||
}
|
||||
|
||||
static int link_json_compare(JsonVariant * const *a, JsonVariant * const *b) {
|
||||
intmax_t index_a, index_b;
|
||||
int64_t index_a, index_b;
|
||||
|
||||
assert(a && *a);
|
||||
assert(b && *b);
|
||||
|
|
|
@ -99,7 +99,7 @@ static int oci_terminal(const char *name, JsonVariant *v, JsonDispatchFlags flag
|
|||
|
||||
static int oci_console_dimension(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
unsigned *u = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
assert(u);
|
||||
|
||||
|
@ -226,7 +226,7 @@ static int oci_rlimit_value(const char *name, JsonVariant *v, JsonDispatchFlags
|
|||
|
||||
z = (rlim_t) json_variant_unsigned(v);
|
||||
|
||||
if ((uintmax_t) z != json_variant_unsigned(v))
|
||||
if ((uint64_t) z != json_variant_unsigned(v))
|
||||
return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
|
||||
"rlimits limit out of range, refusing.");
|
||||
}
|
||||
|
@ -346,7 +346,7 @@ static int oci_capabilities(const char *name, JsonVariant *v, JsonDispatchFlags
|
|||
|
||||
static int oci_oom_score_adj(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
Settings *s = userdata;
|
||||
intmax_t k;
|
||||
int64_t k;
|
||||
|
||||
assert(s);
|
||||
|
||||
|
@ -363,14 +363,14 @@ static int oci_oom_score_adj(const char *name, JsonVariant *v, JsonDispatchFlags
|
|||
|
||||
static int oci_uid_gid(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
uid_t *uid = userdata, u;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
assert(uid);
|
||||
assert_cc(sizeof(uid_t) == sizeof(gid_t));
|
||||
|
||||
k = json_variant_unsigned(v);
|
||||
u = (uid_t) k;
|
||||
if ((uintmax_t) u != k)
|
||||
if ((uint64_t) u != k)
|
||||
return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
|
||||
"UID/GID out of range: %ji", k);
|
||||
|
||||
|
@ -698,7 +698,7 @@ static int oci_namespaces(const char *name, JsonVariant *v, JsonDispatchFlags fl
|
|||
|
||||
static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
uid_t *uid = userdata, u;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
assert(uid);
|
||||
assert_cc(sizeof(uid_t) == sizeof(gid_t));
|
||||
|
@ -709,7 +709,7 @@ static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags
|
|||
|
||||
k = json_variant_unsigned(v);
|
||||
u = (uid_t) k;
|
||||
if ((uintmax_t) u != k)
|
||||
if ((uint64_t) u != k)
|
||||
return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
|
||||
"UID/GID out of range: %ji", k);
|
||||
if (u == 0)
|
||||
|
@ -803,7 +803,7 @@ static int oci_device_type(const char *name, JsonVariant *v, JsonDispatchFlags f
|
|||
|
||||
static int oci_device_major(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
unsigned *u = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
assert_se(u);
|
||||
|
||||
|
@ -818,7 +818,7 @@ static int oci_device_major(const char *name, JsonVariant *v, JsonDispatchFlags
|
|||
|
||||
static int oci_device_minor(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
unsigned *u = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
assert_se(u);
|
||||
|
||||
|
@ -833,14 +833,14 @@ static int oci_device_minor(const char *name, JsonVariant *v, JsonDispatchFlags
|
|||
|
||||
static int oci_device_file_mode(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
mode_t *mode = userdata, m;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
assert(mode);
|
||||
|
||||
k = json_variant_unsigned(v);
|
||||
m = (mode_t) k;
|
||||
|
||||
if ((m & ~07777) != 0 || (uintmax_t) m != k)
|
||||
if ((m & ~07777) != 0 || (uint64_t) m != k)
|
||||
return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
|
||||
"fileMode out of range, refusing.");
|
||||
|
||||
|
@ -1175,7 +1175,7 @@ static int oci_cgroup_devices(const char *name, JsonVariant *v, JsonDispatchFlag
|
|||
|
||||
static int oci_cgroup_memory_limit(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
uint64_t *m = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
assert(m);
|
||||
|
||||
|
@ -1275,7 +1275,7 @@ struct cpu_data {
|
|||
|
||||
static int oci_cgroup_cpu_shares(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
uint64_t *u = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
assert(u);
|
||||
|
||||
|
@ -1290,7 +1290,7 @@ static int oci_cgroup_cpu_shares(const char *name, JsonVariant *v, JsonDispatchF
|
|||
|
||||
static int oci_cgroup_cpu_quota(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
uint64_t *u = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
assert(u);
|
||||
|
||||
|
@ -1382,7 +1382,7 @@ static int oci_cgroup_cpu(const char *name, JsonVariant *v, JsonDispatchFlags fl
|
|||
|
||||
static int oci_cgroup_block_io_weight(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
Settings *s = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
|
@ -1414,18 +1414,18 @@ static int oci_cgroup_block_io_weight_device(const char *name, JsonVariant *v, J
|
|||
struct device_data {
|
||||
unsigned major;
|
||||
unsigned minor;
|
||||
uintmax_t weight;
|
||||
uint64_t weight;
|
||||
} data = {
|
||||
.major = UINT_MAX,
|
||||
.minor = UINT_MAX,
|
||||
.weight = UINTMAX_MAX,
|
||||
.weight = UINT64_MAX,
|
||||
};
|
||||
|
||||
static const JsonDispatch table[] = {
|
||||
{ "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY },
|
||||
{ "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY },
|
||||
{ "weight", JSON_VARIANT_UNSIGNED, json_dispatch_uintmax, offsetof(struct device_data, weight), 0 },
|
||||
{ "leafWeight", JSON_VARIANT_INTEGER, oci_unsupported, 0, JSON_PERMISSIVE },
|
||||
{ "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY },
|
||||
{ "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY },
|
||||
{ "weight", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct device_data, weight), 0 },
|
||||
{ "leafWeight", JSON_VARIANT_INTEGER, oci_unsupported, 0, JSON_PERMISSIVE },
|
||||
{}
|
||||
};
|
||||
|
||||
|
@ -1435,7 +1435,7 @@ static int oci_cgroup_block_io_weight_device(const char *name, JsonVariant *v, J
|
|||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (data.weight == UINTMAX_MAX)
|
||||
if (data.weight == UINT64_MAX)
|
||||
continue;
|
||||
|
||||
if (data.weight < CGROUP_BLKIO_WEIGHT_MIN || data.weight > CGROUP_BLKIO_WEIGHT_MAX)
|
||||
|
@ -1475,16 +1475,16 @@ static int oci_cgroup_block_io_throttle(const char *name, JsonVariant *v, JsonDi
|
|||
struct device_data {
|
||||
unsigned major;
|
||||
unsigned minor;
|
||||
uintmax_t rate;
|
||||
uint64_t rate;
|
||||
} data = {
|
||||
.major = UINT_MAX,
|
||||
.minor = UINT_MAX,
|
||||
};
|
||||
|
||||
static const JsonDispatch table[] = {
|
||||
{ "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY },
|
||||
{ "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY },
|
||||
{ "rate", JSON_VARIANT_UNSIGNED, json_dispatch_uintmax, offsetof(struct device_data, rate), JSON_MANDATORY },
|
||||
{ "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY },
|
||||
{ "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY },
|
||||
{ "rate", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct device_data, rate), JSON_MANDATORY },
|
||||
{}
|
||||
};
|
||||
|
||||
|
@ -1557,7 +1557,7 @@ static int oci_cgroup_pids(const char *name, JsonVariant *v, JsonDispatchFlags f
|
|||
|
||||
m = (uint64_t) json_variant_unsigned(k);
|
||||
|
||||
if ((uintmax_t) m != json_variant_unsigned(k))
|
||||
if ((uint64_t) m != json_variant_unsigned(k))
|
||||
return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
|
||||
"pids limit out of range, refusing.");
|
||||
}
|
||||
|
@ -2097,7 +2097,7 @@ static int oci_linux(const char *name, JsonVariant *v, JsonDispatchFlags flags,
|
|||
|
||||
static int oci_hook_timeout(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
|
||||
usec_t *u = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
k = json_variant_unsigned(v);
|
||||
if (k == 0 || k > (UINT64_MAX-1)/USEC_PER_SEC)
|
||||
|
|
|
@ -82,7 +82,7 @@ static uint32_t ifindex_to_scopeid(int family, const void *a, int ifindex) {
|
|||
|
||||
static int json_dispatch_ifindex(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
int *ifi = userdata;
|
||||
intmax_t t;
|
||||
int64_t t;
|
||||
|
||||
assert(variant);
|
||||
assert(ifi);
|
||||
|
@ -100,7 +100,7 @@ static int json_dispatch_ifindex(const char *name, JsonVariant *variant, JsonDis
|
|||
|
||||
static int json_dispatch_family(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
int *family = userdata;
|
||||
intmax_t t;
|
||||
int64_t t;
|
||||
|
||||
assert(variant);
|
||||
assert(family);
|
||||
|
@ -160,7 +160,7 @@ static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDis
|
|||
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name));
|
||||
|
||||
JSON_VARIANT_ARRAY_FOREACH(i, variant) {
|
||||
intmax_t b;
|
||||
int64_t b;
|
||||
|
||||
if (!json_variant_is_integer(i))
|
||||
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an integer.", k, strna(name));
|
||||
|
|
|
@ -364,7 +364,7 @@ static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDis
|
|||
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name));
|
||||
|
||||
JSON_VARIANT_ARRAY_FOREACH(i, variant) {
|
||||
intmax_t b;
|
||||
int64_t b;
|
||||
|
||||
if (!json_variant_is_integer(i))
|
||||
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an integer.", k, strna(name));
|
||||
|
|
|
@ -9,16 +9,16 @@
|
|||
* interface with this. */
|
||||
|
||||
typedef union JsonValue {
|
||||
/* Encodes a simple value. On x86-64 this structure is 16 bytes wide (as long double is 128bit). */
|
||||
/* Encodes a simple value. This structure is generally 8 bytes wide (as double is 64bit). */
|
||||
bool boolean;
|
||||
long double real;
|
||||
intmax_t integer;
|
||||
uintmax_t unsig;
|
||||
double real;
|
||||
int64_t integer;
|
||||
uint64_t unsig;
|
||||
} JsonValue;
|
||||
|
||||
/* Let's protect us against accidental structure size changes on our most relevant arch */
|
||||
#ifdef __x86_64__
|
||||
assert_cc(sizeof(JsonValue) == 16U);
|
||||
assert_cc(sizeof(JsonValue) == 8U);
|
||||
#endif
|
||||
|
||||
#define JSON_VALUE_NULL ((JsonValue) {})
|
||||
|
|
|
@ -114,16 +114,16 @@ struct JsonVariant {
|
|||
};
|
||||
};
|
||||
|
||||
/* Inside string arrays we have a series of JasonVariant structures one after the other. In this case, strings longer
|
||||
/* Inside string arrays we have a series of JsonVariant 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 15 chars are stored within the array elements, and all others in separate allocations) */
|
||||
* to 7 chars are stored within the array elements, and all others in separate allocations) */
|
||||
#define INLINE_STRING_MAX (sizeof(JsonVariant) - offsetof(JsonVariant, 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). */
|
||||
#ifdef __x86_64__
|
||||
assert_cc(sizeof(JsonVariant) == 48U);
|
||||
assert_cc(INLINE_STRING_MAX == 15U);
|
||||
assert_cc(sizeof(JsonVariant) == 40U);
|
||||
assert_cc(INLINE_STRING_MAX == 7U);
|
||||
#endif
|
||||
|
||||
static JsonSource* json_source_new(const char *name) {
|
||||
|
@ -305,7 +305,7 @@ static int json_variant_new(JsonVariant **ret, JsonVariantType type, size_t spac
|
|||
return 0;
|
||||
}
|
||||
|
||||
int json_variant_new_integer(JsonVariant **ret, intmax_t i) {
|
||||
int json_variant_new_integer(JsonVariant **ret, int64_t i) {
|
||||
JsonVariant *v;
|
||||
int r;
|
||||
|
||||
|
@ -326,7 +326,7 @@ int json_variant_new_integer(JsonVariant **ret, intmax_t i) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int json_variant_new_unsigned(JsonVariant **ret, uintmax_t u) {
|
||||
int json_variant_new_unsigned(JsonVariant **ret, uint64_t u) {
|
||||
JsonVariant *v;
|
||||
int r;
|
||||
|
||||
|
@ -346,7 +346,7 @@ int json_variant_new_unsigned(JsonVariant **ret, uintmax_t u) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int json_variant_new_real(JsonVariant **ret, long double d) {
|
||||
int json_variant_new_real(JsonVariant **ret, double d) {
|
||||
JsonVariant *v;
|
||||
int r;
|
||||
|
||||
|
@ -748,13 +748,13 @@ static size_t json_variant_size(JsonVariant* v) {
|
|||
return offsetof(JsonVariant, string) + strlen(v->string) + 1;
|
||||
|
||||
case JSON_VARIANT_REAL:
|
||||
return offsetof(JsonVariant, value) + sizeof(long double);
|
||||
return offsetof(JsonVariant, value) + sizeof(double);
|
||||
|
||||
case JSON_VARIANT_UNSIGNED:
|
||||
return offsetof(JsonVariant, value) + sizeof(uintmax_t);
|
||||
return offsetof(JsonVariant, value) + sizeof(uint64_t);
|
||||
|
||||
case JSON_VARIANT_INTEGER:
|
||||
return offsetof(JsonVariant, value) + sizeof(intmax_t);
|
||||
return offsetof(JsonVariant, value) + sizeof(int64_t);
|
||||
|
||||
case JSON_VARIANT_BOOLEAN:
|
||||
return offsetof(JsonVariant, value) + sizeof(bool);
|
||||
|
@ -895,7 +895,7 @@ mismatch:
|
|||
return false;
|
||||
}
|
||||
|
||||
intmax_t json_variant_integer(JsonVariant *v) {
|
||||
int64_t json_variant_integer(JsonVariant *v) {
|
||||
if (!v)
|
||||
goto mismatch;
|
||||
if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||
|
||||
|
@ -913,23 +913,23 @@ intmax_t json_variant_integer(JsonVariant *v) {
|
|||
return v->value.integer;
|
||||
|
||||
case JSON_VARIANT_UNSIGNED:
|
||||
if (v->value.unsig <= INTMAX_MAX)
|
||||
return (intmax_t) v->value.unsig;
|
||||
if (v->value.unsig <= INT64_MAX)
|
||||
return (int64_t) v->value.unsig;
|
||||
|
||||
log_debug("Unsigned integer %ju requested as signed integer and out of range, returning 0.", v->value.unsig);
|
||||
return 0;
|
||||
|
||||
case JSON_VARIANT_REAL: {
|
||||
intmax_t converted;
|
||||
int64_t converted;
|
||||
|
||||
converted = (intmax_t) v->value.real;
|
||||
converted = (int64_t) v->value.real;
|
||||
|
||||
DISABLE_WARNING_FLOAT_EQUAL;
|
||||
if ((long double) converted == v->value.real)
|
||||
if ((double) converted == v->value.real)
|
||||
return converted;
|
||||
REENABLE_WARNING;
|
||||
|
||||
log_debug("Real %Lg requested as integer, and cannot be converted losslessly, returning 0.", v->value.real);
|
||||
log_debug("Real %g requested as integer, and cannot be converted losslessly, returning 0.", v->value.real);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -942,7 +942,7 @@ mismatch:
|
|||
return 0;
|
||||
}
|
||||
|
||||
uintmax_t json_variant_unsigned(JsonVariant *v) {
|
||||
uint64_t json_variant_unsigned(JsonVariant *v) {
|
||||
if (!v)
|
||||
goto mismatch;
|
||||
if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||
|
||||
|
@ -958,7 +958,7 @@ uintmax_t json_variant_unsigned(JsonVariant *v) {
|
|||
|
||||
case JSON_VARIANT_INTEGER:
|
||||
if (v->value.integer >= 0)
|
||||
return (uintmax_t) v->value.integer;
|
||||
return (uint64_t) v->value.integer;
|
||||
|
||||
log_debug("Signed integer %ju requested as unsigned integer and out of range, returning 0.", v->value.integer);
|
||||
return 0;
|
||||
|
@ -967,16 +967,16 @@ uintmax_t json_variant_unsigned(JsonVariant *v) {
|
|||
return v->value.unsig;
|
||||
|
||||
case JSON_VARIANT_REAL: {
|
||||
uintmax_t converted;
|
||||
uint64_t converted;
|
||||
|
||||
converted = (uintmax_t) v->value.real;
|
||||
converted = (uint64_t) v->value.real;
|
||||
|
||||
DISABLE_WARNING_FLOAT_EQUAL;
|
||||
if ((long double) converted == v->value.real)
|
||||
if ((double) converted == v->value.real)
|
||||
return converted;
|
||||
REENABLE_WARNING;
|
||||
|
||||
log_debug("Real %Lg requested as unsigned integer, and cannot be converted losslessly, returning 0.", v->value.real);
|
||||
log_debug("Real %g requested as unsigned integer, and cannot be converted losslessly, returning 0.", v->value.real);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -989,7 +989,7 @@ mismatch:
|
|||
return 0;
|
||||
}
|
||||
|
||||
long double json_variant_real(JsonVariant *v) {
|
||||
double json_variant_real(JsonVariant *v) {
|
||||
if (!v)
|
||||
return 0.0;
|
||||
if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER ||
|
||||
|
@ -1007,11 +1007,9 @@ long double json_variant_real(JsonVariant *v) {
|
|||
return v->value.real;
|
||||
|
||||
case JSON_VARIANT_INTEGER: {
|
||||
long double converted;
|
||||
double converted = (double) v->value.integer;
|
||||
|
||||
converted = (long double) v->value.integer;
|
||||
|
||||
if ((intmax_t) converted == v->value.integer)
|
||||
if ((int64_t) converted == v->value.integer)
|
||||
return converted;
|
||||
|
||||
log_debug("Signed integer %ji requested as real, and cannot be converted losslessly, returning 0.", v->value.integer);
|
||||
|
@ -1019,11 +1017,9 @@ long double json_variant_real(JsonVariant *v) {
|
|||
}
|
||||
|
||||
case JSON_VARIANT_UNSIGNED: {
|
||||
long double converted;
|
||||
double converted = (double) v->value.unsig;
|
||||
|
||||
converted = (long double) v->value.unsig;
|
||||
|
||||
if ((uintmax_t) converted == v->value.unsig)
|
||||
if ((uint64_t) converted == v->value.unsig)
|
||||
return converted;
|
||||
|
||||
log_debug("Unsigned integer %ju requested as real, and cannot be converted losslessly, returning 0.", v->value.unsig);
|
||||
|
@ -1123,10 +1119,11 @@ JsonVariantType json_variant_type(JsonVariant *v) {
|
|||
return v->type;
|
||||
}
|
||||
|
||||
_function_no_sanitize_float_cast_overflow_ bool json_variant_has_type(JsonVariant *v, JsonVariantType type) {
|
||||
_function_no_sanitize_float_cast_overflow_
|
||||
bool json_variant_has_type(JsonVariant *v, JsonVariantType type) {
|
||||
JsonVariantType rt;
|
||||
|
||||
/* Note: we turn off ubsan float cast overflo detection for this function, since it would complain
|
||||
/* Note: we turn off ubsan float cast overflow detection for this function, since it would complain
|
||||
* about our float casts but we do them explicitly to detect conversion errors. */
|
||||
|
||||
v = json_variant_dereference(v);
|
||||
|
@ -1158,21 +1155,21 @@ _function_no_sanitize_float_cast_overflow_ bool json_variant_has_type(JsonVarian
|
|||
if (rt == JSON_VARIANT_INTEGER && type == JSON_VARIANT_UNSIGNED)
|
||||
return v->value.integer >= 0;
|
||||
if (rt == JSON_VARIANT_UNSIGNED && type == JSON_VARIANT_INTEGER)
|
||||
return v->value.unsig <= INTMAX_MAX;
|
||||
return v->value.unsig <= INT64_MAX;
|
||||
|
||||
/* Any integer that can be converted lossley to a real and back may also be considered a real */
|
||||
if (rt == JSON_VARIANT_INTEGER && type == JSON_VARIANT_REAL)
|
||||
return (intmax_t) (long double) v->value.integer == v->value.integer;
|
||||
return (int64_t) (double) v->value.integer == v->value.integer;
|
||||
if (rt == JSON_VARIANT_UNSIGNED && type == JSON_VARIANT_REAL)
|
||||
return (uintmax_t) (long double) v->value.unsig == v->value.unsig;
|
||||
return (uint64_t) (double) v->value.unsig == v->value.unsig;
|
||||
|
||||
DISABLE_WARNING_FLOAT_EQUAL;
|
||||
|
||||
/* Any real that can be converted losslessly to an integer and back may also be considered an integer */
|
||||
if (rt == JSON_VARIANT_REAL && type == JSON_VARIANT_INTEGER)
|
||||
return (long double) (intmax_t) v->value.real == v->value.real;
|
||||
return (double) (int64_t) v->value.real == v->value.real;
|
||||
if (rt == JSON_VARIANT_REAL && type == JSON_VARIANT_UNSIGNED)
|
||||
return (long double) (uintmax_t) v->value.real == v->value.real;
|
||||
return (double) (uint64_t) v->value.real == v->value.real;
|
||||
|
||||
REENABLE_WARNING;
|
||||
|
||||
|
@ -1576,7 +1573,7 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha
|
|||
if (flags & JSON_FORMAT_COLOR)
|
||||
fputs(ansi_highlight_blue(), f);
|
||||
|
||||
fprintf(f, "%.*Le", DECIMAL_DIG, json_variant_real(v));
|
||||
fprintf(f, "%.*e", DECIMAL_DIG, json_variant_real(v));
|
||||
|
||||
if (flags & JSON_FORMAT_COLOR)
|
||||
fputs(ANSI_NORMAL, f);
|
||||
|
@ -1964,7 +1961,7 @@ int json_variant_set_field_string(JsonVariant **v, const char *field, const char
|
|||
return json_variant_set_field(v, field, m);
|
||||
}
|
||||
|
||||
int json_variant_set_field_integer(JsonVariant **v, const char *field, intmax_t i) {
|
||||
int json_variant_set_field_integer(JsonVariant **v, const char *field, int64_t i) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *m = NULL;
|
||||
int r;
|
||||
|
||||
|
@ -1975,7 +1972,7 @@ int json_variant_set_field_integer(JsonVariant **v, const char *field, intmax_t
|
|||
return json_variant_set_field(v, field, m);
|
||||
}
|
||||
|
||||
int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uintmax_t u) {
|
||||
int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uint64_t u) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *m = NULL;
|
||||
int r;
|
||||
|
||||
|
@ -2192,19 +2189,19 @@ static int json_variant_copy(JsonVariant **nv, JsonVariant *v) {
|
|||
t = json_variant_type(v);
|
||||
switch (t) {
|
||||
case JSON_VARIANT_INTEGER:
|
||||
k = sizeof(intmax_t);
|
||||
k = sizeof(int64_t);
|
||||
value.integer = json_variant_integer(v);
|
||||
source = &value;
|
||||
break;
|
||||
|
||||
case JSON_VARIANT_UNSIGNED:
|
||||
k = sizeof(uintmax_t);
|
||||
k = sizeof(uint64_t);
|
||||
value.unsig = json_variant_unsigned(v);
|
||||
source = &value;
|
||||
break;
|
||||
|
||||
case JSON_VARIANT_REAL:
|
||||
k = sizeof(long double);
|
||||
k = sizeof(double);
|
||||
value.real = json_variant_real(v);
|
||||
source = &value;
|
||||
break;
|
||||
|
@ -2527,9 +2524,9 @@ static int json_parse_string(const char **p, char **ret) {
|
|||
|
||||
static int json_parse_number(const char **p, JsonValue *ret) {
|
||||
bool negative = false, exponent_negative = false, is_real = false;
|
||||
long double x = 0.0, y = 0.0, exponent = 0.0, shift = 1.0;
|
||||
intmax_t i = 0;
|
||||
uintmax_t u = 0;
|
||||
double x = 0.0, y = 0.0, exponent = 0.0, shift = 1.0;
|
||||
int64_t i = 0;
|
||||
uint64_t u = 0;
|
||||
const char *c;
|
||||
|
||||
assert(p);
|
||||
|
@ -2553,23 +2550,23 @@ static int json_parse_number(const char **p, JsonValue *ret) {
|
|||
if (!is_real) {
|
||||
if (negative) {
|
||||
|
||||
if (i < INTMAX_MIN / 10) /* overflow */
|
||||
if (i < INT64_MIN / 10) /* overflow */
|
||||
is_real = true;
|
||||
else {
|
||||
intmax_t t = 10 * i;
|
||||
int64_t t = 10 * i;
|
||||
|
||||
if (t < INTMAX_MIN + (*c - '0')) /* overflow */
|
||||
if (t < INT64_MIN + (*c - '0')) /* overflow */
|
||||
is_real = true;
|
||||
else
|
||||
i = t - (*c - '0');
|
||||
}
|
||||
} else {
|
||||
if (u > UINTMAX_MAX / 10) /* overflow */
|
||||
if (u > UINT64_MAX / 10) /* overflow */
|
||||
is_real = true;
|
||||
else {
|
||||
uintmax_t t = 10 * u;
|
||||
uint64_t t = 10 * u;
|
||||
|
||||
if (t > UINTMAX_MAX - (*c - '0')) /* overflow */
|
||||
if (t > UINT64_MAX - (*c - '0')) /* overflow */
|
||||
is_real = true;
|
||||
else
|
||||
u = t + (*c - '0');
|
||||
|
@ -2619,7 +2616,7 @@ static int json_parse_number(const char **p, JsonValue *ret) {
|
|||
*p = c;
|
||||
|
||||
if (is_real) {
|
||||
ret->real = ((negative ? -1.0 : 1.0) * (x + (y / shift))) * exp10l((exponent_negative ? -1.0 : 1.0) * exponent);
|
||||
ret->real = ((negative ? -1.0 : 1.0) * (x + (y / shift))) * exp10((exponent_negative ? -1.0 : 1.0) * exponent);
|
||||
return JSON_TOKEN_REAL;
|
||||
} else if (negative) {
|
||||
ret->integer = i;
|
||||
|
@ -3291,14 +3288,14 @@ int json_buildv(JsonVariant **ret, va_list ap) {
|
|||
}
|
||||
|
||||
case _JSON_BUILD_INTEGER: {
|
||||
intmax_t j;
|
||||
int64_t j;
|
||||
|
||||
if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) {
|
||||
r = -EINVAL;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
j = va_arg(ap, intmax_t);
|
||||
j = va_arg(ap, int64_t);
|
||||
|
||||
if (current->n_suppress == 0) {
|
||||
r = json_variant_new_integer(&add, j);
|
||||
|
@ -3319,14 +3316,14 @@ int json_buildv(JsonVariant **ret, va_list ap) {
|
|||
}
|
||||
|
||||
case _JSON_BUILD_UNSIGNED: {
|
||||
uintmax_t j;
|
||||
uint64_t j;
|
||||
|
||||
if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) {
|
||||
r = -EINVAL;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
j = va_arg(ap, uintmax_t);
|
||||
j = va_arg(ap, uint64_t);
|
||||
|
||||
if (current->n_suppress == 0) {
|
||||
r = json_variant_new_unsigned(&add, j);
|
||||
|
@ -3347,14 +3344,14 @@ int json_buildv(JsonVariant **ret, va_list ap) {
|
|||
}
|
||||
|
||||
case _JSON_BUILD_REAL: {
|
||||
long double d;
|
||||
double d;
|
||||
|
||||
if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) {
|
||||
r = -EINVAL;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
d = va_arg(ap, long double);
|
||||
d = va_arg(ap, double);
|
||||
|
||||
if (current->n_suppress == 0) {
|
||||
r = json_variant_new_real(&add, d);
|
||||
|
@ -4067,8 +4064,8 @@ int json_dispatch_tristate(const char *name, JsonVariant *variant, JsonDispatchF
|
|||
return 0;
|
||||
}
|
||||
|
||||
int json_dispatch_intmax(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
intmax_t *i = userdata;
|
||||
int json_dispatch_int64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
int64_t *i = userdata;
|
||||
|
||||
assert(variant);
|
||||
assert(i);
|
||||
|
@ -4080,8 +4077,8 @@ int json_dispatch_intmax(const char *name, JsonVariant *variant, JsonDispatchFla
|
|||
return 0;
|
||||
}
|
||||
|
||||
int json_dispatch_uintmax(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
uintmax_t *u = userdata;
|
||||
int json_dispatch_uint64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
uint64_t *u = userdata;
|
||||
|
||||
assert(variant);
|
||||
assert(u);
|
||||
|
@ -4231,7 +4228,7 @@ int json_dispatch_variant(const char *name, JsonVariant *variant, JsonDispatchFl
|
|||
|
||||
int json_dispatch_uid_gid(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
uid_t *uid = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
assert_cc(sizeof(uid_t) == sizeof(uint32_t));
|
||||
assert_cc(sizeof(gid_t) == sizeof(uint32_t));
|
||||
|
|
|
@ -29,10 +29,10 @@
|
|||
|
||||
Limitations:
|
||||
- Doesn't allow embedded NUL in strings
|
||||
- Can't store integers outside of the -9223372036854775808…18446744073709551615 range (it will use 'long double' for
|
||||
- Can't store integers outside of the -9223372036854775808…18446744073709551615 range (it will use 'double' for
|
||||
values outside this range, which is lossy)
|
||||
- Can't store negative zero (will be treated identical to positive zero, and not retained across serialization)
|
||||
- Can't store non-integer numbers that can't be stored in "long double" losslessly
|
||||
- Can't store non-integer numbers that can't be stored in "double" losslessly
|
||||
- Allows creation and parsing of objects with duplicate keys. The "dispatcher" will refuse them however. This means
|
||||
we can parse and pass around such objects, but will carefully refuse them when we convert them into our own data.
|
||||
|
||||
|
@ -59,9 +59,9 @@ typedef enum JsonVariantType {
|
|||
int json_variant_new_stringn(JsonVariant **ret, const char *s, size_t n);
|
||||
int json_variant_new_base64(JsonVariant **ret, const void *p, size_t n);
|
||||
int json_variant_new_hex(JsonVariant **ret, const void *p, size_t n);
|
||||
int json_variant_new_integer(JsonVariant **ret, intmax_t i);
|
||||
int json_variant_new_unsigned(JsonVariant **ret, uintmax_t u);
|
||||
int json_variant_new_real(JsonVariant **ret, long double d);
|
||||
int json_variant_new_integer(JsonVariant **ret, int64_t i);
|
||||
int json_variant_new_unsigned(JsonVariant **ret, uint64_t u);
|
||||
int json_variant_new_real(JsonVariant **ret, double d);
|
||||
int json_variant_new_boolean(JsonVariant **ret, bool b);
|
||||
int json_variant_new_array(JsonVariant **ret, JsonVariant **array, size_t n);
|
||||
int json_variant_new_array_bytes(JsonVariant **ret, const void *p, size_t n);
|
||||
|
@ -81,9 +81,9 @@ void json_variant_unref_many(JsonVariant **array, size_t n);
|
|||
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref);
|
||||
|
||||
const char *json_variant_string(JsonVariant *v);
|
||||
intmax_t json_variant_integer(JsonVariant *v);
|
||||
uintmax_t json_variant_unsigned(JsonVariant *v);
|
||||
long double json_variant_real(JsonVariant *v);
|
||||
int64_t json_variant_integer(JsonVariant *v);
|
||||
uint64_t json_variant_unsigned(JsonVariant *v);
|
||||
double json_variant_real(JsonVariant *v);
|
||||
bool json_variant_boolean(JsonVariant *v);
|
||||
|
||||
JsonVariantType json_variant_type(JsonVariant *v);
|
||||
|
@ -189,8 +189,8 @@ int json_variant_filter(JsonVariant **v, char **to_remove);
|
|||
|
||||
int json_variant_set_field(JsonVariant **v, const char *field, JsonVariant *value);
|
||||
int json_variant_set_field_string(JsonVariant **v, const char *field, const char *value);
|
||||
int json_variant_set_field_integer(JsonVariant **v, const char *field, intmax_t value);
|
||||
int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uintmax_t value);
|
||||
int json_variant_set_field_integer(JsonVariant **v, const char *field, int64_t value);
|
||||
int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uint64_t value);
|
||||
int json_variant_set_field_boolean(JsonVariant **v, const char *field, bool b);
|
||||
int json_variant_set_field_strv(JsonVariant **v, const char *field, char **l);
|
||||
|
||||
|
@ -240,9 +240,9 @@ enum {
|
|||
};
|
||||
|
||||
#define JSON_BUILD_STRING(s) _JSON_BUILD_STRING, (const char*) { s }
|
||||
#define JSON_BUILD_INTEGER(i) _JSON_BUILD_INTEGER, (intmax_t) { i }
|
||||
#define JSON_BUILD_UNSIGNED(u) _JSON_BUILD_UNSIGNED, (uintmax_t) { u }
|
||||
#define JSON_BUILD_REAL(d) _JSON_BUILD_REAL, (long double) { d }
|
||||
#define JSON_BUILD_INTEGER(i) _JSON_BUILD_INTEGER, (int64_t) { i }
|
||||
#define JSON_BUILD_UNSIGNED(u) _JSON_BUILD_UNSIGNED, (uint64_t) { u }
|
||||
#define JSON_BUILD_REAL(d) _JSON_BUILD_REAL, (double) { d }
|
||||
#define JSON_BUILD_BOOLEAN(b) _JSON_BUILD_BOOLEAN, (bool) { b }
|
||||
#define JSON_BUILD_ARRAY(...) _JSON_BUILD_ARRAY_BEGIN, __VA_ARGS__, _JSON_BUILD_ARRAY_END
|
||||
#define JSON_BUILD_EMPTY_ARRAY _JSON_BUILD_ARRAY_BEGIN, _JSON_BUILD_ARRAY_END
|
||||
|
@ -297,8 +297,8 @@ int json_dispatch_strv(const char *name, JsonVariant *variant, JsonDispatchFlags
|
|||
int json_dispatch_boolean(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
int json_dispatch_tristate(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
int json_dispatch_variant(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
int json_dispatch_intmax(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
int json_dispatch_uintmax(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
int json_dispatch_int64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
int json_dispatch_uint64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
int json_dispatch_uint32(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
int json_dispatch_int32(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
int json_dispatch_uid_gid(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
|
@ -306,12 +306,6 @@ int json_dispatch_user_group_name(const char *name, JsonVariant *variant, JsonDi
|
|||
int json_dispatch_id128(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
int json_dispatch_unsupported(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata);
|
||||
|
||||
assert_cc(sizeof(uintmax_t) == sizeof(uint64_t));
|
||||
#define json_dispatch_uint64 json_dispatch_uintmax
|
||||
|
||||
assert_cc(sizeof(intmax_t) == sizeof(int64_t));
|
||||
#define json_dispatch_int64 json_dispatch_intmax
|
||||
|
||||
assert_cc(sizeof(uint32_t) == sizeof(unsigned));
|
||||
#define json_dispatch_uint json_dispatch_uint32
|
||||
|
||||
|
|
|
@ -252,7 +252,7 @@ int json_dispatch_gecos(const char *name, JsonVariant *variant, JsonDispatchFlag
|
|||
|
||||
static int json_dispatch_nice(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
int *nl = userdata;
|
||||
intmax_t m;
|
||||
int64_t m;
|
||||
|
||||
if (json_variant_is_null(variant)) {
|
||||
*nl = INT_MAX;
|
||||
|
@ -276,10 +276,10 @@ static int json_dispatch_rlimit_value(const char *name, JsonVariant *variant, Js
|
|||
if (json_variant_is_null(variant))
|
||||
*ret = RLIM_INFINITY;
|
||||
else if (json_variant_is_unsigned(variant)) {
|
||||
uintmax_t w;
|
||||
uint64_t w;
|
||||
|
||||
w = json_variant_unsigned(variant);
|
||||
if (w == RLIM_INFINITY || (uintmax_t) w != json_variant_unsigned(variant))
|
||||
if (w == RLIM_INFINITY || (uint64_t) w != json_variant_unsigned(variant))
|
||||
return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "Resource limit value '%s' is out of range.", name);
|
||||
|
||||
*ret = (rlim_t) w;
|
||||
|
@ -453,7 +453,7 @@ static int json_dispatch_image_path(const char *name, JsonVariant *variant, Json
|
|||
|
||||
static int json_dispatch_umask(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
mode_t *m = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
if (json_variant_is_null(variant)) {
|
||||
*m = MODE_INVALID;
|
||||
|
@ -473,7 +473,7 @@ static int json_dispatch_umask(const char *name, JsonVariant *variant, JsonDispa
|
|||
|
||||
static int json_dispatch_access_mode(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
mode_t *m = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t k;
|
||||
|
||||
if (json_variant_is_null(variant)) {
|
||||
*m = MODE_INVALID;
|
||||
|
@ -564,8 +564,7 @@ static int json_dispatch_storage(const char *name, JsonVariant *variant, JsonDis
|
|||
}
|
||||
|
||||
static int json_dispatch_tasks_or_memory_max(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
uint64_t *limit = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t *limit = userdata, k;
|
||||
|
||||
if (json_variant_is_null(variant)) {
|
||||
*limit = UINT64_MAX;
|
||||
|
@ -584,8 +583,7 @@ static int json_dispatch_tasks_or_memory_max(const char *name, JsonVariant *vari
|
|||
}
|
||||
|
||||
static int json_dispatch_weight(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
||||
uint64_t *weight = userdata;
|
||||
uintmax_t k;
|
||||
uint64_t *weight = userdata, k;
|
||||
|
||||
if (json_variant_is_null(variant)) {
|
||||
*weight = UINT64_MAX;
|
||||
|
|
|
@ -46,28 +46,23 @@ static void test_tokenizer(const char *data, ...) {
|
|||
assert_se(streq_ptr(nn, str));
|
||||
|
||||
} else if (t == JSON_TOKEN_REAL) {
|
||||
long double d;
|
||||
double d;
|
||||
|
||||
d = va_arg(ap, long double);
|
||||
|
||||
/* Valgrind doesn't support long double calculations and automatically downgrades to 80bit:
|
||||
* http://www.valgrind.org/docs/manual/manual-core.html#manual-core.limits.
|
||||
* Some architectures might not support long double either.
|
||||
*/
|
||||
d = va_arg(ap, double);
|
||||
|
||||
assert_se(fabsl(d - v.real) < 1e-10 ||
|
||||
fabsl((d - v.real) / v.real) < 1e-10);
|
||||
|
||||
} else if (t == JSON_TOKEN_INTEGER) {
|
||||
intmax_t i;
|
||||
int64_t i;
|
||||
|
||||
i = va_arg(ap, intmax_t);
|
||||
i = va_arg(ap, int64_t);
|
||||
assert_se(i == v.integer);
|
||||
|
||||
} else if (t == JSON_TOKEN_UNSIGNED) {
|
||||
uintmax_t u;
|
||||
uint64_t u;
|
||||
|
||||
u = va_arg(ap, uintmax_t);
|
||||
u = va_arg(ap, uint64_t);
|
||||
assert_se(u == v.unsig);
|
||||
|
||||
} else if (t == JSON_TOKEN_BOOLEAN) {
|
||||
|
@ -523,17 +518,17 @@ static void test_bisect(void) {
|
|||
}
|
||||
|
||||
static void test_float_match(JsonVariant *v) {
|
||||
const long double delta = 0.0001;
|
||||
const double delta = 0.0001;
|
||||
|
||||
assert_se(json_variant_is_array(v));
|
||||
assert_se(json_variant_elements(v) == 9);
|
||||
assert_se(fabsl((long double) 1.0 - ((long double) DBL_MIN / json_variant_real(json_variant_by_index(v, 0)))) <= delta);
|
||||
assert_se(fabsl((long double) 1.0 - ((long double) DBL_MAX / json_variant_real(json_variant_by_index(v, 1)))) <= delta);
|
||||
assert_se(fabsl((double) 1.0 - ((double) DBL_MIN / json_variant_real(json_variant_by_index(v, 0)))) <= delta);
|
||||
assert_se(fabsl((double) 1.0 - ((double) DBL_MAX / json_variant_real(json_variant_by_index(v, 1)))) <= delta);
|
||||
assert_se(json_variant_is_null(json_variant_by_index(v, 2))); /* nan is not supported by json → null */
|
||||
assert_se(json_variant_is_null(json_variant_by_index(v, 3))); /* +inf is not supported by json → null */
|
||||
assert_se(json_variant_is_null(json_variant_by_index(v, 4))); /* -inf is not supported by json → null */
|
||||
assert_se(json_variant_is_null(json_variant_by_index(v, 5)) ||
|
||||
fabsl((long double) 1.0 - ((long double) HUGE_VAL / json_variant_real(json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */
|
||||
fabsl((double) 1.0 - ((double) HUGE_VAL / json_variant_real(json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */
|
||||
assert_se(json_variant_is_real(json_variant_by_index(v, 6)) &&
|
||||
json_variant_is_integer(json_variant_by_index(v, 6)) &&
|
||||
json_variant_integer(json_variant_by_index(v, 6)) == 0);
|
||||
|
@ -580,19 +575,19 @@ int main(int argc, char *argv[]) {
|
|||
test_tokenizer("x", -EINVAL);
|
||||
test_tokenizer("", JSON_TOKEN_END);
|
||||
test_tokenizer(" ", JSON_TOKEN_END);
|
||||
test_tokenizer("0", JSON_TOKEN_UNSIGNED, (uintmax_t) 0, JSON_TOKEN_END);
|
||||
test_tokenizer("-0", JSON_TOKEN_INTEGER, (intmax_t) 0, JSON_TOKEN_END);
|
||||
test_tokenizer("1234", JSON_TOKEN_UNSIGNED, (uintmax_t) 1234, JSON_TOKEN_END);
|
||||
test_tokenizer("-1234", JSON_TOKEN_INTEGER, (intmax_t) -1234, JSON_TOKEN_END);
|
||||
test_tokenizer("18446744073709551615", JSON_TOKEN_UNSIGNED, (uintmax_t) UINT64_MAX, JSON_TOKEN_END);
|
||||
test_tokenizer("-9223372036854775808", JSON_TOKEN_INTEGER, (intmax_t) INT64_MIN, JSON_TOKEN_END);
|
||||
test_tokenizer("18446744073709551616", JSON_TOKEN_REAL, (long double) 18446744073709551616.0L, JSON_TOKEN_END);
|
||||
test_tokenizer("-9223372036854775809", JSON_TOKEN_REAL, (long double) -9223372036854775809.0L, JSON_TOKEN_END);
|
||||
test_tokenizer("-1234", JSON_TOKEN_INTEGER, (intmax_t) -1234, JSON_TOKEN_END);
|
||||
test_tokenizer("3.141", JSON_TOKEN_REAL, (long double) 3.141, JSON_TOKEN_END);
|
||||
test_tokenizer("0.0", JSON_TOKEN_REAL, (long double) 0.0, JSON_TOKEN_END);
|
||||
test_tokenizer("7e3", JSON_TOKEN_REAL, (long double) 7e3, JSON_TOKEN_END);
|
||||
test_tokenizer("-7e-3", JSON_TOKEN_REAL, (long double) -7e-3, JSON_TOKEN_END);
|
||||
test_tokenizer("0", JSON_TOKEN_UNSIGNED, (uint64_t) 0, JSON_TOKEN_END);
|
||||
test_tokenizer("-0", JSON_TOKEN_INTEGER, (int64_t) 0, JSON_TOKEN_END);
|
||||
test_tokenizer("1234", JSON_TOKEN_UNSIGNED, (uint64_t) 1234, JSON_TOKEN_END);
|
||||
test_tokenizer("-1234", JSON_TOKEN_INTEGER, (int64_t) -1234, JSON_TOKEN_END);
|
||||
test_tokenizer("18446744073709551615", JSON_TOKEN_UNSIGNED, (uint64_t) UINT64_MAX, JSON_TOKEN_END);
|
||||
test_tokenizer("-9223372036854775808", JSON_TOKEN_INTEGER, (int64_t) INT64_MIN, JSON_TOKEN_END);
|
||||
test_tokenizer("18446744073709551616", JSON_TOKEN_REAL, (double) 18446744073709551616.0L, JSON_TOKEN_END);
|
||||
test_tokenizer("-9223372036854775809", JSON_TOKEN_REAL, (double) -9223372036854775809.0L, JSON_TOKEN_END);
|
||||
test_tokenizer("-1234", JSON_TOKEN_INTEGER, (int64_t) -1234, JSON_TOKEN_END);
|
||||
test_tokenizer("3.141", JSON_TOKEN_REAL, (double) 3.141, JSON_TOKEN_END);
|
||||
test_tokenizer("0.0", JSON_TOKEN_REAL, (double) 0.0, JSON_TOKEN_END);
|
||||
test_tokenizer("7e3", JSON_TOKEN_REAL, (double) 7e3, JSON_TOKEN_END);
|
||||
test_tokenizer("-7e-3", JSON_TOKEN_REAL, (double) -7e-3, JSON_TOKEN_END);
|
||||
test_tokenizer("true", JSON_TOKEN_BOOLEAN, true, JSON_TOKEN_END);
|
||||
test_tokenizer("false", JSON_TOKEN_BOOLEAN, false, JSON_TOKEN_END);
|
||||
test_tokenizer("null", JSON_TOKEN_NULL, JSON_TOKEN_END);
|
||||
|
@ -612,7 +607,7 @@ int main(int argc, char *argv[]) {
|
|||
test_tokenizer("\"\\udc00\\udc00\"", -EINVAL);
|
||||
test_tokenizer("\"\\ud801\\udc37\"", JSON_TOKEN_STRING, "\xf0\x90\x90\xb7", JSON_TOKEN_END);
|
||||
|
||||
test_tokenizer("[1, 2, -3]", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_UNSIGNED, (uintmax_t) 1, JSON_TOKEN_COMMA, JSON_TOKEN_UNSIGNED, (uintmax_t) 2, JSON_TOKEN_COMMA, JSON_TOKEN_INTEGER, (intmax_t) -3, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END);
|
||||
test_tokenizer("[1, 2, -3]", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_UNSIGNED, (uint64_t) 1, JSON_TOKEN_COMMA, JSON_TOKEN_UNSIGNED, (uint64_t) 2, JSON_TOKEN_COMMA, JSON_TOKEN_INTEGER, (int64_t) -3, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END);
|
||||
|
||||
test_variant("{\"k\": \"v\", \"foo\": [1, 2, 3], \"bar\": {\"zap\": null}}", test_1);
|
||||
test_variant("{\"mutant\": [1, null, \"1\", {\"1\": [1, \"1\"]}], \"thisisaverylongproperty\": 1.27}", test_2);
|
||||
|
|
|
@ -57,6 +57,8 @@ int main(void) {
|
|||
info(long long unsigned);
|
||||
info(__syscall_ulong_t);
|
||||
info(__syscall_slong_t);
|
||||
info(intmax_t);
|
||||
info(uintmax_t);
|
||||
|
||||
info(float);
|
||||
info(double);
|
||||
|
|
|
@ -26,7 +26,7 @@ static int block_write_fd = -1;
|
|||
static int method_something(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *ret = NULL;
|
||||
JsonVariant *a, *b;
|
||||
intmax_t x, y;
|
||||
int64_t x, y;
|
||||
int r;
|
||||
|
||||
a = json_variant_by_key(parameters, "a");
|
||||
|
|
Loading…
Reference in a new issue