Workaround clang alignment warnings (Wcast-align) when working with byt arrays.

This should silence similar warnings like
  warning: cast from 'char *' to 'struct xyz *' increases required alignment from 1 to X
when we try to calclulate byte pointer offsets in a buffer.
This commit is contained in:
Milan Broz
2022-01-20 12:09:44 +01:00
parent c11a83bf0f
commit 0b2c4187b0
6 changed files with 27 additions and 15 deletions

View File

@@ -329,7 +329,7 @@ static int parse_vmk_entry(struct crypt_device *cd, uint8_t *data, int start, in
string = malloc((key_entry_size - BITLK_ENTRY_HEADER_LEN) * 2 + 1); string = malloc((key_entry_size - BITLK_ENTRY_HEADER_LEN) * 2 + 1);
if (!string) if (!string)
return -ENOMEM; return -ENOMEM;
r = crypt_utf16_to_utf8(&string, (const char16_t *) (data + start + BITLK_ENTRY_HEADER_LEN), r = crypt_utf16_to_utf8(&string, CONST_CAST(char16_t *)(data + start + BITLK_ENTRY_HEADER_LEN),
key_entry_size - BITLK_ENTRY_HEADER_LEN); key_entry_size - BITLK_ENTRY_HEADER_LEN);
if (r < 0 || !string) { if (r < 0 || !string) {
free(string); free(string);
@@ -680,7 +680,7 @@ int BITLK_read_sb(struct crypt_device *cd, struct bitlk_metadata *params)
description = malloc((entry_size - BITLK_ENTRY_HEADER_LEN - BITLK_ENTRY_HEADER_LEN) * 2 + 1); description = malloc((entry_size - BITLK_ENTRY_HEADER_LEN - BITLK_ENTRY_HEADER_LEN) * 2 + 1);
if (!description) if (!description)
return -ENOMEM; return -ENOMEM;
r = crypt_utf16_to_utf8(&description, (const char16_t *) (fve_entries + start + BITLK_ENTRY_HEADER_LEN), r = crypt_utf16_to_utf8(&description, CONST_CAST(char16_t *)(fve_entries + start + BITLK_ENTRY_HEADER_LEN),
entry_size - BITLK_ENTRY_HEADER_LEN); entry_size - BITLK_ENTRY_HEADER_LEN);
if (r < 0 || !description) { if (r < 0 || !description) {
free(description); free(description);

View File

@@ -151,7 +151,8 @@ static int crypt_sector_iv_init(struct crypt_sector_iv *ctx,
static int crypt_sector_iv_generate(struct crypt_sector_iv *ctx, uint64_t sector) static int crypt_sector_iv_generate(struct crypt_sector_iv *ctx, uint64_t sector)
{ {
uint64_t val; uint64_t val, *u64_iv;
uint32_t *u32_iv;
switch (ctx->type) { switch (ctx->type) {
case IV_NONE: case IV_NONE:
@@ -161,19 +162,24 @@ static int crypt_sector_iv_generate(struct crypt_sector_iv *ctx, uint64_t sector
break; break;
case IV_PLAIN: case IV_PLAIN:
memset(ctx->iv, 0, ctx->iv_size); memset(ctx->iv, 0, ctx->iv_size);
*(uint32_t *)ctx->iv = cpu_to_le32(sector & 0xffffffff); u32_iv = (void *)ctx->iv;
*u32_iv = cpu_to_le32(sector & 0xffffffff);
break; break;
case IV_PLAIN64: case IV_PLAIN64:
memset(ctx->iv, 0, ctx->iv_size); memset(ctx->iv, 0, ctx->iv_size);
*(uint64_t *)ctx->iv = cpu_to_le64(sector); u64_iv = (void *)ctx->iv;
*u64_iv = cpu_to_le64(sector);
break; break;
case IV_PLAIN64BE: case IV_PLAIN64BE:
memset(ctx->iv, 0, ctx->iv_size); memset(ctx->iv, 0, ctx->iv_size);
*(uint64_t *)&ctx->iv[ctx->iv_size - sizeof(uint64_t)] = cpu_to_be64(sector); /* iv_size is at least of size u64; usually it is 16 bytes */
u64_iv = (void *)&ctx->iv[ctx->iv_size - sizeof(uint64_t)];
*u64_iv = cpu_to_be64(sector);
break; break;
case IV_ESSIV: case IV_ESSIV:
memset(ctx->iv, 0, ctx->iv_size); memset(ctx->iv, 0, ctx->iv_size);
*(uint64_t *)ctx->iv = cpu_to_le64(sector); u64_iv = (void *)ctx->iv;
*u64_iv = cpu_to_le64(sector);
return crypt_cipher_encrypt(ctx->cipher, return crypt_cipher_encrypt(ctx->cipher,
ctx->iv, ctx->iv, ctx->iv_size, NULL, 0); ctx->iv, ctx->iv, ctx->iv_size, NULL, 0);
break; break;
@@ -184,7 +190,8 @@ static int crypt_sector_iv_generate(struct crypt_sector_iv *ctx, uint64_t sector
break; break;
case IV_EBOIV: case IV_EBOIV:
memset(ctx->iv, 0, ctx->iv_size); memset(ctx->iv, 0, ctx->iv_size);
*(uint64_t *)ctx->iv = cpu_to_le64(sector << ctx->shift); u64_iv = (void *)ctx->iv;
*u64_iv = cpu_to_le64(sector << ctx->shift);
return crypt_cipher_encrypt(ctx->cipher, return crypt_cipher_encrypt(ctx->cipher,
ctx->iv, ctx->iv, ctx->iv_size, NULL, 0); ctx->iv, ctx->iv, ctx->iv_size, NULL, 0);
break; break;

View File

@@ -25,6 +25,9 @@
/* to silent gcc -Wcast-qual for const cast */ /* to silent gcc -Wcast-qual for const cast */
#define CONST_CAST(x) (x)(uintptr_t) #define CONST_CAST(x) (x)(uintptr_t)
/* to silent clang -Wcast-align when working with byte arrays */
#define VOIDP_CAST(x) (x)(void*)
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
#ifndef ARRAY_SIZE #ifndef ARRAY_SIZE

View File

@@ -332,7 +332,7 @@ static int _dm_check_versions(struct crypt_device *cd, dm_target_type target_typ
(unsigned)target->version[1], (unsigned)target->version[1],
(unsigned)target->version[2]); (unsigned)target->version[2]);
} }
target = (struct dm_versions *)((char *) target + target->next); target = VOIDP_CAST(struct dm_versions *)((char *) target + target->next);
} while (last_target != target); } while (last_target != target);
r = 1; r = 1;

View File

@@ -264,8 +264,8 @@ static int TCRYPT_hdr_from_disk(struct crypt_device *cd,
*/ */
static void TCRYPT_swab_le(char *buf) static void TCRYPT_swab_le(char *buf)
{ {
uint32_t *l = (uint32_t*)&buf[0]; uint32_t *l = VOIDP_CAST(uint32_t*)&buf[0];
uint32_t *r = (uint32_t*)&buf[4]; uint32_t *r = VOIDP_CAST(uint32_t*)&buf[4];
*l = swab32(*l); *l = swab32(*l);
*r = swab32(*r); *r = swab32(*r);
} }

View File

@@ -67,12 +67,13 @@ void crypt_safe_free(void *data)
{ {
struct safe_allocation *alloc; struct safe_allocation *alloc;
volatile size_t *s; volatile size_t *s;
void *p;
if (!data) if (!data)
return; return;
alloc = (struct safe_allocation *) p = (char *)data - offsetof(struct safe_allocation, data);
((char *)data - offsetof(struct safe_allocation, data)); alloc = (struct safe_allocation *)p;
crypt_safe_memzero(data, alloc->size); crypt_safe_memzero(data, alloc->size);
@@ -85,13 +86,14 @@ void *crypt_safe_realloc(void *data, size_t size)
{ {
struct safe_allocation *alloc; struct safe_allocation *alloc;
void *new_data; void *new_data;
void *p;
new_data = crypt_safe_alloc(size); new_data = crypt_safe_alloc(size);
if (new_data && data) { if (new_data && data) {
alloc = (struct safe_allocation *) p = (char *)data - offsetof(struct safe_allocation, data);
((char *)data - offsetof(struct safe_allocation, data)); alloc = (struct safe_allocation *)p;
if (size > alloc->size) if (size > alloc->size)
size = alloc->size; size = alloc->size;