From 8c6d796da7e050bb5defd863170828a1db7146c9 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Tue, 13 Dec 2022 14:56:17 +1300 Subject: [PATCH] Fix remaining segfaults. --- Graphics/Text/Font/Choose/FontSet.hs | 16 ++++++++++------ cbits/pattern.c | 9 +++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Graphics/Text/Font/Choose/FontSet.hs b/Graphics/Text/Font/Choose/FontSet.hs index 37a1daf..ece57e6 100644 --- a/Graphics/Text/Font/Choose/FontSet.hs +++ b/Graphics/Text/Font/Choose/FontSet.hs @@ -52,15 +52,19 @@ withFontSets' (fonts:fontss) i fontss' cb = withFontSet fonts $ \fonts' -> do thawFontSet :: FontSet_ -> IO FontSet thawFontSet fonts' = do + -- Very hacky, but these debug statements must be in here to avoid segfaults. + -- FIXME: Is there an alternative? + print "a" n <- get_fontSet_nfont fonts' - array <- get_fontSet_fonts fonts' - if n == 0 || array == nullPtr - then return [] + print "b" + if n == 0 then return [] else do - list <- peekArray n array - forM list (thawPattern_ . pure) + print "c" + ret <- forM [0..pred n] (thawPattern_ . get_fontSet_font fonts') + print "d" + return ret foreign import ccall "get_fontSet_nfont" get_fontSet_nfont :: FontSet_ -> IO Int -foreign import ccall "get_fontSet_fonts" get_fontSet_fonts :: FontSet_ -> IO (Ptr Pattern_) +foreign import ccall "get_fontSet_font" get_fontSet_font :: FontSet_ -> Int -> IO Pattern_ thawFontSet_ :: IO FontSet_ -> IO FontSet thawFontSet_ cb = bracket (throwNull <$> cb) fcFontSetDestroy thawFontSet diff --git a/cbits/pattern.c b/cbits/pattern.c index c947988..0eb7512 100644 --- a/cbits/pattern.c +++ b/cbits/pattern.c @@ -1,4 +1,5 @@ #include +#include int my_FcCHARSET_MAP_SIZE() { return FC_CHARSET_MAP_SIZE; @@ -31,6 +32,10 @@ int get_fontSet_nfont(FcFontSet *fonts) { return fonts->nfont; } -FcPattern **get_fontSet_fonts(FcFontSet *fonts) { - return fonts->fonts; +FcPattern *get_fontSet_font(FcFontSet *fonts, int i) { + if (i < 0) return NULL; + if (i >= fonts->nfont) return NULL; + if (i >= fonts->sfont) return NULL; + if (fonts->fonts == NULL) return NULL; + return fonts->fonts[i]; } -- 2.30.2