Skip to content

Commit

Permalink
Common.hs: avoid using Opt.auto to avoid overflows going silent
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Aug 8, 2024
1 parent afdd769 commit 0789ab6
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

{- HLINT ignore "Move brackets to avoid $" -}
{- HLINT ignore "Use <$>" -}
Expand All @@ -28,6 +29,7 @@ import qualified Ouroboros.Network.Protocol.LocalStateQuery.Type as Consensus
import Control.Monad (mfilter)
import qualified Data.Aeson as Aeson
import Data.Bifunctor
import Data.Bits (Bits, toIntegralSized)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Char8 as BSC
Expand All @@ -43,7 +45,7 @@ import qualified Data.Text as Text
import Data.Time.Clock (UTCTime)
import Data.Time.Format (defaultTimeLocale, parseTimeOrError)
import Data.Word
import GHC.Natural (Natural)
import GHC.Natural (Natural, naturalToWordMaybe)
import Network.Socket (PortNumber)
import Options.Applicative hiding (help, str)
import qualified Options.Applicative as Opt
Expand Down Expand Up @@ -3181,9 +3183,24 @@ pMaxTransactionSize =
, Opt.help "Maximum transaction size."
]

wordReader :: (Integral a, Bits a) => String -> ReadM a
wordReader typeString =
Opt.maybeReader parser
where
-- We use 'readMaybe' instead of 'read', because the latter wouldn't output a nice error message
parser s =
case readMaybe s >>= naturalToWordMaybe >>= toIntegralSized of
Nothing ->
error $ "Cannot parse " <> s <> " as a " <> typeString
Just a ->
Just a

word16Reader :: ReadM Word16
word16Reader = wordReader @Word16 "Word16"

pMaxBlockHeaderSize :: Parser Word16
pMaxBlockHeaderSize =
Opt.option Opt.auto $
Opt.option word16Reader $
mconcat
[ Opt.long "max-block-header-size"
, Opt.metavar "WORD16"
Expand Down

0 comments on commit 0789ab6

Please sign in to comment.