xref: /freebsd/contrib/libcbor/test/stack_over_limit_test.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 #include "assertions.h"
2 #include "cbor.h"
3 
4 static size_t generate_overflow_data(unsigned char **overflow_data) {
5   int i;
6   *overflow_data = (unsigned char *)malloc(CBOR_MAX_STACK_SIZE + 3);
7   for (i = 0; i < CBOR_MAX_STACK_SIZE + 1; i++) {
8     (*overflow_data)[i] = 0xC2;  // tag of positive bignum
9   }
10   (*overflow_data)[CBOR_MAX_STACK_SIZE + 1] = 0x41;  // bytestring of length 1
11   (*overflow_data)[CBOR_MAX_STACK_SIZE + 2] = 0x01;  // a bignum of value 1
12   return CBOR_MAX_STACK_SIZE + 3;
13 }
14 
15 static void test_stack_over_limit(void **_CBOR_UNUSED(_state)) {
16   unsigned char *overflow_data;
17   size_t overflow_data_len;
18   struct cbor_load_result res;
19   overflow_data_len = generate_overflow_data(&overflow_data);
20   assert_null(cbor_load(overflow_data, overflow_data_len, &res));
21   free(overflow_data);
22   assert_size_equal(res.error.code, CBOR_ERR_MEMERROR);
23 }
24 
25 int main(void) {
26   const struct CMUnitTest tests[] = {cmocka_unit_test(test_stack_over_limit)};
27   return cmocka_run_group_tests(tests, NULL, NULL);
28 }
29