From 38fb30d7420a4b01f99cee31baa8c3990a1d1c5f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 6 Aug 2014 13:34:49 -0400 Subject: [PATCH] Use atexit() only if it's safe to call from shared library Apparently they are not (advertised as?) safe on BSD systems. We ignore the case of static libraries. Whitelisted on glibc, Android, and MSVC / mingw. https://bugs.freedesktop.org/show_bug.cgi?id=82246 --- src/hb-common.cc | 4 ++-- src/hb-ft.cc | 2 +- src/hb-private.hh | 25 +++++++++++++++++++++++++ src/hb-shape.cc | 2 +- src/hb-shaper.cc | 2 +- src/hb-uniscribe.cc | 2 +- 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/hb-common.cc b/src/hb-common.cc index a089e52cd..a6a514479 100644 --- a/src/hb-common.cc +++ b/src/hb-common.cc @@ -234,7 +234,7 @@ struct hb_language_item_t { static hb_language_item_t *langs; -#ifdef HAVE_ATEXIT +#ifdef HB_USE_ATEXIT static inline void free_langs (void) { @@ -269,7 +269,7 @@ retry: goto retry; } -#ifdef HAVE_ATEXIT +#ifdef HB_USE_ATEXIT if (!first_lang) atexit (free_langs); /* First person registers atexit() callback. */ #endif diff --git a/src/hb-ft.cc b/src/hb-ft.cc index 44e0b0bc2..16bf51f56 100644 --- a/src/hb-ft.cc +++ b/src/hb-ft.cc @@ -455,7 +455,7 @@ retry: goto retry; } -#ifdef HAVE_ATEXIT +#ifdef HB_USE_ATEXIT atexit (free_ft_library); /* First person registers atexit() callback. */ #endif } diff --git a/src/hb-private.hh b/src/hb-private.hh index 5a4ca6991..1b39e5731 100644 --- a/src/hb-private.hh +++ b/src/hb-private.hh @@ -130,6 +130,31 @@ # define STRICT #endif +#if HAVE_ATEXIT +/* atexit() is only safe to be called from shared libraries on certain + * platforms. Whitelist. + * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */ +# if defined(__linux) && defined(__GLIBC_PREREQ) +# if __GLIBC_PREREQ(2,3) +/* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */ +# define HB_USE_ATEXIT 1 +# endif +# elif defined(_MSC_VER) || defined(__MINGW32__) +/* For MSVC: + * http://msdn.microsoft.com/en-ca/library/tze57ck3.aspx + * http://msdn.microsoft.com/en-ca/library/zk17ww08.aspx + * mingw32 headers say atexit is safe to use in shared libraries. + */ +# define HB_USE_ATEXIT 1 +# elif defined(__ANDROID__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) +/* This was fixed in Android NKD r8 or r8b: + * https://code.google.com/p/android/issues/detail?id=6455 + * which introduced GCC 4.6: + * https://developer.android.com/tools/sdk/ndk/index.html + */ +# define HB_USE_ATEXIT 1 +# endif +#endif /* Basics */ diff --git a/src/hb-shape.cc b/src/hb-shape.cc index 812cf5919..74747f88b 100644 --- a/src/hb-shape.cc +++ b/src/hb-shape.cc @@ -320,7 +320,7 @@ retry: goto retry; } -#ifdef HAVE_ATEXIT +#ifdef HB_USE_ATEXIT atexit (free_static_shaper_list); /* First person registers atexit() callback. */ #endif } diff --git a/src/hb-shaper.cc b/src/hb-shaper.cc index 44f718aa7..6ae260138 100644 --- a/src/hb-shaper.cc +++ b/src/hb-shaper.cc @@ -100,7 +100,7 @@ retry: goto retry; } -#ifdef HAVE_ATEXIT +#ifdef HB_USE_ATEXIT atexit (free_static_shapers); /* First person registers atexit() callback. */ #endif } diff --git a/src/hb-uniscribe.cc b/src/hb-uniscribe.cc index 6bf87a5f4..0a2b48163 100644 --- a/src/hb-uniscribe.cc +++ b/src/hb-uniscribe.cc @@ -245,7 +245,7 @@ retry: goto retry; } -#ifdef HAVE_ATEXIT +#ifdef HB_USE_ATEXIT atexit (free_uniscribe_funcs); /* First person registers atexit() callback. */ #endif }