Fixing cast-align warning in ovdb
Russ Allbery
eagle at eyrie.org
Sat Sep 1 21:45:09 UTC 2018
Julien ÉLIE <julien at trigofacile.com> writes:
> Does anyone have a clue about how to fix the following new warnings
> emitted by GCC 8? (without of course generating another different
> warning)
> ovdb/ovdb.c: In function 'ovdb_add':
> ovdb/ovdb.c:1951:6: error: cast increases required alignment of target type [-Werror=cast-align]
> ((struct ovdata *)databuf)->token = token;
> ^
> ovdb/ovdb.c:1952:6: error: cast increases required alignment of target type [-Werror=cast-align]
> ((struct ovdata *)databuf)->arrived = arrived;
> ^
> ovdb/ovdb.c:1953:6: error: cast increases required alignment of target type [-Werror=cast-align]
> ((struct ovdata *)databuf)->expires = expires;
> ^
I suspect it will work to change the type of databuf to void *, and then
cast it to (struct ovdata *) when writing into that header and later to
assign it to a variable of type char * when doing pointer math on it. The
pointer is aligned, since the pointer is returned from malloc(), which is
guaranteed to return a pointer with suitable alignment for any built-in
type, so I think the warnings are just because it's immediately assigned
to a char * variable, which has no required alignment, and the compiler
isn't smart enough to keep track of this.
> ovdb_stat.c: In function 'print_value':
> ovdb_stat.c:157:7: warning: cast increases required alignment of target type [-Werror=cast-align]
> dl = (DB_LSN *)(cp + tab[i].a);
> ^
This is a similar problem, but the pointer math immediately before use of
the pointer makes it a bit more complicated. If you replace cp in that
expression with (char *) p, I wonder if having all the casts in the same
expression would let the compiler figure it out?
--
Russ Allbery (eagle at eyrie.org) <http://www.eyrie.org/~eagle/>
Please send questions to the list rather than mailing me directly.
<http://www.eyrie.org/~eagle/faqs/questions.html> explains why.
More information about the inn-workers
mailing list