/src/Python-3.8.3/Python/asdl.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "Python.h" |
2 | | #include "asdl.h" |
3 | | |
4 | | asdl_seq * |
5 | | _Py_asdl_seq_new(Py_ssize_t size, PyArena *arena) |
6 | 344 | { |
7 | 344 | asdl_seq *seq = NULL; |
8 | 344 | size_t n; |
9 | | |
10 | | /* check size is sane */ |
11 | 344 | if (size < 0 || |
12 | 344 | (size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) { |
13 | 0 | PyErr_NoMemory(); |
14 | 0 | return NULL; |
15 | 0 | } |
16 | 344 | n = (size ? (sizeof(void *) * (size - 1)) : 0); |
17 | | |
18 | | /* check if size can be added safely */ |
19 | 344 | if (n > SIZE_MAX - sizeof(asdl_seq)) { |
20 | 0 | PyErr_NoMemory(); |
21 | 0 | return NULL; |
22 | 0 | } |
23 | 344 | n += sizeof(asdl_seq); |
24 | | |
25 | 344 | seq = (asdl_seq *)PyArena_Malloc(arena, n); |
26 | 344 | if (!seq) { |
27 | 0 | PyErr_NoMemory(); |
28 | 0 | return NULL; |
29 | 0 | } |
30 | 344 | memset(seq, 0, n); |
31 | 344 | seq->size = size; |
32 | 344 | return seq; |
33 | 344 | } |
34 | | |
35 | | asdl_int_seq * |
36 | | _Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena) |
37 | 24 | { |
38 | 24 | asdl_int_seq *seq = NULL; |
39 | 24 | size_t n; |
40 | | |
41 | | /* check size is sane */ |
42 | 24 | if (size < 0 || |
43 | 24 | (size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) { |
44 | 0 | PyErr_NoMemory(); |
45 | 0 | return NULL; |
46 | 0 | } |
47 | 24 | n = (size ? (sizeof(void *) * (size - 1)) : 0); |
48 | | |
49 | | /* check if size can be added safely */ |
50 | 24 | if (n > SIZE_MAX - sizeof(asdl_seq)) { |
51 | 0 | PyErr_NoMemory(); |
52 | 0 | return NULL; |
53 | 0 | } |
54 | 24 | n += sizeof(asdl_seq); |
55 | | |
56 | 24 | seq = (asdl_int_seq *)PyArena_Malloc(arena, n); |
57 | 24 | if (!seq) { |
58 | 0 | PyErr_NoMemory(); |
59 | 0 | return NULL; |
60 | 0 | } |
61 | 24 | memset(seq, 0, n); |
62 | 24 | seq->size = size; |
63 | 24 | return seq; |
64 | 24 | } |