Skip to content

Commit 0573b08

Browse files
committed
dict: embed item structs in hash entries
Instead of duplicating an item struct's fields in the hash entry struct that wraps it, add the entire item struct as a field of the hash entry. This eliminates casts and ensures the type system understands what we're doing. Fixes strict aliasing violations in dcm_init(): we were assigning through a cast and then immediately reading through the original pointer. This happened to cause segfaults on RHEL 9 aarch64, but any compiler would be entitled to optimize away the read.
1 parent 574dc42 commit 0573b08

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/dicom-dict.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,7 @@ struct _DcmVRTable {
3333
};
3434

3535
struct _DcmVRTable_hash_entry {
36-
/* All of VRTable, plus a hash handle.
37-
*/
38-
DcmVR vr;
39-
char *str;
40-
DcmVRClass vr_class;
41-
size_t size;
42-
uint32_t capacity;
43-
int header_length;
44-
36+
struct _DcmVRTable table;
4537
UT_hash_handle hh;
4638
};
4739

@@ -175,10 +167,7 @@ struct _DcmAttribute {
175167
};
176168

177169
struct _DcmAttribute_hash_entry {
178-
uint32_t tag;
179-
DcmVRTag vr_tag;
180-
char keyword[63];
181-
170+
struct _DcmAttribute attr;
182171
UT_hash_handle hh;
183172
};
184173

@@ -5102,8 +5091,8 @@ void dcm_init(void)
51025091
}
51035092

51045093
entry = DCM_NEW(NULL, struct _DcmVRTable_hash_entry);
5105-
*((struct _DcmVRTable *)entry) = vr_table[i];
5106-
HASH_ADD_STR(vrtable_from_str_dict, str, entry);
5094+
entry->table = vr_table[i];
5095+
HASH_ADD_STR(vrtable_from_str_dict, table.str, entry);
51075096
}
51085097
}
51095098

@@ -5120,13 +5109,13 @@ void dcm_init(void)
51205109
"%8X (%s) registered previously as '%s'",
51215110
attribute_table[i].tag,
51225111
attribute_table[i].keyword,
5123-
entry->keyword);
5112+
entry->attr.keyword);
51245113
return;
51255114
}
51265115

51275116
entry = DCM_NEW(NULL, struct _DcmAttribute_hash_entry);
5128-
*((struct _DcmAttribute *)entry) = attribute_table[i];
5129-
HASH_ADD_INT(attribute_from_tag_dict, tag, entry);
5117+
entry->attr = attribute_table[i];
5118+
HASH_ADD_INT(attribute_from_tag_dict, attr.tag, entry);
51305119
}
51315120
}
51325121

@@ -5152,8 +5141,8 @@ void dcm_init(void)
51525141
}
51535142

51545143
entry = DCM_NEW(NULL, struct _DcmAttribute_hash_entry);
5155-
*((struct _DcmAttribute *)entry) = attribute_table[i];
5156-
HASH_ADD_STR(attribute_from_keyword_dict, keyword, entry);
5144+
entry->attr = attribute_table[i];
5145+
HASH_ADD_STR(attribute_from_keyword_dict, attr.keyword, entry);
51575146
}
51585147
}
51595148

@@ -5171,7 +5160,10 @@ static const struct _DcmVRTable *vrtable_from_vr(const char *vr)
51715160

51725161
HASH_FIND_STR(vrtable_from_str_dict, vr, entry);
51735162

5174-
return (const struct _DcmVRTable *)entry;
5163+
if (entry == NULL) {
5164+
return NULL;
5165+
}
5166+
return &entry->table;
51755167
}
51765168

51775169

@@ -5264,7 +5256,10 @@ static const struct _DcmAttribute *attribute_from_tag(uint32_t tag)
52645256

52655257
HASH_FIND_INT(attribute_from_tag_dict, &tag, entry);
52665258

5267-
return (const struct _DcmAttribute *)entry;
5259+
if (entry == NULL) {
5260+
return NULL;
5261+
}
5262+
return &entry->attr;
52685263
}
52695264

52705265

@@ -5362,7 +5357,10 @@ static const struct _DcmAttribute *attribute_from_keyword(const char *keyword)
53625357

53635358
HASH_FIND_STR(attribute_from_keyword_dict, keyword, entry);
53645359

5365-
return (const struct _DcmAttribute *)entry;
5360+
if (entry == NULL) {
5361+
return NULL;
5362+
}
5363+
return &entry->attr;
53665364
}
53675365

53685366

0 commit comments

Comments
 (0)