enhanced/fixed memory handling + take the extra \\0 ascii char into account in string...
authorwsourdeau <wsourdeau@71d39326-ef09-db11-b2a4-00e04c779ad1>
Thu, 6 Jan 2011 03:12:27 +0000 (03:12 +0000)
committerwsourdeau <wsourdeau@71d39326-ef09-db11-b2a4-00e04c779ad1>
Thu, 6 Jan 2011 03:12:27 +0000 (03:12 +0000)
git-svn-id: https://svn.openchange.org/openchange@2484 71d39326-ef09-db11-b2a4-00e04c779ad1

branches/sogo/mapiproxy/libmapiproxy/mapi_handles.c

index 7fa2b6c6f13cadcd0db8f0f77fa8a33fdb030f82..2cbfddc3fbd6ce87fdcea6b27e9fa2f5392e75fc 100644 (file)
@@ -114,10 +114,10 @@ _PUBLIC_ enum MAPISTATUS mapi_handles_search(struct mapi_handles_context *handle
        OPENCHANGE_RETVAL_IF(!dbuf.dsize, MAPI_E_NOT_FOUND, mem_ctx);
 
        /* Ensure this is not a free'd record */
-       OPENCHANGE_RETVAL_IF((dbuf.dsize == sizeof(MAPI_HANDLES_NULL) && !strncmp((char *)dbuf.dptr, MAPI_HANDLES_NULL, dbuf.dsize)),
+       OPENCHANGE_RETVAL_IF(dbuf.dsize == (sizeof(MAPI_HANDLES_NULL) - 1) && !strncmp((char *)dbuf.dptr, MAPI_HANDLES_NULL, dbuf.dsize),
                             MAPI_E_NOT_FOUND, mem_ctx);
-       free(dbuf.dptr);
 
+       talloc_free(mem_ctx);
        /* Step 2. Return the record within the double chained list */
        for (el = handles_ctx->handles; el; el = el->next) {
                if (el->handle == handle) {
@@ -166,16 +166,13 @@ static enum MAPISTATUS mapi_handles_tdb_free(struct mapi_handles_context *handle
        dbuf.dsize = strlen(MAPI_HANDLES_NULL);
 
        ret = tdb_store(handles_ctx->tdb_ctx, key, dbuf, TDB_MODIFY);
+       talloc_free(mem_ctx);
        if (ret == -1) {
                DEBUG(3, ("[%s:%d]: Unable to create 0x%x record: %s\n", __FUNCTION__, __LINE__,
                          handle, tdb_errorstr(handles_ctx->tdb_ctx)));
-
-               talloc_free(mem_ctx);
                return MAPI_E_CORRUPT_STORE;
        }
 
-       talloc_free(mem_ctx);
-
        return MAPI_E_SUCCESS;
 }
 
@@ -212,16 +209,14 @@ static enum MAPISTATUS mapi_handles_tdb_update(struct mapi_handles_context *hand
        dbuf.dsize = strlen((const char *)dbuf.dptr);
 
        ret = tdb_store(handles_ctx->tdb_ctx, key, dbuf, TDB_MODIFY);
+       talloc_free(mem_ctx);
        if (ret == -1) {
                DEBUG(3, ("[%s:%d]: Unable to update 0x%x record: %s\n", __FUNCTION__, __LINE__,
                          handle, tdb_errorstr(handles_ctx->tdb_ctx)));
 
-               talloc_free(mem_ctx);
                return MAPI_E_CORRUPT_STORE;    
        }
 
-       talloc_free(mem_ctx);
-
        return MAPI_E_SUCCESS;
 }
 
@@ -245,11 +240,10 @@ static int mapi_handles_traverse_null(TDB_CONTEXT *tdb_ctx,
        uint32_t        *handle = (uint32_t *) state;
        char            *handle_str = NULL;
 
-       if (dbuf.dptr && dbuf.dsize == sizeof(MAPI_HANDLES_NULL) && !strncmp((const char *)dbuf.dptr, MAPI_HANDLES_NULL, dbuf.dsize)) {
+       if (dbuf.dptr && (dbuf.dsize == sizeof(MAPI_HANDLES_NULL) - 1) && !strncmp((const char *)dbuf.dptr, MAPI_HANDLES_NULL, dbuf.dsize)) {
                mem_ctx = talloc_named(NULL, 0, "mapi_handles_traverse_null");
                handle_str = talloc_strndup(mem_ctx, (char *)key.dptr, key.dsize);
                *handle = strtol((const char *) handle_str, NULL, 16);
-               talloc_free(handle_str);
                talloc_free(mem_ctx);
 
                return 1;
@@ -308,6 +302,8 @@ _PUBLIC_ enum MAPISTATUS mapi_handles_add(struct mapi_handles_context *handles_c
                *rec = el;
                DLIST_ADD_END(handles_ctx->handles, el, struct mapi_handles *);
 
+               talloc_free(mem_ctx);
+
                return MAPI_E_SUCCESS;
        }
 
@@ -426,11 +422,9 @@ static int mapi_handles_traverse_delete(TDB_CONTEXT *tdb_ctx,
        if (dbuf.dptr && strlen(container_handle_str) == dbuf.dsize && !strncmp((const char *)dbuf.dptr, container_handle_str, dbuf.dsize)) {
                handle_str = talloc_strndup(mem_ctx, (char *)key.dptr, key.dsize);
                handle = strtol((const char *) handle_str, NULL, 16);
-               talloc_free(handle_str);
                mapi_handles_delete(handles_private->handles_ctx, handle);
        }
 
-       talloc_free(container_handle_str);
        talloc_free(mem_ctx);
 
        return 0;