Fixing cast-align warning in ovdb

Julien ÉLIE julien at trigofacile.com
Sun Sep 2 20:10:07 UTC 2018


Hi Russ,

>> 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.

-    ((struct ovdata *)databuf)->token = token;
-    ((struct ovdata *)databuf)->arrived = arrived;
-    ((struct ovdata *)databuf)->expires = expires;
+    ((struct ovdata *)(void *)databuf)->token = token;
+    ((struct ovdata *)(void *)databuf)->arrived = arrived;
+    ((struct ovdata *)(void *)databuf)->expires = expires;

gcc indeed no longer complains with the above change.



> 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.

OK, thanks!



>> 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?

Still the same cast-align error with either
   dl = (DB_LSN *)((char *)cp + tab[i].a);
or
   dl = (DB_LSN *)((char *)p + tab[i].a);

Note that
   dl = (DB_LSN *)(void *)(cp + tab[i].a);
suppresses the warning.  I can commit that change too so that the 
warning is suppressed.

-- 
Julien ÉLIE

« Vti, non abuti. »


More information about the inn-workers mailing list