Next step in tidying up the LHASH code.
authorGeoff Thorpe <geoff@openssl.org>
Fri, 8 Dec 2000 20:02:01 +0000 (20:02 +0000)
committerGeoff Thorpe <geoff@openssl.org>
Fri, 8 Dec 2000 20:02:01 +0000 (20:02 +0000)
DECLARE/IMPLEMENT macros now exist to create type (and prototype) safe
wrapper functions that avoid the use of function pointer casting yet retain
type-safety for type-specific callbacks. However, most of the usage within
OpenSSL itself doesn't really require the extra function because the hash
and compare callbacks are internal functions declared only for use by the
hash table. So this change catches all those cases and reimplements the
functions using the base-level LHASH prototypes and does per-variable
casting inside those functions to convert to the appropriate item type.

The exception so far is in ssl_lib.c where the hash and compare callbacks
are not static - they're exposed in ssl.h so their prototypes should not be
changed. In this last case, the IMPLEMENT_LHASH_*** macros have been left
intact.

apps/openssl.c
crypto/conf/conf_api.c
crypto/err/err.c
crypto/mem_dbg.c
crypto/objects/o_names.c
crypto/objects/obj_dat.c
ssl/ssl_lib.c

index b7e50c7..14cb93e 100644 (file)
 #include "s_apps.h"
 #include <openssl/err.h>
 
-static unsigned long MS_CALLBACK hash(FUNCTION *a);
-static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b);
+/* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
+ * base prototypes (we cast each variable inside the function to the required
+ * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
+ * functions. */
+
+/* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
+static unsigned long MS_CALLBACK hash(void *a_void);
+/* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
+static int MS_CALLBACK cmp(void *a_void,void *b_void);
 static LHASH *prog_init(void );
 static int do_cmd(LHASH *prog,int argc,char *argv[]);
 LHASH *config=NULL;
@@ -85,9 +92,6 @@ char *default_config_file=NULL;
 BIO *bio_err=NULL;
 #endif
 
-static IMPLEMENT_LHASH_HASH_FN(hash,FUNCTION *)
-static IMPLEMENT_LHASH_COMP_FN(cmp,FUNCTION *)
-
 int main(int Argc, char *Argv[])
        {
        ARGS arg;
@@ -354,8 +358,7 @@ static LHASH *prog_init(void)
            ;
        qsort(functions,i,sizeof *functions,SortFnByName);
 
-       if ((ret=lh_new(LHASH_HASH_FN(hash),
-                       LHASH_COMP_FN(cmp))) == NULL)
+       if ((ret=lh_new(hash, cmp)) == NULL)
                return(NULL);
 
        for (f=functions; f->name != NULL; f++)
@@ -363,12 +366,15 @@ static LHASH *prog_init(void)
        return(ret);
        }
 
-static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b)
+/* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
+static int MS_CALLBACK cmp(void *a_void, void *b_void)
        {
-       return(strncmp(a->name,b->name,8));
+       return(strncmp(((FUNCTION *)a_void)->name,
+                       ((FUNCTION *)b_void)->name,8));
        }
 
-static unsigned long MS_CALLBACK hash(FUNCTION *a)
+/* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
+static unsigned long MS_CALLBACK hash(void *a_void)
        {
-       return(lh_strhash(a->name));
+       return(lh_strhash(((FUNCTION *)a_void)->name));
        }
index 64bbd97..8216e64 100644 (file)
 
 static void value_free_hash(CONF_VALUE *a, LHASH *conf);
 static void value_free_stack(CONF_VALUE *a,LHASH *conf);
-static unsigned long hash(CONF_VALUE *v);
-static int cmp_conf(CONF_VALUE *a,CONF_VALUE *b);
-
-static IMPLEMENT_LHASH_HASH_FN(hash, CONF_VALUE *)
-static IMPLEMENT_LHASH_COMP_FN(cmp_conf, CONF_VALUE *)
+/* We don't use function pointer casting or wrapper functions - but cast each
+ * callback parameter inside the callback functions. */
+/* static unsigned long hash(CONF_VALUE *v); */
+static unsigned long hash(void *v_void);
+/* static int cmp_conf(CONF_VALUE *a,CONF_VALUE *b); */
+static int cmp_conf(void *a_void,void *b_void);
 
 /* Up until OpenSSL 0.9.5a, this was get_section */
 CONF_VALUE *_CONF_get_section(CONF *conf, char *section)
@@ -184,8 +185,7 @@ int _CONF_new_data(CONF *conf)
                return 0;
                }
        if (conf->data == NULL)
-               if ((conf->data = lh_new(LHASH_HASH_FN(hash),
-                                       LHASH_COMP_FN(cmp_conf))) == NULL)
+               if ((conf->data = lh_new(hash, cmp_conf)) == NULL)
                        {
                        return 0;
                        }
@@ -238,14 +238,19 @@ static void value_free_stack(CONF_VALUE *a, LHASH *conf)
        OPENSSL_free(a);
        }
 
-static unsigned long hash(CONF_VALUE *v)
+/* static unsigned long hash(CONF_VALUE *v) */
+static unsigned long hash(void *v_void)
        {
+       CONF_VALUE *v = (CONF_VALUE *)v_void;
        return((lh_strhash(v->section)<<2)^lh_strhash(v->name));
        }
 
-static int cmp_conf(CONF_VALUE *a, CONF_VALUE *b)
+/* static int cmp_conf(CONF_VALUE *a, CONF_VALUE *b) */
+static int cmp_conf(void *a_void, void *b_void)
        {
        int i;
+       CONF_VALUE *a = (CONF_VALUE *)a_void;
+       CONF_VALUE *b = (CONF_VALUE *)b_void;
 
        if (a->section != b->section)
                {
index 3b4de23..48eed68 100644 (file)
 static LHASH *error_hash=NULL;
 static LHASH *thread_hash=NULL;
 
-static unsigned long err_hash(ERR_STRING_DATA *a);
-static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b);
-static unsigned long pid_hash(ERR_STATE *pid);
-static int pid_cmp(ERR_STATE *a,ERR_STATE *pid);
+/* static unsigned long err_hash(ERR_STRING_DATA *a); */
+static unsigned long err_hash(void *a_void);
+/* static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b); */
+static int err_cmp(void *a_void, void *b_void);
+/* static unsigned long pid_hash(ERR_STATE *pid); */
+static unsigned long pid_hash(void *pid_void);
+/* static int pid_cmp(ERR_STATE *a,ERR_STATE *pid); */
+static int pid_cmp(void *a_void,void *pid_void);
 static unsigned long get_error_values(int inc,const char **file,int *line,
                                      const char **data,int *flags);
 
-static IMPLEMENT_LHASH_HASH_FN(err_hash, ERR_STRING_DATA *)
-static IMPLEMENT_LHASH_COMP_FN(err_cmp, ERR_STRING_DATA *)
-static IMPLEMENT_LHASH_HASH_FN(pid_hash, ERR_STATE *)
-static IMPLEMENT_LHASH_COMP_FN(pid_cmp, ERR_STATE *)
-
 static void ERR_STATE_free(ERR_STATE *s);
 #ifndef NO_ERR
 static ERR_STRING_DATA ERR_str_libraries[]=
@@ -322,8 +321,7 @@ void ERR_load_strings(int lib, ERR_STRING_DATA *str)
        if (error_hash == NULL)
                {
                CRYPTO_w_lock(CRYPTO_LOCK_ERR_HASH);
-               error_hash=lh_new(LHASH_HASH_FN(err_hash),
-                               LHASH_COMP_FN(err_cmp));
+               error_hash=lh_new(err_hash, err_cmp);
                if (error_hash == NULL)
                        {
                        CRYPTO_w_unlock(CRYPTO_LOCK_ERR_HASH);
@@ -627,28 +625,34 @@ const char *ERR_reason_error_string(unsigned long e)
        return((p == NULL)?NULL:p->string);
        }
 
-static unsigned long err_hash(ERR_STRING_DATA *a)
+/* static unsigned long err_hash(ERR_STRING_DATA *a) */
+static unsigned long err_hash(void *a_void)
        {
        unsigned long ret,l;
 
-       l=a->error;
+       l=((ERR_STRING_DATA *)a_void)->error;
        ret=l^ERR_GET_LIB(l)^ERR_GET_FUNC(l);
        return(ret^ret%19*13);
        }
 
-static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b)
+/* static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b) */
+static int err_cmp(void *a_void, void *b_void)
        {
-       return((int)(a->error-b->error));
+       return((int)(((ERR_STRING_DATA *)a_void)->error -
+                       ((ERR_STRING_DATA *)b_void)->error));
        }
 
-static unsigned long pid_hash(ERR_STATE *a)
+/* static unsigned long pid_hash(ERR_STATE *a) */
+static unsigned long pid_hash(void *a_void)
        {
-       return(a->pid*13);
+       return(((ERR_STATE *)a_void)->pid*13);
        }
 
-static int pid_cmp(ERR_STATE *a, ERR_STATE *b)
+/* static int pid_cmp(ERR_STATE *a, ERR_STATE *b) */
+static int pid_cmp(void *a_void, void *b_void)
        {
-       return((int)((long)a->pid - (long)b->pid));
+       return((int)((long)((ERR_STATE *)a_void)->pid -
+                       (long)((ERR_STATE *)b_void)->pid));
        }
 
 void ERR_remove_state(unsigned long pid)
@@ -713,8 +717,7 @@ ERR_STATE *ERR_get_state(void)
                /* no entry yet in thread_hash for current thread -
                 * thus, it may have changed since we last looked at it */
                if (thread_hash == NULL)
-                       thread_hash = lh_new(LHASH_HASH_FN(pid_hash),
-                                       LHASH_COMP_FN(pid_cmp));
+                       thread_hash = lh_new(pid_hash, pid_cmp);
                if (thread_hash == NULL)
                        thread_state_exists = 0; /* allocation error */
                else
index 56ce7dd..8e48e25 100644 (file)
@@ -219,42 +219,40 @@ long CRYPTO_dbg_get_options(void)
        return options;
        }
 
-static int mem_cmp(MEM *a, MEM *b)
+/* static int mem_cmp(MEM *a, MEM *b) */
+static int mem_cmp(void *a_void, void *b_void)
        {
-       return((char *)a->addr - (char *)b->addr);
+       return((char *)((MEM *)a_void)->addr - (char *)((MEM *)b_void)->addr);
        }
 
-static unsigned long mem_hash(MEM *a)
+/* static unsigned long mem_hash(MEM *a) */
+static unsigned long mem_hash(void *a_void)
        {
        unsigned long ret;
 
-       ret=(unsigned long)a->addr;
+       ret=(unsigned long)((MEM *)a_void)->addr;
 
        ret=ret*17851+(ret>>14)*7+(ret>>4)*251;
        return(ret);
        }
 
-static IMPLEMENT_LHASH_HASH_FN(mem_hash, MEM *)
-static IMPLEMENT_LHASH_COMP_FN(mem_cmp, MEM *)
-
-static int app_info_cmp(APP_INFO *a, APP_INFO *b)
+/* static int app_info_cmp(APP_INFO *a, APP_INFO *b) */
+static int app_info_cmp(void *a_void, void *b_void)
        {
-       return(a->thread != b->thread);
+       return(((APP_INFO *)a_void)->thread != ((APP_INFO *)b_void)->thread);
        }
 
-static unsigned long app_info_hash(APP_INFO *a)
+/* static unsigned long app_info_hash(APP_INFO *a) */
+static unsigned long app_info_hash(void *a_void)
        {
        unsigned long ret;
 
-       ret=(unsigned long)a->thread;
+       ret=(unsigned long)((APP_INFO *)a_void)->thread;
 
        ret=ret*17851+(ret>>14)*7+(ret>>4)*251;
        return(ret);
        }
 
-static IMPLEMENT_LHASH_HASH_FN(app_info_hash, APP_INFO *)
-static IMPLEMENT_LHASH_COMP_FN(app_info_cmp, APP_INFO *)
-
 static APP_INFO *pop_info(void)
        {
        APP_INFO tmp;
@@ -308,8 +306,7 @@ int CRYPTO_push_info_(const char *info, const char *file, int line)
                        }
                if (amih == NULL)
                        {
-                       if ((amih=lh_new(LHASH_HASH_FN(app_info_hash),
-                                       LHASH_COMP_FN(app_info_cmp))) == NULL)
+                       if ((amih=lh_new(app_info_hash, app_info_cmp)) == NULL)
                                {
                                OPENSSL_free(ami);
                                ret=0;
@@ -401,8 +398,7 @@ void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line,
                                }
                        if (mh == NULL)
                                {
-                               if ((mh=lh_new(LHASH_HASH_FN(mem_hash),
-                                       LHASH_COMP_FN(mem_cmp))) == NULL)
+                               if ((mh=lh_new(mem_hash, mem_cmp)) == NULL)
                                        {
                                        OPENSSL_free(addr);
                                        OPENSSL_free(m);
index ccf3a18..a5b1aac 100644 (file)
@@ -24,18 +24,20 @@ IMPLEMENT_STACK_OF(NAME_FUNCS)
 
 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
 
-static unsigned long obj_name_hash(OBJ_NAME *a);
-static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b);
+/* The LHASH callbacks now use the raw "void *" prototypes and do per-variable
+ * casting in the functions. This prevents function pointer casting without the
+ * need for macro-generated wrapper functions. */
 
-static IMPLEMENT_LHASH_HASH_FN(obj_name_hash, OBJ_NAME *)
-static IMPLEMENT_LHASH_COMP_FN(obj_name_cmp, OBJ_NAME *)
+/* static unsigned long obj_name_hash(OBJ_NAME *a); */
+static unsigned long obj_name_hash(void *a_void);
+/* static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); */
+static int obj_name_cmp(void *a_void,void *b_void);
 
 int OBJ_NAME_init(void)
        {
        if (names_lh != NULL) return(1);
        MemCheck_off();
-       names_lh=lh_new(LHASH_HASH_FN(obj_name_hash),
-                       LHASH_COMP_FN(obj_name_cmp));
+       names_lh=lh_new(obj_name_hash, obj_name_cmp);
        MemCheck_on();
        return(names_lh != NULL);
        }
@@ -85,9 +87,12 @@ int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
        return(ret);
        }
 
-static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b)
+/* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
+static int obj_name_cmp(void *a_void, void *b_void)
        {
        int ret;
+       OBJ_NAME *a = (OBJ_NAME *)a_void;
+       OBJ_NAME *b = (OBJ_NAME *)b_void;
 
        ret=a->type-b->type;
        if (ret == 0)
@@ -104,9 +109,11 @@ static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b)
        return(ret);
        }
 
-static unsigned long obj_name_hash(OBJ_NAME *a)
+/* static unsigned long obj_name_hash(OBJ_NAME *a) */
+static unsigned long obj_name_hash(void *a_void)
        {
        unsigned long ret;
+       OBJ_NAME *a = (OBJ_NAME *)a_void;
 
        if ((name_funcs_stack != NULL) && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type))
                {
index 6eb9f48..0057da2 100644 (file)
@@ -108,12 +108,14 @@ static int ln_cmp(const void *a, const void *b)
        return(strcmp((*ap)->ln,(*bp)->ln));
        }
 
-static unsigned long add_hash(ADDED_OBJ *ca)
+/* static unsigned long add_hash(ADDED_OBJ *ca) */
+static unsigned long add_hash(void *ca_void)
        {
        ASN1_OBJECT *a;
        int i;
        unsigned long ret=0;
        unsigned char *p;
+       ADDED_OBJ *ca = (ADDED_OBJ *)ca_void;
 
        a=ca->obj;
        switch (ca->type)
@@ -142,10 +144,13 @@ static unsigned long add_hash(ADDED_OBJ *ca)
        return(ret);
        }
 
-static int add_cmp(ADDED_OBJ *ca, ADDED_OBJ *cb)
+/* static int add_cmp(ADDED_OBJ *ca, ADDED_OBJ *cb) */
+static int add_cmp(void *ca_void, void *cb_void)
        {
        ASN1_OBJECT *a,*b;
        int i;
+       ADDED_OBJ *ca = (ADDED_OBJ *)ca_void;
+       ADDED_OBJ *cb = (ADDED_OBJ *)cb_void;
 
        i=ca->type-cb->type;
        if (i) return(i);
@@ -174,13 +179,10 @@ static int add_cmp(ADDED_OBJ *ca, ADDED_OBJ *cb)
        return(1); /* should not get here */
        }
 
-static IMPLEMENT_LHASH_HASH_FN(add_hash, ADDED_OBJ *)
-static IMPLEMENT_LHASH_COMP_FN(add_cmp, ADDED_OBJ *)
-
 static int init_added(void)
        {
        if (added != NULL) return(1);
-       added=lh_new(LHASH_HASH_FN(add_hash),LHASH_COMP_FN(add_cmp));
+       added=lh_new(add_hash,add_cmp);
        return(added != NULL);
        }
 
index 685fc55..c757ea8 100644 (file)
@@ -1101,6 +1101,10 @@ int SSL_SESSION_cmp(SSL_SESSION *a,SSL_SESSION *b)
        return(memcmp(a->session_id,b->session_id,a->session_id_length));
        }
 
+/* These wrapper functions should remain rather than redeclaring
+ * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
+ * variable. The reason is that the functions aren't static, they're exposed via
+ * ssl.h. */
 static IMPLEMENT_LHASH_HASH_FN(SSL_SESSION_hash, SSL_SESSION *)
 static IMPLEMENT_LHASH_COMP_FN(SSL_SESSION_cmp, SSL_SESSION *)