mroonga: correct offsetof calculation

Using a null pointer as a basic for calculation
was undefined behaviour.
This commit is contained in:
Daniel Black 2025-03-28 18:11:09 +11:00 committed by Sergei Golubchik
commit 2ba49bd804
2 changed files with 9 additions and 9 deletions

View file

@ -969,8 +969,8 @@ calc_rec_size(grn_table_flags flags, uint32_t max_n_subrecs, uint32_t range_size
*subrec_size = range_size + sizeof(uint32_t) + sizeof(uint32_t);
break;
}
*value_size = (uintptr_t)GRN_RSET_SUBRECS_NTH((((grn_rset_recinfo *)0)->subrecs),
*subrec_size, max_n_subrecs);
*value_size = (uintptr_t) GRN_RSET_SUBRECS_NTH(offsetof(grn_rset_recinfo, subrecs),
*subrec_size, max_n_subrecs);
} else {
*value_size = range_size;
}

View file

@ -1727,15 +1727,15 @@ grn_io_hash_calculate_entry_size(uint32_t key_size, uint32_t value_size,
{
if (flags & GRN_OBJ_KEY_VAR_SIZE) {
if (flags & GRN_OBJ_KEY_LARGE) {
return (uintptr_t)((grn_io_hash_entry_large *)0)->value + value_size;
return offsetof(grn_io_hash_entry_large, value) + value_size;
} else {
return (uintptr_t)((grn_io_hash_entry_normal *)0)->value + value_size;
return offsetof(grn_io_hash_entry_normal, value) + value_size;
}
} else {
if (key_size == sizeof(uint32_t)) {
return (uintptr_t)((grn_plain_hash_entry *)0)->value + value_size;
return offsetof(grn_plain_hash_entry, value) + value_size;
} else {
return (uintptr_t)((grn_rich_hash_entry *)0)->key_and_value
return offsetof(grn_rich_hash_entry, key_and_value)
+ key_size + value_size;
}
}
@ -1865,12 +1865,12 @@ grn_tiny_hash_calculate_entry_size(uint32_t key_size, uint32_t value_size,
{
uint32_t entry_size;
if (flags & GRN_OBJ_KEY_VAR_SIZE) {
entry_size = (uintptr_t)((grn_tiny_hash_entry *)0)->value + value_size;
entry_size = offsetof(grn_tiny_hash_entry, value) + value_size;
} else {
if (key_size == sizeof(uint32_t)) {
entry_size = (uintptr_t)((grn_plain_hash_entry *)0)->value + value_size;
entry_size = offsetof(grn_plain_hash_entry, value) + value_size;
} else {
entry_size = (uintptr_t)((grn_rich_hash_entry *)0)->key_and_value
entry_size = offsetof(grn_rich_hash_entry, key_and_value)
+ key_size + value_size;
}
}