Bugfixes and additional comments.
authorcrh <crh@ubiqx.org>
Wed, 20 Jun 2012 16:45:36 +0000 (11:45 -0500)
committercrh <crh@ubiqx.org>
Wed, 20 Jun 2012 16:45:36 +0000 (11:45 -0500)
 * Fixed a crashbug in printGstr().
   * The local Gstr used in printGstr() to compose the output was not being initialized.
   * Added additional checks to make sure faied Gstr initializations are caught.
 * Added another test to isemptyGstr().
   * Checking the Gstr.len in addition to .bufr and .bufr[0].
   * Added comments.

src/daemon/Gstr.c
src/daemon/Gstr.h

index 73fc5f42c26006f79933418de922bdd92646393a..78dba4fd2e096fbaaaa833525dcdd8e2227ae341 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Email: crh@ubiqx.mn.org
  *
- * $Id: Gstr.c 2012-06-12 21:46:10 -0500 crh$
+ * $Id: Gstr.c 2012-06-20 11:45:36 -0500 crh$
  *
  * -------------------------------------------------------------------------- **
  *
@@ -262,10 +262,13 @@ int printGstr( Gstr *gs, const char *fmt, ... )
   Gstr    tmp[1];
   va_list ap;
 
-  /* Make sure we have a buffer in our Gstr.
+  /* Make sure we have buffers in our Gstrs.
    */
   if( NULL == gs->bufr )
-    initGstr( gs );
+    if( NULL == initGstr( gs ) )
+      return( -1 );
+  if( NULL == initGstr( tmp ) )
+    return( -1 );
 
   /* It may take two tries to format the string.
    * If the first try returns a length that is greater than our buffer size,
@@ -275,6 +278,7 @@ int printGstr( Gstr *gs, const char *fmt, ... )
   tmp->len = 1 + vsnprintf( tmp->bufr, tmp->bSize, fmt, ap );
   va_end( ap );
 
+  /* If our buffer is too small...  */
   if( (tmp->len) > (tmp->bSize) )
     {
     if( growGstr( tmp, tmp->len ) < 0 )
@@ -305,10 +309,16 @@ bool isemptyGstr( Gstr *gs )
    *
    *  Output: True if the string is empty, else false.
    *
+   *  Notes:  There are three factors to consider when determining whether
+   *          the string is empty.
+   *          + If the string doesn't exist (is NULL), then it's empty.
+   *          + If the string exists, but the first byte is '\0', it's empty.
+   *          + If the Gstr length is zero, the string is empty.
+   *
    * ------------------------------------------------------------------------ **
    */
   {
-  if( (NULL != gs->bufr) && ( '\0' != *(gs->bufr) ) )
+  if( (NULL != gs->bufr) && ('\0' != *(gs->bufr)) && (gs->len) )
     return( false );
   return( true );
   } /* isemptyGstr */
index f5ea7d283e195d5e5e96de3ea4ea0eb6428d2b85..7c61e98cfa971caa9f6dda7ac4e519fd9272e88b 100644 (file)
@@ -8,7 +8,7 @@
  *
  * Email: crh@ubiqx.mn.org
  *
- * $Id: Gstr.h 2012-06-12 21:46:10 -0500 crh$
+ * $Id: Gstr.h 2012-06-20 11:45:36 -0500 crh$
  *
  * -------------------------------------------------------------------------- **
  *
@@ -206,10 +206,15 @@ bool isemptyGstr( Gstr *gs );
    *
    *  Output: True if the string is empty, else false.
    *
+   *  Notes:  There are three factors to consider when determining whether
+   *          the string is empty.
+   *          + If the string doesn't exist (is NULL), then it's empty.
+   *          + If the string exists, but the first byte is '\0', it's empty.
+   *          + If the Gstr length is zero, the string is empty.
+   *
    * ------------------------------------------------------------------------ **
    */
 
-
 int lenGstr( Gstr *gs );
   /* ------------------------------------------------------------------------ **
    * Return the length of the string currently stored in the Gstr.