Skip to content

Commit 777bfc4

Browse files
authored
Merge pull request #141 from ericu/master
Escape % to make format strings safe.
2 parents 09bc6f6 + 5c9581d commit 777bfc4

File tree

1 file changed

+11
-4
lines changed
  • src/Graphics/UI/FLTK/LowLevel

1 file changed

+11
-4
lines changed

src/Graphics/UI/FLTK/LowLevel/Ask.chs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ enum BeepType {
3030
};
3131
#endc
3232

33+
escapePercent :: T.Text -> T.Text
34+
escapePercent t =
35+
let helper [] = []
36+
helper ('%' : cs) = '%' : '%' : helper cs
37+
helper (c : cs) = c : helper cs
38+
in T.pack $ helper $ T.unpack t
39+
3340
{#enum BeepType {} deriving (Eq, Show, Ord) #}
3441

3542
{# fun flc_beep as flBeep' {} -> `()' #}
@@ -41,7 +48,7 @@ flBeep (Just bt) = flBeepType' (fromIntegral (fromEnum bt))
4148
{# fun flc_input_with_deflt as flInput' { `CString',`CString' } -> `CString' #}
4249
flInput :: T.Text -> Maybe T.Text -> IO (Maybe T.Text)
4350
flInput msg defaultMsg = do
44-
msgC <- copyTextToCString msg
51+
msgC <- copyTextToCString $ escapePercent msg
4552
let def = fromMaybe T.empty defaultMsg
4653
defaultC <- copyTextToCString def
4754
r <- flInput' msgC defaultC
@@ -50,7 +57,7 @@ flInput msg defaultMsg = do
5057
{# fun flc_choice as flChoice' { `CString',`CString',`CString',`CString' } -> `CInt' #}
5158
flChoice :: T.Text -> T.Text -> Maybe T.Text -> Maybe T.Text -> IO Int
5259
flChoice msg b0 b1 b2 = do
53-
msgC <- copyTextToCString msg
60+
msgC <- copyTextToCString $ escapePercent msg
5461
b0C <- copyTextToCString b0
5562
let stringOrNull t = maybe (return nullPtr) copyTextToCString t
5663
b1C <- stringOrNull b1
@@ -61,12 +68,12 @@ flChoice msg b0 b1 b2 = do
6168
{# fun flc_password as flPassword' { `CString' } -> `CString' #}
6269
flPassword :: T.Text -> IO (Maybe T.Text)
6370
flPassword msg = do
64-
r <- copyTextToCString msg >>= flPassword'
71+
r <- copyTextToCString (escapePercent msg) >>= flPassword'
6572
cStringToMaybeText r
6673

6774
{# fun flc_message as flMessage' { `CString' } -> `()' #}
6875
flMessage :: T.Text -> IO ()
69-
flMessage t = copyTextToCString t >>= flMessage'
76+
flMessage t = copyTextToCString (escapePercent t) >>= flMessage'
7077

7178
{# fun flc_alert as flAlert' { `CString' } -> `()' #}
7279
flAlert :: T.Text -> IO ()

0 commit comments

Comments
 (0)