memcache/request.c
changeset 46 8a832c0e01ee
parent 44 03a7e064f833
child 48 1c67f512779b
--- a/memcache/request.c	Thu Aug 28 01:42:28 2008 +0300
+++ b/memcache/request.c	Thu Aug 28 03:12:11 2008 +0300
@@ -19,12 +19,20 @@
     // state
     req->state = MEMCACHE_STATE_INVALID;
 
+    // key length?
+    if (key->len == 0)
+        req->key.len = strlen(key->buf);
+    else
+        req->key.len = key->len;
+
+    if (req->key.len == 0)
+        ERROR("zero-length key");
+
     // copy the key
-    if ((req->key.buf = malloc(key->len)) == NULL)
+    if ((req->key.buf = malloc(req->key.len)) == NULL)
         ERROR("malloc key buf");
     
-    memcpy(req->key.buf, key->buf, key->len);
-    req->key.len = key->len;
+    memcpy(req->key.buf, key->buf, req->key.len);
     
     // copy the obj if provided
     if (obj) {
@@ -32,10 +40,16 @@
         req->have_obj = 1;
     }
 
-    // copy the buf if provided
+    // copy the buf if provided, and reset the offset to zero
     if (buf) {
+        // ensure that it is a valid buffer
+        assert(buf->data && buf->len > 0 && buf->offset == buf->len);
+
         memcpy(&req->buf, buf, sizeof(req->buf));
         req->have_buf = 1;
+
+        // set offset to zero
+        req->buf.offset = 0;
     }
 
     // store the other data