From: Charlie Brej Date: Wed, 17 Dec 2008 12:07:57 +0000 (+0000) Subject: Ensure ply-buffer data block always terminates with a '\0' X-Git-Tag: 0.7.0~237 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=66edc73d260e3fadca5658cfe77331aab0074916;p=thirdparty%2Fplymouth.git Ensure ply-buffer data block always terminates with a '\0' In some instances, ply-buffer did not terminate the data block with a '\0'. Doing so allows the data to be used as a string. Additionally it now tries harder to deal with very long additions to the buffer by repeatedly expanding the capacity and dealing with appends larger than the maximum buffer size. --- diff --git a/src/libply/ply-buffer.c b/src/libply/ply-buffer.c index 052195c8..1a2a54b0 100644 --- a/src/libply/ply-buffer.c +++ b/src/libply/ply-buffer.c @@ -83,6 +83,7 @@ ply_buffer_remove_bytes (ply_buffer_t *buffer, buffer->size - bytes_to_remove); buffer->size -= bytes_to_remove; } + buffer->data[buffer->size] = '\0'; } void @@ -94,6 +95,7 @@ ply_buffer_remove_bytes_at_end (ply_buffer_t *buffer, bytes_to_remove = MIN (buffer->size, bytes_to_remove); buffer->size -= bytes_to_remove; + buffer->data[buffer->size] = '\0'; } ply_buffer_t * @@ -176,22 +178,25 @@ ply_buffer_append_with_non_literal_format_string (ply_buffer_t *buffer, void ply_buffer_append_bytes (ply_buffer_t *buffer, - const void *bytes, + const void *bytes_in, size_t length) { assert (buffer != NULL); - assert (bytes != NULL); + assert (bytes_in != NULL); assert (length != 0); - - if ((buffer->size + length) >= buffer->capacity) + + const uint8_t *bytes = bytes_in; + + if (length >PLY_BUFFER_MAX_BUFFER_CAPACITY){ + bytes += length - (PLY_BUFFER_MAX_BUFFER_CAPACITY-1); + length = (PLY_BUFFER_MAX_BUFFER_CAPACITY-1); + } + + while ((buffer->size + length) >= buffer->capacity) { if (!ply_buffer_increase_capacity (buffer)) { ply_buffer_remove_bytes (buffer, length); - - if ((buffer->size + length) >= buffer->capacity) - if (!ply_buffer_increase_capacity (buffer)) - return; } } @@ -201,6 +206,7 @@ ply_buffer_append_bytes (ply_buffer_t *buffer, bytes, length); buffer->size += length; + buffer->data[buffer->size] = '\0'; } void