[Fontconfig] [PATCH 1/1] fc-lang: gracefully handle the case where the last language initial is < 'z'

Florent Rougon f.rougon at free.fr
Tue Jun 6 09:10:18 UTC 2017


FcLangSetIndex() contains code like this:

  low = fcLangCharSetRanges[firstChar - 'a'].begin;
  high = fcLangCharSetRanges[firstChar - 'a'].end;
  /* no matches */
  if (low > high)

The assumption behind this test didn't hold before this commit, unless
there is at least one language name that starts with 'z' (which is
thankfully the case in our world :-). If the last language name in
lexicographic order starts for instance with 'x', this change ensures
that fcLangCharSetRanges['y' - 'a'].begin and
     fcLangCharSetRanges['z' - 'a'].begin
are equal to INT_MAX, to make the above assumption correct in all cases.
---
 fc-lang/fc-lang.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/fc-lang/fc-lang.c b/fc-lang/fc-lang.c
index 38fc697..bec6a27 100644
--- a/fc-lang/fc-lang.c
+++ b/fc-lang/fc-lang.c
@@ -22,6 +22,8 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <limits.h>
+
 #include "fccharset.c"
 #include "fcstr.c"
 #include "fcserialize.c"
@@ -249,6 +251,10 @@ static int compare (const void *a, const void *b)
 #define MAX_LANG	    1024
 #define MAX_LANG_SET_MAP    ((MAX_LANG + 31) / 32)
 
+/* MAX_LANG must be strictly less than INT_MAX, otherwise some assumptions
+ * related to fcLangCharSetRanges won't hold. */
+FC_ASSERT_STATIC (MAX_LANG < INT_MAX);
+
 #define BitSet(map, i)   ((map)[(entries[i].id)>>5] |= ((FcChar32) 1 << ((entries[i].id) & 0x1f)))
 
 int
@@ -561,6 +567,9 @@ main (int argc FC_UNUSED, char **argv)
 	while (setRangeChar <= c && c <= 'z')
 	    setRangeStart[setRangeChar++ - 'a'] = i;
     }
+    while (setRangeChar <= 'z')
+	setRangeStart[setRangeChar++ - 'a'] = INT_MAX;
+
     for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
 	setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
     setRangeEnd[setRangeChar - 'a'] = i - 1;
-- 
2.11.0



More information about the Fontconfig mailing list