[Opensrf-commits] r1496 - trunk/src/libopensrf

svn at svn.open-ils.org svn at svn.open-ils.org
Sun Nov 16 22:17:52 EST 2008


Author: erickson
Date: 2008-11-16 22:17:50 -0500 (Sun, 16 Nov 2008)
New Revision: 1496

Modified:
   trunk/src/libopensrf/utils.c
Log:

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.




Modified: trunk/src/libopensrf/utils.c
===================================================================
--- trunk/src/libopensrf/utils.c	2008-11-17 02:58:32 UTC (rev 1495)
+++ trunk/src/libopensrf/utils.c	2008-11-17 03:17:50 UTC (rev 1496)
@@ -242,7 +242,7 @@
 			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_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 @@
 
 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;



More information about the opensrf-commits mailing list