Skip to content

Commit 7ebd44f

Browse files
authored
Restrict Value quantities to signed 128-bit integer range (#7389)
1 parent d605142 commit 7ebd44f

File tree

10 files changed

+424
-144
lines changed

10 files changed

+424
-144
lines changed

plutus-core/plutus-core/src/PlutusCore/Builtin/Result.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
-- editorconfig-checker-disable-file
2-
{-# LANGUAGE FlexibleInstances #-}
3-
{-# LANGUAGE FunctionalDependencies #-}
4-
{-# LANGUAGE LambdaCase #-}
5-
{-# LANGUAGE MultiParamTypeClasses #-}
6-
{-# LANGUAGE OverloadedStrings #-}
7-
{-# LANGUAGE RankNTypes #-}
8-
{-# LANGUAGE StrictData #-}
9-
{-# LANGUAGE TemplateHaskell #-}
2+
{-# LANGUAGE FlexibleInstances #-}
3+
{-# LANGUAGE LambdaCase #-}
4+
{-# LANGUAGE MultiParamTypeClasses #-}
5+
{-# LANGUAGE OverloadedStrings #-}
6+
{-# LANGUAGE RankNTypes #-}
7+
{-# LANGUAGE StrictData #-}
108

119
module PlutusCore.Builtin.Result
1210
( EvaluationError (..)

plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
20632063
(runCostingFunThreeArguments . unimplementedCostingFun)
20642064

20652065
toBuiltinMeaning _semvar UnionValue =
2066-
let unionValueDenotation :: Value -> Value -> Value
2066+
let unionValueDenotation :: Value -> Value -> BuiltinResult Value
20672067
unionValueDenotation = Value.unionValue
20682068
{-# INLINE unionValueDenotation #-}
20692069
in makeBuiltinMeaning

plutus-core/plutus-core/src/PlutusCore/Parser/Builtin.hs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module PlutusCore.Parser.Builtin where
55

66
import PlutusPrelude (Word8, reoption, void)
77

8+
import PlutusCore.Builtin.Result qualified
89
import PlutusCore.Crypto.BLS12_381.G1 qualified as BLS12_381.G1
910
import PlutusCore.Crypto.BLS12_381.G2 qualified as BLS12_381.G2
1011
import PlutusCore.Data
@@ -91,13 +92,22 @@ conArray uniA = Vector.fromList <$> conList uniA
9192
-- | Parser for values.
9293
conValue :: Parser PLC.Value
9394
conValue = do
94-
Value.fromList <$> (traverse validateKeys =<< conList knownUni)
95+
keys <- traverse validateKeys =<< conList knownUni
96+
case Value.fromList keys of
97+
PlutusCore.Builtin.Result.BuiltinSuccess v -> pure v
98+
PlutusCore.Builtin.Result.BuiltinSuccessWithLogs _logs v -> pure v
99+
PlutusCore.Builtin.Result.BuiltinFailure logs _trace ->
100+
fail $ "Failed to construct Value: " <> show logs
95101
where
96102
validateToken (token, amt) = do
97-
tk <- maybe (fail $ "Invalid token: " <> show (unpack token)) pure (Value.k token)
98-
pure (tk, amt)
103+
tk <- maybe (fail $ "Token name exceeds maximum length of 32 bytes: " <> show (unpack token))
104+
pure (Value.k token)
105+
qty <- maybe (fail $ "Token quantity out of signed 128-bit integer bounds: " <> show amt)
106+
pure (Value.quantity amt)
107+
pure (tk, qty)
99108
validateKeys (currency, tokens) = do
100-
ck <- maybe (fail $ "Invalid currency: " <> show (unpack currency)) pure (Value.k currency)
109+
ck <- maybe (fail $ "Currency symbol exceeds maximum length of 32 bytes: " <> show (unpack currency))
110+
pure (Value.k currency)
101111
tks <- traverse validateToken tokens
102112
pure (ck, tks)
103113

plutus-core/plutus-core/src/PlutusCore/Pretty/PrettyConst.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ instance NonDefaultPrettyBy ConstConfig T.Text where
113113
nonDefaultPrettyListBy conf = Prettyprinter.list . Prelude.map (nonDefaultPrettyBy conf)
114114
nonDefaultPrettyBy = inContextM $ \t -> pure $ pretty $ "\"" <> escape t <> "\""
115115
where
116-
escape t = T.foldr' prettyChar "" t
116+
escape = T.foldr' prettyChar ""
117117
prettyChar c acc
118118
| c == '"' = "\\\"" <> acc -- Not handled by 'showLitChar'
119119
| c == '\\' = "\\\\" <> acc -- Not handled by 'showLitChar'
@@ -162,6 +162,9 @@ instance PrettyBy ConstConfig Data where
162162
instance PrettyBy ConstConfig Value.K where
163163
prettyBy config = prettyBy config . Value.unK
164164

165+
instance PrettyBy ConstConfig Value.Quantity where
166+
prettyBy config = prettyBy config . Value.unQuantity
167+
165168
instance PrettyBy ConstConfig Value where
166169
prettyBy config = prettyBy config . Value.toList
167170

0 commit comments

Comments
 (0)