# HG changeset patch # User Paul Boddie # Date 1479851070 -3600 # Node ID a1890e60bb0dff540bb43309bd8a2a8f089edf71 # Parent 47ab3fff828ba29885dc6d1334661f26951d28a6 Made buffer initialisation more robust. Fixed list growth from empty and buffer serialisation. diff -r 47ab3fff828b -r a1890e60bb0d lib/__builtins__/buffer.py --- a/lib/__builtins__/buffer.py Tue Nov 22 17:36:39 2016 +0100 +++ b/lib/__builtins__/buffer.py Tue Nov 22 22:44:30 2016 +0100 @@ -25,11 +25,18 @@ "A buffer, used to build strings." - def __init__(self, args=None, size=None): + def __init__(self, args=None, size=0): "Initialise a buffer from the given 'args' or the given 'size'." - self.__data__ = _list_init(len(args) or size or 0) + if args is not None: + n = len(args) + elif isinstance(size, int): + n = size + else: + raise TypeError(size) + + self.__data__ = _list_init(n) # Append all arguments in string form to the buffer. diff -r 47ab3fff828b -r a1890e60bb0d templates/native.c --- a/templates/native.c Tue Nov 22 17:36:39 2016 +0100 +++ b/templates/native.c Tue Nov 22 22:44:30 2016 +0100 @@ -1,7 +1,7 @@ #include /* calloc, exit, realloc */ #include /* read, write */ #include /* ceil, log10, pow */ -#include /* strcmp, strlen */ +#include /* strcmp, strncpy, strlen */ #include /* snprintf */ #include "types.h" #include "exceptions.h" @@ -362,7 +362,7 @@ if (size >= capacity) { /* NOTE: Consider various restrictions on capacity increases. */ - n = data->capacity * 2; + n = capacity ? capacity * 2 : 1; data = realloc(data, __FRAGMENT_SIZE(n)); data->capacity = n; } @@ -441,8 +441,8 @@ __attr * const self = &__args[1]; /* self.__data__ interpreted as buffer */ __fragment *data = __load_via_object(self->value, __pos___data__).data; - unsigned int size = 0, i, j; - char *s; + unsigned int size = 0, i, j, n; + char *s, *o; /* Calculate the size of the string. */ for (i = 0; i < data->size; i++) @@ -452,8 +452,13 @@ s = calloc(size + 1, sizeof(char)); /* Build a single string from the buffer contents. */ - for (i = 0, j = 0; i < data->size; j += strlen(data->attrs[i].strvalue), i++) - strcpy(s + j, data->attrs[i].strvalue); + for (i = 0, j = 0; i < data->size; i++) + { + o = __load_via_object(data->attrs[i].value, __pos___data__).strvalue; + n = strlen(o); + strncpy(s + j, o, n); + j += n; + } /* Return a new string. */ return __new_str(s);