Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | #include "Python.h" |
2 | #include "asdl.h" | ||||
3 | |||||
4 | asdl_seq * | ||||
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 5 | _Py_asdl_seq_new(Py_ssize_t size, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7 | asdl_seq *seq = NULL; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 8 | size_t n; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 9 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 10 | /* check size is sane */ |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 11 | if (size < 0 || |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 12 | (size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 13 | PyErr_NoMemory(); |
14 | return NULL; | ||||
15 | } | ||||
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 16 | n = (size ? (sizeof(void *) * (size - 1)) : 0); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 17 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | /* check if size can be added safely */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 19 | if (n > SIZE_MAX - sizeof(asdl_seq)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | PyErr_NoMemory(); |
21 | return NULL; | ||||
22 | } | ||||
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 23 | n += sizeof(asdl_seq); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 24 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | seq = (asdl_seq *)PyArena_Malloc(arena, n); |
26 | if (!seq) { | ||||
27 | PyErr_NoMemory(); | ||||
28 | return NULL; | ||||
29 | } | ||||
30 | memset(seq, 0, n); | ||||
31 | seq->size = size; | ||||
32 | return seq; | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 33 | } |
34 | |||||
35 | asdl_int_seq * | ||||
Antoine Pitrou | d01d396e | 2013-10-12 22:52:43 +0200 | [diff] [blame] | 36 | _Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | asdl_int_seq *seq = NULL; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 39 | size_t n; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 40 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | /* check size is sane */ |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 42 | if (size < 0 || |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 43 | (size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | PyErr_NoMemory(); |
45 | return NULL; | ||||
46 | } | ||||
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 47 | n = (size ? (sizeof(void *) * (size - 1)) : 0); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 48 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | /* check if size can be added safely */ |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 50 | if (n > SIZE_MAX - sizeof(asdl_seq)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | PyErr_NoMemory(); |
52 | return NULL; | ||||
53 | } | ||||
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | n += sizeof(asdl_seq); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 55 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | seq = (asdl_int_seq *)PyArena_Malloc(arena, n); |
57 | if (!seq) { | ||||
58 | PyErr_NoMemory(); | ||||
59 | return NULL; | ||||
60 | } | ||||
61 | memset(seq, 0, n); | ||||
62 | seq->size = size; | ||||
63 | return seq; | ||||
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 64 | } |