File: stack_over_limit_test.c

package info (click to toggle)
mysql-8.0 8.0.43-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,904 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,184; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,196; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (28 lines) | stat: -rw-r--r-- 1,012 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "assertions.h"
#include "cbor.h"

static size_t generate_overflow_data(unsigned char **overflow_data) {
  int i;
  *overflow_data = (unsigned char *)malloc(CBOR_MAX_STACK_SIZE + 3);
  for (i = 0; i < CBOR_MAX_STACK_SIZE + 1; i++) {
    (*overflow_data)[i] = 0xC2;  // tag of positive bignum
  }
  (*overflow_data)[CBOR_MAX_STACK_SIZE + 1] = 0x41;  // bytestring of length 1
  (*overflow_data)[CBOR_MAX_STACK_SIZE + 2] = 0x01;  // a bignum of value 1
  return CBOR_MAX_STACK_SIZE + 3;
}

static void test_stack_over_limit(void **_CBOR_UNUSED(_state)) {
  unsigned char *overflow_data;
  size_t overflow_data_len;
  struct cbor_load_result res;
  overflow_data_len = generate_overflow_data(&overflow_data);
  assert_null(cbor_load(overflow_data, overflow_data_len, &res));
  free(overflow_data);
  assert_size_equal(res.error.code, CBOR_ERR_MEMERROR);
}

int main(void) {
  const struct CMUnitTest tests[] = {cmocka_unit_test(test_stack_over_limit)};
  return cmocka_run_group_tests(tests, NULL, NULL);
}