[or-cvs] Resolve type-punning warnings

Ben Laurie ben at algroup.co.uk
Wed Mar 31 19:46:40 UTC 2004


Nick Mathewson wrote:

> Update of /home/or/cvsroot/src/common
> In directory moria.mit.edu:/tmp/cvs-serv317/src/common
> 
> Modified Files:
> 	crypto.c 
> Log Message:
> Resolve type-punning warnings
> 
> Index: crypto.c
> ===================================================================
> RCS file: /home/or/cvsroot/src/common/crypto.c,v
> retrieving revision 1.59
> retrieving revision 1.60
> diff -u -d -r1.59 -r1.60
> --- crypto.c	30 Mar 2004 22:42:26 -0000	1.59
> +++ crypto.c	30 Mar 2004 22:50:49 -0000	1.60
> @@ -650,12 +650,21 @@
>  int crypto_pk_asn1_encode(crypto_pk_env_t *pk, char *dest, int dest_len)
>  {
>    int len;
> +  unsigned char *buf, *bufp;
>    len = i2d_RSAPublicKey((RSA*)pk->key, NULL);
>    if (len < 0 || len > dest_len)
>      return -1;
> -  len = i2d_RSAPublicKey((RSA*)pk->key, (unsigned char**)&dest);
> -  if (len < 0)
> +  bufp = buf = (unsigned char *)tor_malloc(len+1);
> +  len = i2d_RSAPublicKey((RSA*)pk->key, &bufp);
> +  if (len < 0) {
> +    tor_free(buf);
>      return -1;
> +  }
> +  /* We don't encode directly into 'dest', because that would be illegal
> +   * type-punning.  (C99 is smarter than me, C99 is smarter than me...)
> +   */
> +  memcpy(dest,buf,len);
> +  tor_free(buf);
>    return len;
>  }
>  
> @@ -664,14 +673,18 @@
>  crypto_pk_env_t *crypto_pk_asn1_decode(const char *str, int len)
>  {
>    RSA *rsa;
> +  unsigned char *buf, *bufp;
> +  bufp = buf = (unsigned char *)tor_malloc(len);

Why isn't tor_malloc returning void *?

> +  memcpy(buf,str,len);
>    /* This ifdef suppresses a type warning.  Take out the first case once
>     * everybody is using openssl 0.9.7 or later.
>     */
>  #if OPENSSL_VERSION_NUMBER < 0x00907000l
> -  rsa = d2i_RSAPublicKey(NULL, (unsigned char**)&str, len);
> +  rsa = d2i_RSAPublicKey(NULL, &bufp, len);
>  #else
> -  rsa = d2i_RSAPublicKey(NULL, (const unsigned char**)&str, len);
> +  rsa = d2i_RSAPublicKey(NULL, (const unsigned char **)&bufp, len);

Why not make bufp a const unsigned char *?

>  #endif
> +  tor_free(buf);
>    if (!rsa)
>      return NULL; /* XXXX log openssl error */
>    return _crypto_new_pk_env_rsa(rsa);
> 
> 


-- 
http://www.apache-ssl.org/ben.html       http://www.thebunker.net/

"There is no limit to what a man can do or how far he can go if he
doesn't mind who gets the credit." - Robert Woodruff



More information about the tor-dev mailing list