From 7595fa2d9a15518a9ca41f6892a17fd36858e5af Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 17 Nov 2022 15:19:29 -0700 Subject: [PATCH] [map] Fix copy-assignment operator Ouch! --- src/hb-map.hh | 2 +- src/test-map.cc | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/hb-map.hh b/src/hb-map.hh index ca9224ddc..3886449c8 100644 --- a/src/hb-map.hh +++ b/src/hb-map.hh @@ -45,7 +45,7 @@ struct hb_hashmap_t hb_hashmap_t (const hb_hashmap_t& o) : hb_hashmap_t () { resize (o.population); hb_copy (o, *this); } hb_hashmap_t (hb_hashmap_t&& o) : hb_hashmap_t () { hb_swap (*this, o); } - hb_hashmap_t& operator= (const hb_hashmap_t& o) { resize (o.population); hb_copy (o, *this); return *this; } + hb_hashmap_t& operator= (const hb_hashmap_t& o) { reset (); resize (o.population); hb_copy (o, *this); return *this; } hb_hashmap_t& operator= (hb_hashmap_t&& o) { hb_swap (*this, o); return *this; } hb_hashmap_t (std::initializer_list> lst) : hb_hashmap_t () diff --git a/src/test-map.cc b/src/test-map.cc index 91cf26e02..454a66907 100644 --- a/src/test-map.cc +++ b/src/test-map.cc @@ -249,15 +249,26 @@ main (int argc, char **argv) m.set (1, bytes); assert (m.has (1)); } - /* Test equality. */ + /* Test operators. */ { hb_map_t m1, m2, m3; m1.set (1, 2); + m1.set (2, 4); m2.set (1, 2); + m2.set (2, 4); m3.set (1, 3); + m3.set (3, 5); + assert (m1 == m2); assert (m1 != m3); assert (!(m2 == m3)); + + m2 = m3; + assert (m2.has (1)); + assert (!m2.has (2)); + assert (m2.has (3)); + + assert (m3.has (3)); } return 0;