From 3e9e1cb9d300e13109206496d825351c4f41cc1c Mon Sep 17 00:00:00 2001 From: Sibi Prabakaran Date: Sun, 10 May 2020 19:43:56 +0530 Subject: Fix crashes/busy looping happening via index Right now, with the `StdinReader` plugin enabled - you can crash/cause busy looping of xmobar if the following html file is opened: ``` hello <fn=1>string</fn> ``` More details about this bug is here: https://github.com/jaor/xmobar/issues/442#issuecomment-625706001 This MR also fixes another bug which produces a crash in xmobar if you pass non integer items between fn: --- src/Xmobar/System/Utils.hs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'src/Xmobar/System') diff --git a/src/Xmobar/System/Utils.hs b/src/Xmobar/System/Utils.hs index 59c485c..33f221c 100644 --- a/src/Xmobar/System/Utils.hs +++ b/src/Xmobar/System/Utils.hs @@ -17,11 +17,16 @@ ------------------------------------------------------------------------------ -module Xmobar.System.Utils (expandHome, changeLoop, onSomeException) -where +module Xmobar.System.Utils + ( expandHome + , changeLoop + , onSomeException + , safeIndex + ) where import Control.Monad import Control.Concurrent.STM +import qualified Data.List.NonEmpty as NE import System.Environment import System.FilePath @@ -50,3 +55,18 @@ onSomeException :: IO a -> (SomeException -> IO b) -> IO a onSomeException io what = io `catch` \e -> do _ <- what e throwIO (e :: SomeException) +(!!?) :: [a] -> Int -> Maybe a +(!!?) xs i + | i < 0 = Nothing + | otherwise = go i xs + where + go :: Int -> [a] -> Maybe a + go 0 (x:_) = Just x + go j (_:ys) = go (j - 1) ys + go _ [] = Nothing +{-# INLINE (!!?) #-} + +safeIndex :: NE.NonEmpty a -> Int -> a +safeIndex xs index = case (NE.toList xs) !!? index of + Nothing -> NE.head xs + Just value -> value -- cgit v1.2.3