From d2ae3a4ac629609fc29c16c53757767fd2963b88 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 31 May 2020 12:01:27 +0200 Subject: [PATCH] list-c++, set-c++, oset-c++, map-c++, omap-c++: Don't fool the compiler. Reported by Akim Demaille in . * lib/gl_list.hh (gl_List::iterator::next): Avoid a reinterpret_cast. * lib/gl_set.hh (gl_Set::iterator::next): Likewise. * lib/gl_oset.hh (gl_OSet::iterator::next): Likewise. * lib/gl_map.hh (gl_Map::iterator::next): Likewise. * lib/gl_omap.hh (gl_OMap::iterator::next): Likewise. --- ChangeLog | 11 +++++++++++ lib/gl_list.hh | 16 ++++++++++++++-- lib/gl_map.hh | 12 +++++++++++- lib/gl_omap.hh | 12 +++++++++++- lib/gl_oset.hh | 8 +++++++- lib/gl_set.hh | 8 +++++++- 6 files changed, 61 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index e8133349c1..321d5e28c3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2020-05-31 Bruno Haible + + list-c++, set-c++, oset-c++, map-c++, omap-c++: Don't fool the compiler. + Reported by Akim Demaille in + . + * lib/gl_list.hh (gl_List::iterator::next): Avoid a reinterpret_cast. + * lib/gl_set.hh (gl_Set::iterator::next): Likewise. + * lib/gl_oset.hh (gl_OSet::iterator::next): Likewise. + * lib/gl_map.hh (gl_Map::iterator::next): Likewise. + * lib/gl_omap.hh (gl_OMap::iterator::next): Likewise. + 2020-05-30 Bruno Haible wmemchr: Relicense under LGPLv2+. diff --git a/lib/gl_list.hh b/lib/gl_list.hh index b19bda71b1..8b0ccad1ba 100644 --- a/lib/gl_list.hh +++ b/lib/gl_list.hh @@ -351,13 +351,25 @@ public: the iterator and returns true. Otherwise, returns false. */ bool next (ELTYPE *& elt) - { return gl_list_iterator_next (&_state, reinterpret_cast(&elt), NULL); } + { + const void *next_elt; + bool has_next = gl_list_iterator_next (&_state, &next_elt, NULL); + if (has_next) + elt = static_cast(next_elt); + return has_next; + } /* If there is a next element, stores the next element in ELT, stores its node in *NODEP if NODEP is non-NULL, advances the iterator and returns true. Otherwise, returns false. */ bool next (ELTYPE *& elt, gl_list_node_t *nodep) - { return gl_list_iterator_next (&_state, reinterpret_cast(&elt), nodep); } + { + const void *next_elt; + bool has_next = gl_list_iterator_next (&_state, &next_elt, nodep); + if (has_next) + elt = static_cast(next_elt); + return has_next; + } ~iterator () { gl_list_iterator_free (&_state); } diff --git a/lib/gl_map.hh b/lib/gl_map.hh index f3d0a46b94..082694fe6b 100644 --- a/lib/gl_map.hh +++ b/lib/gl_map.hh @@ -143,7 +143,17 @@ public: /* If there is a next pair, stores the next pair in KEY and VALUE, advance the iterator, and returns true. Otherwise, returns false. */ bool next (KEYTYPE *& key, VALUETYPE *& value) - { return gl_map_iterator_next (&_state, reinterpret_cast(&key), reinterpret_cast(&value)); } + { + const void *next_key; + const void *next_value; + bool has_next = gl_map_iterator_next (&_state, &next_key, &next_value); + if (has_next) + { + key = static_cast(next_key); + value = static_cast(next_value); + } + return has_next; + } ~iterator () { gl_map_iterator_free (&_state); } diff --git a/lib/gl_omap.hh b/lib/gl_omap.hh index 15d81041d4..1f892e46f8 100644 --- a/lib/gl_omap.hh +++ b/lib/gl_omap.hh @@ -150,7 +150,17 @@ public: /* If there is a next pair, stores the next pair in KEY and VALUE, advances the iterator, and returns true. Otherwise, returns false. */ bool next (KEYTYPE *& key, VALUETYPE *& value) - { return gl_omap_iterator_next (&_state, reinterpret_cast(&key), reinterpret_cast(&value)); } + { + const void *next_key; + const void *next_value; + bool has_next = gl_omap_iterator_next (&_state, &next_key, &next_value); + if (has_next) + { + key = static_cast(next_key); + value = static_cast(next_value); + } + return has_next; + } ~iterator () { gl_omap_iterator_free (&_state); } diff --git a/lib/gl_oset.hh b/lib/gl_oset.hh index 16da0dd2b3..b78ef52802 100644 --- a/lib/gl_oset.hh +++ b/lib/gl_oset.hh @@ -121,7 +121,13 @@ public: /* If there is a next element, stores the next element in ELT, advances the iterator and returns true. Otherwise, returns false. */ bool next (ELTYPE *& elt) - { return gl_oset_iterator_next (&_state, reinterpret_cast(&elt)); } + { + const void *next_elt; + bool has_next = gl_oset_iterator_next (&_state, &next_elt); + if (has_next) + elt = static_cast(next_elt); + return has_next; + } ~iterator () { gl_oset_iterator_free (&_state); } diff --git a/lib/gl_set.hh b/lib/gl_set.hh index 357e14175a..3b64bc5b1a 100644 --- a/lib/gl_set.hh +++ b/lib/gl_set.hh @@ -114,7 +114,13 @@ public: /* If there is a next element, stores the next element in ELT, advances the iterator and returns true. Otherwise, returns false. */ bool next (ELTYPE *& elt) - { return gl_set_iterator_next (&_state, reinterpret_cast(&elt)); } + { + const void *next_elt; + bool has_next = gl_set_iterator_next (&_state, &next_elt); + if (has_next) + elt = static_cast(next_elt); + return has_next; + } ~iterator () { gl_set_iterator_free (&_state); } -- 2.39.5