|
13 | 13 | #include <common-json-private.h>
|
14 | 14 | #include <bson/bson-iso8601-private.h>
|
15 | 15 | #include <bson/bson-json-private.h>
|
| 16 | +#include "test-libmongoc.h" // skip_if_no_large_allocations |
16 | 17 |
|
17 | 18 | static ssize_t
|
18 | 19 | test_bson_json_read_cb_helper (void *string, uint8_t *buf, size_t len)
|
@@ -3793,6 +3794,42 @@ test_bson_as_json_all_formats (void)
|
3793 | 3794 | }
|
3794 | 3795 | }
|
3795 | 3796 |
|
| 3797 | +static void |
| 3798 | +test_json_big_key (void *unused) |
| 3799 | +{ |
| 3800 | + BSON_UNUSED (unused); |
| 3801 | + |
| 3802 | + // Create a JSON string like: { "<'a' repeated INT_MAX + 1 times>": 123 } |
| 3803 | + size_t keylen = INT_MAX + 1u; |
| 3804 | + size_t json_len = strlen ("{\"") + keylen + strlen ("\":123}"); |
| 3805 | + char *json = bson_malloc (json_len + 1); |
| 3806 | + json[json_len] = '\0'; |
| 3807 | + |
| 3808 | + size_t offset = 0; |
| 3809 | + // Write {" |
| 3810 | + { |
| 3811 | + bson_strncpy (json, "{\"", json_len + 1); |
| 3812 | + offset += strlen ("{\""); |
| 3813 | + } |
| 3814 | + // Write key of repeated a |
| 3815 | + { |
| 3816 | + for (size_t i = 0; i < keylen; i++) { |
| 3817 | + json[offset++] = 'a'; |
| 3818 | + } |
| 3819 | + } |
| 3820 | + // Write remaining ":123} |
| 3821 | + { |
| 3822 | + bson_strncpy (json + offset, "\":123}", json_len + 1 - offset); |
| 3823 | + offset += strlen ("\":123}"); |
| 3824 | + } |
| 3825 | + |
| 3826 | + bson_error_t error; |
| 3827 | + bson_t b; |
| 3828 | + bool ok = bson_init_from_json (&b, json, (ssize_t) json_len, &error); |
| 3829 | + ASSERT (!ok); |
| 3830 | + ASSERT_ERROR_CONTAINS (error, BSON_ERROR_JSON, BSON_JSON_ERROR_READ_INVALID_PARAM, "too large"); |
| 3831 | + bson_free (json); |
| 3832 | +} |
3796 | 3833 |
|
3797 | 3834 | void
|
3798 | 3835 | test_json_install (TestSuite *suite)
|
@@ -3899,4 +3936,5 @@ test_json_install (TestSuite *suite)
|
3899 | 3936 | TestSuite_Add (suite, "/bson/parse_array", test_parse_array);
|
3900 | 3937 | TestSuite_Add (suite, "/bson/decimal128_overflowing_exponent", test_decimal128_overflowing_exponent);
|
3901 | 3938 | TestSuite_Add (suite, "/bson/as_json/all_formats", test_bson_as_json_all_formats);
|
| 3939 | + TestSuite_AddFull (suite, "/bson/json/big_key", test_json_big_key, NULL, NULL, skip_if_no_large_allocations); |
3902 | 3940 | }
|
0 commit comments