From: Brian Aker Date: Tue, 6 Apr 2010 19:33:06 +0000 (-0700) Subject: Fix issue with memcached_st structure being a different size based on X-Git-Tag: 0.40~12 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=d5c34fa54897336e3c92789d1abcceca8c5b213b;p=m6w6%2Flibmemcached Fix issue with memcached_st structure being a different size based on compile. --- diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index 5ffaee9e..e6124493 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -92,10 +92,7 @@ static inline bool _memcached_init(memcached_st *self) self->get_key_failure= NULL; self->delete_trigger= NULL; self->callbacks= NULL; -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - self->sasl.callbacks= NULL; - self->sasl.is_allocated= false; -#endif + self->sasl= NULL; return true; } @@ -170,12 +167,12 @@ void memcached_free(memcached_st *ptr) if (ptr->continuum) libmemcached_free(ptr, ptr->continuum); -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - if (ptr->sasl.callbacks != NULL) + if (ptr->sasl) { +#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT memcached_destroy_sasl_auth_data(ptr); - } #endif + } if (memcached_is_allocated(ptr)) { @@ -267,7 +264,7 @@ memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source) } #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - if (source->sasl.callbacks) + if (source->sasl && source->sasl->callbacks) { if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS) { diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index 668e9a12..69b469dc 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -125,17 +125,7 @@ struct memcached_st { memcached_trigger_key_fn get_key_failure; memcached_trigger_delete_key_fn delete_trigger; memcached_callback_st *callbacks; -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - struct { - const sasl_callback_t *callbacks; - /* - ** Did we allocate data inside the callbacks, or did the user - ** supply that. - */ - bool is_allocated:1; - } sasl; - -#endif + struct memcached_sasl_st *sasl; char prefix_key[MEMCACHED_PREFIX_KEY_MAX_SIZE]; struct { bool is_allocated:1; diff --git a/libmemcached/sasl.c b/libmemcached/sasl.c index d77532de..440b7638 100644 --- a/libmemcached/sasl.c +++ b/libmemcached/sasl.c @@ -14,13 +14,13 @@ void memcached_set_sasl_callbacks(memcached_st *ptr, const sasl_callback_t *callbacks) { - ptr->sasl.callbacks= callbacks; - ptr->sasl.is_allocated= false; + ptr->sasl->callbacks= callbacks; + ptr->sasl->is_allocated= false; } const sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *ptr) { - return ptr->sasl.callbacks; + return ptr->sasl->callbacks; } /** @@ -116,7 +116,7 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s sasl_conn_t *conn; int ret= sasl_client_new("memcached", server->hostname, laddr, raddr, - server->root->sasl.callbacks, 0, &conn); + server->root->sasl->callbacks, 0, &conn); if (ret != SASL_OK) { return MEMCACHED_AUTH_PROBLEM; @@ -218,7 +218,7 @@ memcached_return_t memcached_set_sasl_auth_data(memcached_st *ptr, const char *password) { if (ptr == NULL || username == NULL || - password == NULL || ptr->sasl.callbacks != NULL) + password == NULL || ptr->sasl->callbacks != NULL) { return MEMCACHED_FAILURE; } @@ -249,46 +249,62 @@ memcached_return_t memcached_set_sasl_auth_data(memcached_st *ptr, cb[2].context= secret; cb[3].id= SASL_CB_LIST_END; - ptr->sasl.callbacks= cb; - ptr->sasl.is_allocated= true; + ptr->sasl->callbacks= cb; + ptr->sasl->is_allocated= true; return MEMCACHED_SUCCESS; } memcached_return_t memcached_destroy_sasl_auth_data(memcached_st *ptr) { - if (ptr == NULL || ptr->sasl.callbacks == NULL) + if (ptr == NULL || ptr->sasl->callbacks == NULL) { return MEMCACHED_FAILURE; } - if (ptr->sasl.is_allocated) + if (ptr->sasl->is_allocated) { - libmemcached_free(ptr, ptr->sasl.callbacks[0].context); - libmemcached_free(ptr, ptr->sasl.callbacks[2].context); - libmemcached_free(ptr, (void*)ptr->sasl.callbacks); - ptr->sasl.is_allocated= false; + libmemcached_free(ptr, ptr->sasl->callbacks[0].context); + libmemcached_free(ptr, ptr->sasl->callbacks[2].context); + libmemcached_free(ptr, (void*)ptr->sasl->callbacks); + ptr->sasl->is_allocated= false; } - ptr->sasl.callbacks= NULL; + ptr->sasl->callbacks= NULL; + libmemcached_free(ptr, ptr->sasl); + ptr->sasl= NULL; return MEMCACHED_SUCCESS; } memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st *source) { + if (source->sasl == NULL) + { + return MEMCACHED_SUCCESS; + } + else + { + clone->sasl= libmemcached_malloc(source, sizeof(struct memcached_sasl_st)); + + if (clone->sasl == NULL) + { + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + } + } + /* Hopefully we are using our own callback mechanisms.. */ - if (source->sasl.callbacks[0].id == SASL_CB_USER && - source->sasl.callbacks[0].proc == get_username && - source->sasl.callbacks[1].id == SASL_CB_AUTHNAME && - source->sasl.callbacks[1].proc == get_username && - source->sasl.callbacks[2].id == SASL_CB_PASS && - source->sasl.callbacks[2].proc == get_password && - source->sasl.callbacks[3].id == SASL_CB_LIST_END) + if (source->sasl->callbacks[0].id == SASL_CB_USER && + source->sasl->callbacks[0].proc == get_username && + source->sasl->callbacks[1].id == SASL_CB_AUTHNAME && + source->sasl->callbacks[1].proc == get_username && + source->sasl->callbacks[2].id == SASL_CB_PASS && + source->sasl->callbacks[2].proc == get_password && + source->sasl->callbacks[3].id == SASL_CB_LIST_END) { - sasl_secret_t *secret= source->sasl.callbacks[2].context; + sasl_secret_t *secret= source->sasl->callbacks[2].context; return memcached_set_sasl_auth_data(clone, - source->sasl.callbacks[0].context, + source->sasl->callbacks[0].context, (const char*)secret->data); } @@ -299,8 +315,9 @@ memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st */ size_t total= 0; - while (source->sasl.callbacks[total].id != SASL_CB_LIST_END) { - switch (source->sasl.callbacks[total].id) + while (source->sasl->callbacks[total].id != SASL_CB_LIST_END) + { + switch (source->sasl->callbacks[total].id) { case SASL_CB_USER: case SASL_CB_AUTHNAME: @@ -314,43 +331,43 @@ memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st ++total; } - sasl_callback_t *cb= libmemcached_calloc(clone, total + 1, - sizeof(sasl_callback_t)); + sasl_callback_t *cb= libmemcached_calloc(clone, total + 1, sizeof(sasl_callback_t)); if (cb == NULL) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - memcpy(cb, source->sasl.callbacks, (total + 1) * sizeof(sasl_callback_t)); + memcpy(cb, source->sasl->callbacks, (total + 1) * sizeof(sasl_callback_t)); /* Now update the context... */ for (size_t x= 0; x < total; ++x) { if (cb[x].id == SASL_CB_USER || cb[x].id == SASL_CB_AUTHNAME) { - cb[x].context= libmemcached_malloc(clone, strlen(source->sasl.callbacks[x].context)); + cb[x].context= libmemcached_malloc(clone, strlen(source->sasl->callbacks[x].context)); + if (cb[x].context == NULL) { /* Failed to allocate memory, clean up previously allocated memory */ for (size_t y= 0; y < x; ++y) { - libmemcached_free(clone, clone->sasl.callbacks[y].context); + libmemcached_free(clone, clone->sasl->callbacks[y].context); } libmemcached_free(clone, cb); return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - strcpy(cb[x].context, source->sasl.callbacks[x].context); + strcpy(cb[x].context, source->sasl->callbacks[x].context); } else { - sasl_secret_t *src = source->sasl.callbacks[x].context; + sasl_secret_t *src = source->sasl->callbacks[x].context; sasl_secret_t *n = libmemcached_malloc(clone, src->len + 1 + sizeof(*n)); if (n == NULL) { /* Failed to allocate memory, clean up previously allocated memory */ for (size_t y= 0; y < x; ++y) { - libmemcached_free(clone, clone->sasl.callbacks[y].context); + libmemcached_free(clone, clone->sasl->callbacks[y].context); } libmemcached_free(clone, cb); @@ -361,8 +378,8 @@ memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st } } - clone->sasl.callbacks= cb; - clone->sasl.is_allocated= true; + clone->sasl->callbacks= cb; + clone->sasl->is_allocated= true; return MEMCACHED_SUCCESS; } diff --git a/libmemcached/sasl.h b/libmemcached/sasl.h index 6867f079..1105cd81 100644 --- a/libmemcached/sasl.h +++ b/libmemcached/sasl.h @@ -47,4 +47,19 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s #endif /* LIBMEMCACHED_WITH_SASL_SUPPORT */ +struct memcached_sasl_st { +#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT + const sasl_callback_t *callbacks; +#else + const void *callbacks; +#endif + /* + ** Did we allocate data inside the callbacks, or did the user + ** supply that. + */ + bool is_allocated; +}; + + + #endif /* LIBMEMCACHED_MEMCACHED_SASL_H */