Add an assert to track if we try to use an rpc_context after it has been destroyed
[deb_libnfs.git] / lib / init.c
index 4c8f84f42308ea61c131089d4d35cc8d9b670720..ee6e474831b2b6913b84979d670b708521428bdd 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdarg.h>
 #include <string.h>
 #include <stdlib.h>
+#include <assert.h>
 #include <rpc/rpc.h>
 #include <rpc/xdr.h>
 #include "slist.h"
@@ -41,6 +42,7 @@ struct rpc_context *rpc_init_context(void)
        }
        memset(rpc, 0, sizeof(struct rpc_context));
 
+       rpc->magic = RPC_CONTEXT_MAGIC;
        rpc->encodebuflen = 65536;
        rpc->encodebuf = malloc(rpc->encodebuflen);
        if (rpc->encodebuf == NULL) {
@@ -79,6 +81,8 @@ struct rpc_context *rpc_init_udp_context(void)
 
 void rpc_set_auth(struct rpc_context *rpc, AUTH *auth)
 {
+       assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
        if (rpc->auth != NULL) {
                auth_destroy(rpc->auth);
        }
@@ -90,6 +94,8 @@ void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
 {
         va_list ap;
 
+       assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
        if (rpc->error_string != NULL) {
                free(rpc->error_string);
        }
@@ -101,6 +107,8 @@ void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
 
 char *rpc_get_error(struct rpc_context *rpc)
 {
+       assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
        return rpc->error_string;
 }
 
@@ -108,6 +116,8 @@ void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
 {
        struct rpc_pdu *pdu;
 
+       assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
        while((pdu = rpc->outqueue) != NULL) {
                pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
                SLIST_REMOVE(&rpc->outqueue, pdu);
@@ -130,6 +140,8 @@ static void rpc_free_fragment(struct rpc_fragment *fragment)
 
 void rpc_free_all_fragments(struct rpc_context *rpc)
 {
+       assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
        while (rpc->fragments != NULL) {
              struct rpc_fragment *fragment = rpc->fragments;
 
@@ -142,6 +154,8 @@ int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
 {
        struct rpc_fragment *fragment;
 
+       assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
        fragment = malloc(sizeof(struct rpc_fragment));
        if (fragment == NULL) {
                return -1;
@@ -163,6 +177,8 @@ void rpc_destroy_context(struct rpc_context *rpc)
 {
        struct rpc_pdu *pdu;
 
+       assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
        while((pdu = rpc->outqueue) != NULL) {
                pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
                SLIST_REMOVE(&rpc->outqueue, pdu);
@@ -202,6 +218,7 @@ void rpc_destroy_context(struct rpc_context *rpc)
                rpc->udp_dest = NULL;
        }
 
+       rpc->magic = 0;
        free(rpc);
 }