blob: c21107811813af96ca21a15ae508b0ae39bf9a6e [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001#include "Python.h"
2#include "asdl.h"
3
4asdl_seq *
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005_Py_asdl_seq_new(Py_ssize_t size, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007 asdl_seq *seq = NULL;
Victor Stinner049e5092014-08-17 22:20:00 +02008 size_t n;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000010 /* check size is sane */
Victor Stinner049e5092014-08-17 22:20:00 +020011 if (size < 0 ||
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -070012 (size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000013 PyErr_NoMemory();
14 return NULL;
15 }
Victor Stinner049e5092014-08-17 22:20:00 +020016 n = (size ? (sizeof(void *) * (size - 1)) : 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 /* check if size can be added safely */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -070019 if (n > SIZE_MAX - sizeof(asdl_seq)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 PyErr_NoMemory();
21 return NULL;
22 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 n += sizeof(asdl_seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 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 Wouters49fd7fa2006-04-21 10:40:58 +000033}
34
35asdl_int_seq *
Antoine Pitroud01d396e2013-10-12 22:52:43 +020036_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 asdl_int_seq *seq = NULL;
Victor Stinner049e5092014-08-17 22:20:00 +020039 size_t n;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 /* check size is sane */
Victor Stinner049e5092014-08-17 22:20:00 +020042 if (size < 0 ||
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -070043 (size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 PyErr_NoMemory();
45 return NULL;
46 }
Victor Stinner049e5092014-08-17 22:20:00 +020047 n = (size ? (sizeof(void *) * (size - 1)) : 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 /* check if size can be added safely */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -070050 if (n > SIZE_MAX - sizeof(asdl_seq)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 PyErr_NoMemory();
52 return NULL;
53 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 n += sizeof(asdl_seq);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 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 Hylton3e0055f2005-10-20 19:59:25 +000064}