From 3ce310e939427216e461681e0a71c4883ff5bf03 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Wed, 27 Jul 2022 14:25:19 -0700 Subject: [PATCH] Plug space leaks when processing multiple files --- shellcheck.hs | 2 +- src/ShellCheck/Formatter/JSON.hs | 3 ++- src/ShellCheck/Formatter/JSON1.hs | 3 ++- src/ShellCheck/Formatter/TTY.hs | 3 ++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/shellcheck.hs b/shellcheck.hs index bf70445..a525251 100644 --- a/shellcheck.hs +++ b/shellcheck.hs @@ -225,7 +225,7 @@ runFormatter sys format options files = do f :: Status -> FilePath -> IO Status f status file = do newStatus <- process file `catch` handler file - return $ status `mappend` newStatus + return $! status `mappend` newStatus handler :: FilePath -> IOException -> IO Status handler file e = reportFailure file (show e) reportFailure file str = do diff --git a/src/ShellCheck/Formatter/JSON.hs b/src/ShellCheck/Formatter/JSON.hs index 7c26421..6b38532 100644 --- a/src/ShellCheck/Formatter/JSON.hs +++ b/src/ShellCheck/Formatter/JSON.hs @@ -23,6 +23,7 @@ module ShellCheck.Formatter.JSON (format) where import ShellCheck.Interface import ShellCheck.Formatter.Format +import Control.DeepSeq import Data.Aeson import Data.IORef import Data.Monoid @@ -103,7 +104,7 @@ collectResult ref cr sys = mapM_ f groups comments = crComments cr groups = groupWith sourceFile comments f :: [PositionedComment] -> IO () - f group = modifyIORef ref (\x -> comments ++ x) + f group = deepseq comments $ modifyIORef ref (\x -> comments ++ x) finish ref = do list <- readIORef ref diff --git a/src/ShellCheck/Formatter/JSON1.hs b/src/ShellCheck/Formatter/JSON1.hs index 54aad34..2169bf6 100644 --- a/src/ShellCheck/Formatter/JSON1.hs +++ b/src/ShellCheck/Formatter/JSON1.hs @@ -23,6 +23,7 @@ module ShellCheck.Formatter.JSON1 (format) where import ShellCheck.Interface import ShellCheck.Formatter.Format +import Control.DeepSeq import Data.Aeson import Data.IORef import Data.Monoid @@ -120,7 +121,7 @@ collectResult ref cr sys = mapM_ f groups result <- siReadFile sys (Just True) filename let contents = either (const "") id result let comments' = makeNonVirtual comments contents - modifyIORef ref (\x -> comments' ++ x) + deepseq comments' $ modifyIORef ref (\x -> comments' ++ x) finish ref = do list <- readIORef ref diff --git a/src/ShellCheck/Formatter/TTY.hs b/src/ShellCheck/Formatter/TTY.hs index 8dd90d4..e28696c 100644 --- a/src/ShellCheck/Formatter/TTY.hs +++ b/src/ShellCheck/Formatter/TTY.hs @@ -23,6 +23,7 @@ import ShellCheck.Fixer import ShellCheck.Interface import ShellCheck.Formatter.Format +import Control.DeepSeq import Control.Monad import Data.Array import Data.Foldable @@ -88,7 +89,7 @@ rankError err = (ranking, cSeverity $ pcComment err, cCode $ pcComment err) appendComments errRef comments max = do previous <- readIORef errRef let current = map (\x -> (rankError x, cCode $ pcComment x, cMessage $ pcComment x)) comments - writeIORef errRef . take max . nubBy equal . sort $ previous ++ current + writeIORef errRef $! force . take max . nubBy equal . sort $ previous ++ current where fst3 (x,_,_) = x equal x y = fst3 x == fst3 y