From c1f787f89431628753a412ab3faf29702ef4553d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 16 Oct 2015 18:48:27 +0200 Subject: improve and fix bprintf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/stdio/printf.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/stdio/printf.c b/src/stdio/printf.c index 7e89378..ac7b8f9 100644 --- a/src/stdio/printf.c +++ b/src/stdio/printf.c @@ -76,9 +76,9 @@ struct buffer } buf; /** - * The size of the buffer, in number of elements. + * Pointer to the size of the buffer, in number of elements. */ - size_t size; + size_t* size; /** * The write offset. @@ -86,7 +86,8 @@ struct buffer size_t off; /** - * Whether `secure_realloc` shall be used. + * Whether `EXTALLOC_CLEAR`, `secure_realloc`, + * or `secure_free` shall be used. */ int secure; @@ -237,11 +238,17 @@ static int wwrite_stream(const wchar_t* text, size_t length, FILE* stream) */ static int write_buffer(const char* text, size_t length, struct buffer* buffer) { + enum extalloc_mode flags = EXTALLOC_MALLOC | (buffer->secure ? EXTALLOC_CLEAR : 0); char* new; - if (buffer->off + length > buffer->size) + + if (buffer->off + length > *(buffer->size)) { - new = (buffer->secure ? secure_realloc : fast_realloc) - (buffer->buf.str, buffer->size * sizeof(char)); + if (buffer->off || !*(buffer->size)) + new = (buffer->secure ? secure_realloc : fast_realloc) + (buffer->buf.str, *(buffer->size) * sizeof(char)); + else + new = extalloc(buffer->buf.str, *(buffer->size) * sizeof(char), flags); + if (new == NULL) { if (buffer->free_on_error) @@ -249,7 +256,7 @@ static int write_buffer(const char* text, size_t length, struct buffer* buffer) buffer->buf.str = NULL; return -1; } - buffer->size = buffer->off + length; + *(buffer->size) = buffer->off + length; buffer->buf.str = new; } memcpy(buffer->buf.str, text, length); @@ -268,11 +275,17 @@ static int write_buffer(const char* text, size_t length, struct buffer* buffer) */ static int wwrite_buffer(const wchar_t* text, size_t length, struct buffer* buffer) { + enum extalloc_mode flags = EXTALLOC_MALLOC | (buffer->secure ? EXTALLOC_CLEAR : 0); wchar_t* new; + if (buffer->off + length > buffer->size) { - new = (buffer->secure ? secure_realloc : fast_realloc) - (buffer->buf.wcs, buffer->size * sizeof(wchar_t)); + if (buffer->off || !*(buffer->size)) + new = (buffer->secure ? secure_realloc : fast_realloc) + (buffer->buf.wcs, *(buffer->size) * sizeof(wchar_t)); + else + new = extalloc(buffer->buf.wcs, *(buffer->size) * sizeof(wchar_t), flags); + if (new == NULL) { if (buffer->free_on_error) @@ -280,7 +293,7 @@ static int wwrite_buffer(const wchar_t* text, size_t length, struct buffer* buff buffer->buf.wcs = NULL; return -1; } - buffer->size = buffer->off + length; + *(buffer->size) = buffer->off + length; buffer->buf.wcs = new; } wmemcpy(buffer->buf.wcs, text, length); @@ -764,7 +777,7 @@ int vbprintf(char** restrict buffer, size_t* restrict size, size_t offset, struct buffer buf = { .buf.str = *buffer, - .size = *size, + .size = size, .off = offset, .secure = secure, .free_on_error = buffer == NULL, @@ -1199,7 +1212,7 @@ int vbwprintf(wchar_t** restrict buffer, size_t* restrict size, size_t offset, struct buffer buf = { .buf.wcs = *buffer, - .size = *size, + .size = size, .off = offset, .secure = secure, .free_on_error = buffer == NULL, -- cgit v1.2.3-70-g09d2