xref: /freebsd/contrib/libcbor/test/stack_over_limit_test.c (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
110ff414cSEd Maste #include "assertions.h"
210ff414cSEd Maste #include "cbor.h"
310ff414cSEd Maste 
generate_overflow_data(unsigned char ** overflow_data)410ff414cSEd Maste static size_t generate_overflow_data(unsigned char **overflow_data) {
510ff414cSEd Maste   int i;
610ff414cSEd Maste   *overflow_data = (unsigned char *)malloc(CBOR_MAX_STACK_SIZE + 3);
710ff414cSEd Maste   for (i = 0; i < CBOR_MAX_STACK_SIZE + 1; i++) {
810ff414cSEd Maste     (*overflow_data)[i] = 0xC2;  // tag of positive bignum
910ff414cSEd Maste   }
1010ff414cSEd Maste   (*overflow_data)[CBOR_MAX_STACK_SIZE + 1] = 0x41;  // bytestring of length 1
1110ff414cSEd Maste   (*overflow_data)[CBOR_MAX_STACK_SIZE + 2] = 0x01;  // a bignum of value 1
1210ff414cSEd Maste   return CBOR_MAX_STACK_SIZE + 3;
1310ff414cSEd Maste }
1410ff414cSEd Maste 
test_stack_over_limit(void ** _CBOR_UNUSED (_state))15*5d3e7166SEd Maste static void test_stack_over_limit(void **_CBOR_UNUSED(_state)) {
1610ff414cSEd Maste   unsigned char *overflow_data;
1710ff414cSEd Maste   size_t overflow_data_len;
1810ff414cSEd Maste   struct cbor_load_result res;
1910ff414cSEd Maste   overflow_data_len = generate_overflow_data(&overflow_data);
20*5d3e7166SEd Maste   assert_null(cbor_load(overflow_data, overflow_data_len, &res));
2110ff414cSEd Maste   free(overflow_data);
22*5d3e7166SEd Maste   assert_size_equal(res.error.code, CBOR_ERR_MEMERROR);
2310ff414cSEd Maste }
2410ff414cSEd Maste 
main(void)25*5d3e7166SEd Maste int main(void) {
2610ff414cSEd Maste   const struct CMUnitTest tests[] = {cmocka_unit_test(test_stack_over_limit)};
2710ff414cSEd Maste   return cmocka_run_group_tests(tests, NULL, NULL);
2810ff414cSEd Maste }
29