fix line_proto bugs new-transport
authorTero Marttila <terom@fixme.fi>
Tue, 28 Apr 2009 23:10:38 +0300
branchnew-transport
changeset 160 4f8dc89d7cbb
parent 159 d3e253d7281a
child 161 d229e4668476
fix line_proto bugs
src/line_proto.c
src/line_proto.h
--- a/src/line_proto.c	Tue Apr 28 23:10:30 2009 +0300
+++ b/src/line_proto.c	Tue Apr 28 23:10:38 2009 +0300
@@ -102,16 +102,19 @@
 {
     struct line_proto *lp;
 
+    // alloc
+    if ((lp = calloc(1, sizeof(*lp))) == NULL)
+        return SET_ERROR(err, ERR_CALLOC);
+
     // store
     lp->transport = transport;
     lp->buf_len = buf_size;
     lp->callbacks = *callbacks;
     lp->cb_arg = cb_arg;
 
-    // allocate struct and buffers
+    // allocate buffers
     if (
-            (lp = calloc(1, sizeof(*lp))) == NULL
-        ||  (lp->in_buf = malloc(buf_size)) == NULL
+            (lp->in_buf = malloc(buf_size)) == NULL
         ||  (lp->out_buf = malloc(buf_size)) == NULL
     )
         JUMP_SET_ERROR(err, ERR_CALLOC);
@@ -129,8 +132,7 @@
 
 error:
     // cleanup the lp
-    if (lp)
-        line_proto_destroy(lp);
+    line_proto_destroy(lp);
 
     return ERROR_CODE(err);
 }
@@ -281,6 +283,8 @@
     int ret;
     size_t ret_len;
 
+    assert(lp->out_offset);
+
     // try and write the line
     if ((ret = transport_write(lp->transport, lp->out_buf, lp->out_offset, &lp->err)) < 0)
         return -ERROR_CODE(&lp->err);
--- a/src/line_proto.h	Tue Apr 28 23:10:30 2009 +0300
+++ b/src/line_proto.h	Tue Apr 28 23:10:38 2009 +0300
@@ -64,6 +64,8 @@
 /**
  * Flush out any buffered line fragment. Returns zero if the buffer was flushed empty, >0 if there's still fragments
  * remaining, or -err on errors.
+ *
+ * It is a bug to call this if there is no data waiting to be sent.
  */
 int line_proto_flush (struct line_proto *lp);