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