Merge pull request #10378 from poettering/json-fuzz-fix

json: a comprehensive fix for oss-fuzz#10908
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2018-10-25 16:25:39 +02:00
committed by GitHub
6 changed files with 214 additions and 59 deletions
+35
View File
@@ -292,6 +292,7 @@ static void test_build(void) {
JSON_BUILD_STRING(NULL),
JSON_BUILD_NULL,
JSON_BUILD_INTEGER(77),
JSON_BUILD_ARRAY(JSON_BUILD_VARIANT(JSON_VARIANT_STRING_CONST("foobar")), JSON_BUILD_VARIANT(JSON_VARIANT_STRING_CONST("zzz"))),
JSON_BUILD_STRV(STRV_MAKE("one", "two", "three", "four")))) >= 0);
assert_se(json_variant_format(a, 0, &s) >= 0);
@@ -354,6 +355,38 @@ static void test_source(void) {
printf("--- pretty end ---\n");
}
static void test_depth(void) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
unsigned i;
int r;
v = JSON_VARIANT_STRING_CONST("start");
/* Let's verify that the maximum depth checks work */
for (i = 0;; i++) {
_cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
assert_se(i <= UINT16_MAX);
if (i & 1)
r = json_variant_new_array(&w, &v, 1);
else
r = json_variant_new_object(&w, (JsonVariant*[]) { JSON_VARIANT_STRING_CONST("key"), v }, 2);
if (r == -ELNRNG) {
log_info("max depth at %u", i);
break;
}
assert_se(r >= 0);
json_variant_unref(v);
v = TAKE_PTR(w);
}
json_variant_dump(v, 0, stdout, NULL);
fputs("\n", stdout);
}
int main(int argc, char *argv[]) {
log_set_max_level(LOG_DEBUG);
@@ -407,5 +440,7 @@ int main(int argc, char *argv[]) {
test_source();
test_depth();
return 0;
}