From c5904a8e5afdcc24dd9d400c970ddb6db626bbfe Mon Sep 17 00:00:00 2001 From: erickson Date: Mon, 17 Nov 2008 03:17:50 +0000 Subject: [PATCH] Patch from Scott McKellar: This patch is mostly a couple of tweaks to the growing_buffer code, loosely related to my previous patch to utils.h. There is also a small tweak to uescape(). 1. in buffer_add() I replaced strcat() with strcpy() for appending the new string. Since we already know where the end of the old string is, we don't need to ask strcat() to find it for us. 2. In buffer_reset(), the old code contains the following: osrf_clearbuf( gb->buf, sizeof(gb->buf) ); The evident intent is to clear the buffer. However sizeof(gb->buf) is not the size of the buffer, it's the size of the pointer to the buffer. We were clearing only the first four bytes or so. I changed the line to: osrf_clearbuf( gb->buf, gb->size ); 3. Also in buffer_reset(), I added a line to populate the first byte of the buffer with a nul, to ensure that the length of the (empty) string matches the n_used member. 4. In uescape(), we were examining the contents of string[] without first verifying that string was not NULL. The result would be undefined behavior if string were ever NULL. I added a couple of lines to treat a NULL pointer as if it were a pointer to an empty string. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1496 9efc2488-bf62-4759-914b-345cdb29e865 --- src/libopensrf/utils.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libopensrf/utils.c b/src/libopensrf/utils.c index 4a22b02..778afab 100644 --- a/src/libopensrf/utils.c +++ b/src/libopensrf/utils.c @@ -242,7 +242,7 @@ int buffer_add(growing_buffer* gb, const char* data) { return -1; } - strcat( gb->buf, data ); + strcpy( gb->buf + gb->n_used, data ); gb->n_used = total_len; return total_len; } @@ -251,8 +251,9 @@ int buffer_add(growing_buffer* gb, const char* data) { int buffer_reset( growing_buffer *gb){ if( gb == NULL ) { return -1; } if( gb->buf == NULL ) { return -1; } - osrf_clearbuf( gb->buf, sizeof(gb->buf) ); + osrf_clearbuf( gb->buf, gb->size ); gb->n_used = 0; + gb->buf[ 0 ] = '\0'; return gb->n_used; } @@ -322,6 +323,9 @@ int buffer_add_char(growing_buffer* gb, char c ) { char* uescape( const char* string, int size, int full_escape ) { + if( NULL == string ) + return NULL; + growing_buffer* buf = buffer_init(size + 64); int clen = 0; int idx = 0; -- 2.43.2