From f8fdd180c8ce9f95507dc16873186743e5bc6d03 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Sat, 30 Sep 2023 15:36:08 +1300 Subject: [PATCH] Attempted segfault fix. --- Graphics/Text/Font/Choose/CharSet.hs | 11 ++++++----- Graphics/Text/Font/Choose/FontSet.hs | 2 -- cbits/pattern.c | 9 ++++++++- fontconfig-pure.cabal | 2 +- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Graphics/Text/Font/Choose/CharSet.hs b/Graphics/Text/Font/Choose/CharSet.hs index 91d4326..ae86ffc 100644 --- a/Graphics/Text/Font/Choose/CharSet.hs +++ b/Graphics/Text/Font/Choose/CharSet.hs @@ -8,7 +8,8 @@ import Data.Word (Word32) import Foreign.Ptr import Control.Exception (bracket) import Control.Monad (forM) -import Foreign.Marshal.Alloc (alloca, allocaBytes) +import Foreign.Marshal.Alloc (alloca) +import Foreign.Marshal.Array (allocaArray) import GHC.Base (unsafeChr) import Data.Char (ord, isHexDigit) import Numeric (readHex) @@ -61,10 +62,10 @@ foreign import ccall "FcCharSetAddChar" fcCharSetAddChar :: CharSet_ -> Word32 - thawCharSet :: CharSet_ -> IO CharSet thawCharSet chars' | chars' == nullPtr = return Set.empty - | otherwise = allocaBytes fcCHARSET_MAP_SIZE $ \iter' -> alloca $ \next' -> do + | otherwise = allocaArray fcCHARSET_MAP_SIZE $ \iter' -> alloca $ \next' -> do first <- fcCharSetFirstPage chars' iter' next' let go = do - ch <- fcCharSetNextPage chars' iter' next'; + ch <- fcCharSetNextPage chars' iter' next' if ch == maxBound then return [] else do chs <- go @@ -72,9 +73,9 @@ thawCharSet chars' if first == maxBound then return Set.empty else do rest <- go return $ Set.fromList $ map (unsafeChr . fromIntegral) (first:rest) -foreign import ccall "FcCharSetFirstPage" fcCharSetFirstPage :: +foreign import ccall "my_FcCharSetFirstPage" fcCharSetFirstPage :: CharSet_ -> Ptr Word32 -> Ptr Word32 -> IO Word32 -foreign import ccall "FcCharSetNextPage" fcCharSetNextPage :: +foreign import ccall "my_FcCharSetNextPage" fcCharSetNextPage :: CharSet_ -> Ptr Word32 -> Ptr Word32 -> IO Word32 foreign import ccall "my_FcCHARSET_MAP_SIZE" fcCHARSET_MAP_SIZE :: Int diff --git a/Graphics/Text/Font/Choose/FontSet.hs b/Graphics/Text/Font/Choose/FontSet.hs index 044a727..7c19b0e 100644 --- a/Graphics/Text/Font/Choose/FontSet.hs +++ b/Graphics/Text/Font/Choose/FontSet.hs @@ -55,8 +55,6 @@ 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? n <- get_fontSet_nfont fonts' if n == 0 then return [] else diff --git a/cbits/pattern.c b/cbits/pattern.c index 6754bda..28184bf 100644 --- a/cbits/pattern.c +++ b/cbits/pattern.c @@ -2,7 +2,14 @@ #include int my_FcCHARSET_MAP_SIZE() { - return FC_CHARSET_MAP_SIZE*sizeof(FcChar32); + return FC_CHARSET_MAP_SIZE; +} + +FcChar32 my_FcCharSetFirstPage(const FcCharSet *a, FcChar32 *map, FcChar32 *next) { + return FcCharSetFirstPage(a, map, next); +} +FcChar32 my_FcCharSetNextPage(const FcCharSet *a, FcChar32 *map, FcChar32 *next) { + return FcCharSetNextPage(a, map, next); } FcBool my_FcPatternAdd(FcPattern *p, const char *object, diff --git a/fontconfig-pure.cabal b/fontconfig-pure.cabal index 3e49167..277e35f 100644 --- a/fontconfig-pure.cabal +++ b/fontconfig-pure.cabal @@ -10,7 +10,7 @@ name: fontconfig-pure -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change -version: 0.1.1.1 +version: 0.2.0.0 -- A short (one-line) description of the package. synopsis: Pure-functional language bindings to FontConfig -- 2.30.2