From: Richard Guenther Date: Wed, 27 Jan 2010 15:49:00 +0000 (+0000) Subject: re PR libstdc++/42832 (Revisit std::function for aliasing issues and efficiency) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=77597d3907e17a45b0d4825edb893c23a0715181;p=gcc.git re PR libstdc++/42832 (Revisit std::function for aliasing issues and efficiency) 2010-01-27 Richard Guenther PR libstdc++/42832 * include/std/functional (function<>::swap): Perform bytewise swap of _M_functor. * include/tr1/functional (function<>::swap): Likewise. From-SVN: r156290 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 4e5adf75e48..25cda7e571b 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2010-01-27 Richard Guenther + + PR libstdc++/42832 + * include/std/functional (function<>::swap): Perform bytewise + swap of _M_functor. + * include/tr1/functional (function<>::swap): Likewise. + 2010-01-27 Andreas Krebbel * config/abi/pre/gnu.ver: Avoid time_get pattern conflicts. diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index 29b19f67d0f..c390c3bbdcd 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -1940,9 +1940,16 @@ namespace std */ void swap(function& __x) { - _Any_data __old_functor = _M_functor; - _M_functor = __x._M_functor; - __x._M_functor = __old_functor; + /* We cannot perform direct assignments of the _M_functor + parts as they are of type _Any_data and have a different + dynamic type. Doing so would violate type-based aliasing + rules and lead to spurious miscompilations. + Instead perform a bytewise exchange of the memory of + both POD objects. + ??? A wordwise exchange honoring alignment of _M_functor + would be more efficient. See PR42845. */ + for (unsigned i = 0; i < sizeof (_M_functor._M_pod_data); ++i) + std::swap (_M_functor._M_pod_data[i], __x._M_functor._M_pod_data[i]); _Manager_type __old_manager = _M_manager; _M_manager = __x._M_manager; __x._M_manager = __old_manager; diff --git a/libstdc++-v3/include/tr1/functional b/libstdc++-v3/include/tr1/functional index 4825509aed7..2052f885de8 100644 --- a/libstdc++-v3/include/tr1/functional +++ b/libstdc++-v3/include/tr1/functional @@ -1904,9 +1904,16 @@ namespace tr1 */ void swap(function& __x) { - _Any_data __old_functor = _M_functor; - _M_functor = __x._M_functor; - __x._M_functor = __old_functor; + /* We cannot perform direct assignments of the _M_functor + parts as they are of type _Any_data and have a different + dynamic type. Doing so would violate type-based aliasing + rules and lead to spurious miscompilations. + Instead perform a bytewise exchange of the memory of + both POD objects. + ??? A wordwise exchange honoring alignment of _M_functor + would be more efficient. See PR42845. */ + for (unsigned i = 0; i < sizeof (_M_functor._M_pod_data); ++i) + std::swap (_M_functor._M_pod_data[i], __x._M_functor._M_pod_data[i]); _Manager_type __old_manager = _M_manager; _M_manager = __x._M_manager; __x._M_manager = __old_manager;