Update HarfBuzz to 0.9.40

Most of ubsan.patch seems to have been applied upstream, and I can’t
reproduce the issue referenced for the remaining bits, anyway it is
better to push such changes upstream first.

Change-Id: Ie56786c01c06d3542052cd91e36d1f707092beba
Reviewed-on: https://gerrit.libreoffice.org/15643
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/download.lst b/download.lst
index 7c0b781..01ed4cf 100644
--- a/download.lst
+++ b/download.lst
@@ -60,8 +60,8 @@ export GLEW_TARBALL := 594eb47b4b1210e25438d51825404d5a-glew-1.10.0.zip
export GLM_TARBALL := bae83fa5dc7f081768daace6e199adc3-glm-0.9.4.6-libreoffice.zip
export GRAPHITE_MD5SUM := 2ef839348fe28e3b923bf8cced440227
export GRAPHITE_TARBALL := graphite2-1.2.4.tgz
export HARFBUZZ_MD5SUM := a4a9b548577e2ee22f0887937da5fd6c
export HARFBUZZ_TARBALL := harfbuzz-0.9.23.tar.bz2
export HARFBUZZ_MD5SUM := 0e27e531f4c4acff601ebff0957755c2
export HARFBUZZ_TARBALL := harfbuzz-0.9.40.tar.bz2
export HSQLDB_TARBALL := 17410483b5b5f267aa18b7e00b65e6e0-hsqldb_1_8_0.zip
export HUNSPELL_TARBALL := 4967da60b23413604c9e563beacc63b4-hunspell-1.3.3.tar.gz
export HYPHEN_TARBALL := 5ade6ae2a99bc1e9e57031ca88d36dad-hyphen-2.8.8.tar.gz
diff --git a/external/harfbuzz/ExternalProject_harfbuzz.mk b/external/harfbuzz/ExternalProject_harfbuzz.mk
index 2514124..302eed7 100644
--- a/external/harfbuzz/ExternalProject_harfbuzz.mk
+++ b/external/harfbuzz/ExternalProject_harfbuzz.mk
@@ -25,11 +25,13 @@ $(call gb_ExternalProject_get_state_target,harfbuzz,build) :
		./configure \
			--enable-static \
			--disable-shared \
			--disable-gtk-doc \
			--with-pic \
			--with-icu=yes \
			--with-freetype=no \
			--with-cairo=no \
			--with-glib=no \
			$(if $(VERBOSE)$(verbose),--disable-silent-rules,--enable-silent-rules) \
			$(if $(CROSS_COMPILING),--build=$(BUILD_PLATFORM) --host=$(HOST_PLATFORM)) \
		&& (cd $(EXTERNAL_WORKDIR)/src && $(MAKE)) \
	)
diff --git a/external/harfbuzz/UnpackedTarball_harfbuzz.mk b/external/harfbuzz/UnpackedTarball_harfbuzz.mk
index 9a5560f..5450a81 100644
--- a/external/harfbuzz/UnpackedTarball_harfbuzz.mk
+++ b/external/harfbuzz/UnpackedTarball_harfbuzz.mk
@@ -13,10 +13,6 @@ $(eval $(call gb_UnpackedTarball_set_tarball,harfbuzz,$(HARFBUZZ_TARBALL),,harfb

$(eval $(call gb_UnpackedTarball_set_patchlevel,harfbuzz,0))

$(eval $(call gb_UnpackedTarball_add_patches,harfbuzz, \
    external/harfbuzz/ubsan.patch \
))

ifneq ($(ENABLE_RUNTIME_OPTIMIZATIONS),TRUE)
$(eval $(call gb_UnpackedTarball_add_patches,harfbuzz, \
    external/harfbuzz/harfbuzz-rtti.patch \
diff --git a/external/harfbuzz/ubsan.patch b/external/harfbuzz/ubsan.patch
deleted file mode 100644
index 6c301c4..0000000
--- a/external/harfbuzz/ubsan.patch
+++ /dev/null
@@ -1,86 +0,0 @@
--- src/hb-object-private.hh
+++ src/hb-object-private.hh
@@ -131,7 +131,7 @@
   }
 
   inline bool destroy (void) {
-    if (unlikely (!this || this->is_inert ()))
+    if (unlikely (is_inert ()))
       return false;
     if (ref_count.dec () != 1)
       return false;
@@ -160,13 +160,12 @@
   }
 
   inline void trace (const char *function) const {
-    if (unlikely (!this)) return;
     /* TODO We cannot use DEBUG_MSG_FUNC here since that one currently only
      * prints the class name and throws away the template info. */
     DEBUG_MSG (OBJECT, (void *) this,
 	       "%s refcount=%d",
 	       function,
-	       this ? ref_count.ref_count : 0);
+	       ref_count.ref_count);
   }
 
   private:
@@ -179,7 +179,7 @@
 template <typename Type>
 static inline void hb_object_trace (const Type *obj, const char *function)
 {
-  obj->header.trace (function);
+  if (likely (obj)) obj->header.trace (function);
 }
 template <typename Type>
 static inline Type *hb_object_create (void)
@@ -204,7 +204,7 @@
 static inline bool hb_object_destroy (Type *obj)
 {
   hb_object_trace (obj, HB_FUNC);
-  return obj->header.destroy ();
+  return likely (obj) && obj->header.destroy ();
 }
 template <typename Type>
 static inline bool hb_object_set_user_data (Type               *obj,
--- src/hb-ot-map-private.hh
+++ src/hb-ot-map-private.hh
@@ -52,8 +52,12 @@
     unsigned int needs_fallback : 1;
     unsigned int auto_zwj : 1;
 
-    static int cmp (const feature_map_t *a, const feature_map_t *b)
-    { return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0; }
+    static int cmp (const void *va, const void *vb)
+    {
+      const feature_map_t *a = static_cast<const feature_map_t *>(va);
+      const feature_map_t *b = static_cast<const feature_map_t *>(vb);
+      return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0;
+    }
   };
 
   struct lookup_map_t {
--- src/hb-ot-tag.cc
+++ src/hb-ot-tag.cc
@@ -591,9 +591,11 @@
 };
 
 static int
-lang_compare_first_component (const char *a,
-			      const char *b)
+lang_compare_first_component (const void *va,
+			      const void *vb)
 {
+  const char *a = static_cast<const char *>(va);
+  const char *b = static_cast<const char *>(vb);
   unsigned int da, db;
   const char *p;
 
@@ -641,7 +643,7 @@
   /* Find a language matching in the first component */
   lang_tag = (LangTag *) bsearch (lang_str, ot_languages,
 				  ARRAY_LENGTH (ot_languages), sizeof (LangTag),
-				  (hb_compare_func_t) lang_compare_first_component);
+				  lang_compare_first_component);
   if (lang_tag)
     return lang_tag->tag;