[openssl-dev] [openssl.org #4476] PATCH: fix cast-alignment of "struct lhash_st *"

Jeffrey Walton noloader at gmail.com
Fri Mar 25 08:04:07 UTC 2016


>> > $ git diff include/openssl/lhash.h
>> > diff --git a/include/openssl/lhash.h b/include/openssl/lhash.h index
>> > 2edd738..5da5054 100644
>> > --- a/include/openssl/lhash.h
>> > +++ b/include/openssl/lhash.h
>> > @@ -180,7 +180,7 @@ void lh_node_usage_stats_bio(const _LHASH *lh, BIO
>> > *out);  # define LHASH_OF(type) struct lhash_st_##type
>> >
>> >  # define DEFINE_LHASH_OF(type) \
>> > -    LHASH_OF(type) { int dummy; }; \
>> > +    LHASH_OF(type) { unsigned long dummy; }; \
>> >      static ossl_inline LHASH_OF(type) * \
>> >          lh_##type##_new(unsigned long (*hfn)(const type *), \
>> >                          int (*cfn)(const type *, const type *)) \
>>
>> Does changing it to "void *dummy" also work?
>
> Not necessarily.  A union might be more comprehensive.
>
>         LHASH_OF(type) {
>             union {
>                 void *v;
>                 unsigned long long ull;
>                 uint64_t u64;
>                 long double ld;
>             } u;
>         };

$ git diff include/openssl/lhash.h
diff --git a/include/openssl/lhash.h b/include/openssl/lhash.h
index 2edd738..b097b88 100644
--- a/include/openssl/lhash.h
+++ b/include/openssl/lhash.h
@@ -180,7 +180,7 @@ void lh_node_usage_stats_bio(const _LHASH *lh, BIO *out);
 # define LHASH_OF(type) struct lhash_st_##type

 # define DEFINE_LHASH_OF(type) \
-    LHASH_OF(type) { int dummy; }; \
+    LHASH_OF(type) { union { void* dummy1; unsigned long dummy2; }; };  \
     static ossl_inline LHASH_OF(type) * \
         lh_##type##_new(unsigned long (*hfn)(const type *), \
                         int (*cfn)(const type *, const type *)) \


Tested OK under i686, x86_64, ARM32 and ARM64.

Original code with -Wcast-align (not a typo):

    $ grep warning -c openssl-build.txt
    34180

Modified with the union and -Wcast-align:

    $ grep warning -c openssl-build.txt
    228

That's a lot of bang for the buck...

Jeff


More information about the openssl-dev mailing list