Fix memory handling
authorAlexander Bokovoy <ab@samba.org>
Mon, 18 Dec 2006 14:55:48 +0000 (17:55 +0300)
committerAlexander Bokovoy <ab@samba.org>
Mon, 18 Dec 2006 14:55:48 +0000 (17:55 +0300)
(This used to be ctdb commit a7ff834cec4c44eee7d1a84218e050a45b94d599)

ctdb/common/ctdb_ltdb.c
ctdb/include/ctdb_private.h

index 881cf48630b38893a1c18198a1a72f560a9f1cf6..895814000254207d1604fc49839649e8b5c4b155 100644 (file)
@@ -79,6 +79,7 @@ int ctdb_ltdb_fetch(struct ctdb_context *ctdb,
        if (rec.dsize < sizeof(*header)) {
                /* return an initial header */
                ltdb_initial_header(ctdb, key, header);
        if (rec.dsize < sizeof(*header)) {
                /* return an initial header */
                ltdb_initial_header(ctdb, key, header);
+               SAFE_FREE(rec.dptr);
                data->dptr = NULL;
                data->dsize = 0;
                return 0;
                data->dptr = NULL;
                data->dsize = 0;
                return 0;
@@ -89,6 +90,7 @@ int ctdb_ltdb_fetch(struct ctdb_context *ctdb,
        data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
        data->dptr = talloc_memdup(ctdb, sizeof(struct ctdb_ltdb_header)+rec.dptr,
                                   data->dsize);
        data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
        data->dptr = talloc_memdup(ctdb, sizeof(struct ctdb_ltdb_header)+rec.dptr,
                                   data->dsize);
+       SAFE_FREE(rec.dptr);
        CTDB_NO_MEMORY(ctdb, data->dptr);
 
        return 0;
        CTDB_NO_MEMORY(ctdb, data->dptr);
 
        return 0;
@@ -106,7 +108,7 @@ int ctdb_ltdb_store(struct ctdb_context *ctdb, TDB_DATA key,
        TDB_DATA rec;
        int ret;
 
        TDB_DATA rec;
        int ret;
 
-       rec.dsize = sizeof(struct ctdb_ltdb_header) + data.dsize;
+       rec.dsize = sizeof(*header) + data.dsize;
        rec.dptr = talloc_size(ctdb, rec.dsize);
        CTDB_NO_MEMORY(ctdb, rec.dptr);
 
        rec.dptr = talloc_size(ctdb, rec.dsize);
        CTDB_NO_MEMORY(ctdb, rec.dptr);
 
index f21139eab2cab36b3f4d5449588d3106c8ae1776..d4e89c83c12754f27a5a4e5b21bf85dc64d91cc0 100644 (file)
@@ -186,6 +186,11 @@ struct ctdb_reply_dmaster {
        uint8_t  data[0];
 };
 
        uint8_t  data[0];
 };
 
+/* free memory if the pointer is valid and zero the pointer */
+#ifndef SAFE_FREE
+#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
+#endif
+
 /* internal prototypes */
 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...);
 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
 /* internal prototypes */
 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...);
 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);