deal with out of memory situations for realloc

svn:r429
This commit is contained in:
Niels Provos 2007-09-15 00:53:47 +00:00
parent 82153e6e92
commit 640c61bac9

View File

@ -931,20 +931,26 @@ class EntryArray(Entry):
'{',
' msg->%s_length++;' % name,
' if (msg->%s_length >= msg->%s_num_allocated) { ' % (name, name),
' if (!msg->%s_num_allocated) ' % name,
' msg->%s_num_allocated = 1; ' % name,
' struct %s** new_data = NULL; ' % self._refname,
' int tobe_allocated = msg->%s_num_allocated; ' % name,
' if (!tobe_allocated) ',
' tobe_allocated = 1; ',
' else ',
' msg->%s_num_allocated <<= 1; ' % name,
' msg->%s_data = (struct %s**)realloc(msg->%s_data, '
' msg->%s_num_allocated * sizeof(struct %s*));' % (
name, self._refname, name, name, self._refname ),
' tobe_allocated <<= 1; ',
' new_data = (struct %s**)realloc(msg->%s_data, '
'tobe_allocated * sizeof(struct %s*));' % (
self._refname, name, self._refname ),
' if (new_data == NULL) {',
' --msg->%s_length;' % name,
' return (NULL);',
' }',
' msg->%s_data = new_data;' % name,
' msg->%s_num_allocated = tobe_allocated;' % name,
' }',
' if (msg->%s_data == NULL)' % name,
' return (NULL);',
' msg->%s_data[msg->%s_length - 1] = %s_new();' % (
name, name, self._refname),
' if (msg->%s_data[msg->%s_length - 1] == NULL) {' % (name, name),
' msg->%s_length--; ' % name,
' --msg->%s_length; ' % name,
' return (NULL);',
' }',
' msg->%s_set = 1;' % name,