new version 1.2.12
[sahlberg/ctdb.git] / libctdb / io_elem.c
index 91e84cce5c24f743d8a971ff771514630fdab1ec..bff21cb313cba268fffbffcf78b4a4af68c41044 100644 (file)
@@ -17,6 +17,7 @@
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 #include <sys/types.h>
+#include <string.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include <unistd.h>
@@ -24,6 +25,7 @@
 #include <stdlib.h>
 #include "io_elem.h"
 #include <tdb.h>
+#include <netinet/in.h>
 #include <ctdb_protocol.h> // For CTDB_DS_ALIGNMENT and ctdb_req_header
 
 struct io_elem {
@@ -34,6 +36,8 @@ struct io_elem {
 struct io_elem *new_io_elem(size_t len)
 {
        struct io_elem *elem;
+       size_t ask = len;
+
        len = (len + (CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
 
        elem = malloc(sizeof(*elem));
@@ -45,6 +49,10 @@ struct io_elem *new_io_elem(size_t len)
                return NULL;
        }
 
+       /* stamp out any padding to keep valgrind happy */
+       if (ask != len) {
+               memset(elem->data + ask, 0, len-ask);
+       }
        elem->len = len;
        elem->off = 0;
        return elem;
@@ -103,11 +111,13 @@ int read_io_elem(int fd, struct io_elem *io)
                /* Finished.  But maybe this was just header? */
                if (io->len == sizeof(*hdr) && hdr->length > io->len) {
                        int reret;
+                       void *newdata;
                        /* Enlarge and re-read. */
                        io->len = hdr->length;
-                       io->data = realloc(io->data, io->len);
-                       if (!io->data)
+                       newdata = realloc(io->data, io->len);
+                       if (!newdata)
                                return -1;
+                       io->data = newdata;
                        /* Try reading again immediately. */
                        reret = read_io_elem(fd, io);
                        if (reret >= 0)
@@ -130,3 +140,8 @@ int write_io_elem(int fd, struct io_elem *io)
        io->off += ret;
        return ret;
 }
+
+void io_elem_reset(struct io_elem *io)
+{
+       io->off = 0;
+}