Updating from Monty
[m6w6/libmemcached] / clients / generator.c
index cd1a17bfe71059fceb49fff382b7975220284b35..80b398b251df3a73b0f476f0439a91553f8951b1 100644 (file)
@@ -1,4 +1,15 @@
-#include "libmemcached/common.h"
+/* LibMemcached
+ * Copyright (C) 2006-2009 Brian Aker
+ * All rights reserved.
+ *
+ * Use and distribution licensed under the BSD license.  See
+ * the COPYING file in the parent directory for full text.
+ *
+ * Summary:
+ *
+ */
+
+#include "config.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -31,14 +42,15 @@ void pairs_free(pairs_st *pairs)
 {
   uint32_t x;
 
-  if (!pairs)
+  if (! pairs)
     return;
 
   /* We free until we hit the null pair we stores during creation */
   for (x= 0; pairs[x].key; x++)
   {
     free(pairs[x].key);
-    free(pairs[x].value);
+    if (pairs[x].value)
+      free(pairs[x].value);
   }
 
   free(pairs);
@@ -49,7 +61,7 @@ pairs_st *pairs_generate(uint64_t number_of, size_t value_length)
   unsigned int x;
   pairs_st *pairs;
 
-  pairs= (pairs_st*)calloc(number_of + 1, sizeof(pairs_st));
+  pairs= (pairs_st*)calloc((size_t)number_of + 1, sizeof(pairs_st));
 
   if (!pairs)
     goto error;
@@ -62,11 +74,19 @@ pairs_st *pairs_generate(uint64_t number_of, size_t value_length)
     get_random_string(pairs[x].key, 100);
     pairs[x].key_length= 100;
 
-    pairs[x].value= (char *)calloc(value_length, sizeof(char));
-    if (!pairs[x].value)
-      goto error;
-    get_random_string(pairs[x].value, value_length);
-    pairs[x].value_length= value_length;
+    if (value_length)
+    {
+      pairs[x].value= (char *)calloc(value_length, sizeof(char));
+      if (!pairs[x].value)
+        goto error;
+      get_random_string(pairs[x].value, value_length);
+      pairs[x].value_length= value_length;
+    }
+    else
+    {
+      pairs[x].value= NULL;
+      pairs[x].value_length= 0;
+    }
   }
 
   return pairs;