Fix undefined behavior when hash function given null pointer

[test_subscriber-12] /opt/ros/master/src/eclipse-cyclonedds/cyclonedds/src/ddsrt/src/mh3.c:28:53: runtime error: applying zero offset to null pointer
[test_subscriber-12] SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /opt/ros/master/src/eclipse-cyclonedds/cyclonedds/src/ddsrt/src/mh3.c:28:53 in
Signed-off-by: Dan Rose <dan@digilabs.io>
This commit is contained in:
Dan Rose 2020-02-28 15:19:21 -06:00 committed by Erik Boasson
parent e8b0931798
commit ca4b5a368f
2 changed files with 30 additions and 27 deletions

View file

@ -56,7 +56,7 @@ static uint32_t sertopic_default_hash (const struct ddsi_sertopic *tpcmn)
{
const struct ddsi_sertopic_default *tp = (struct ddsi_sertopic_default *) tpcmn;
uint32_t h = 0;
h = ddsrt_mh3 (&tp->native_encoding_identifier, sizeof (tp->native_encoding_identifier), 0);
h = ddsrt_mh3 (&tp->native_encoding_identifier, sizeof (tp->native_encoding_identifier), h);
h = ddsrt_mh3 (&tp->type.m_size, sizeof (tp->type.m_size), h);
h = ddsrt_mh3 (&tp->type.m_align, sizeof (tp->type.m_align), h);
h = ddsrt_mh3 (&tp->type.m_flagset, sizeof (tp->type.m_flagset), h);

View file

@ -25,6 +25,8 @@ uint32_t ddsrt_mh3 (const void *key, size_t len, uint32_t seed)
const uint32_t c2 = 0x1b873593;
uint32_t h1 = seed;
if(len){
const uint32_t *blocks = (const uint32_t *) (data + nblocks * 4);
for (intptr_t i = -nblocks; i; i++)
{
@ -56,7 +58,8 @@ uint32_t ddsrt_mh3 (const void *key, size_t len, uint32_t seed)
k1 *= c2;
h1 ^= k1;
/* FALLS THROUGH */
};
}
}
/* finalization */
h1 ^= (uint32_t) len;