gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-taler-util] branch master updated: amount.py: merge


From: gnunet
Subject: [GNUnet-SVN] [taler-taler-util] branch master updated: amount.py: merge def check_overflow from bank.git
Date: Wed, 25 Sep 2019 16:49:09 +0200

This is an automated email from the git hooks/post-receive script.

ng0 pushed a commit to branch master
in repository taler-util.

The following commit(s) were added to refs/heads/master by this push:
     new 2827dea  amount.py: merge def check_overflow from bank.git
2827dea is described below

commit 2827dea755bc5ed97eff6f7dd598c40bd6baf211
Author: ng0 <address@hidden>
AuthorDate: Wed Sep 25 14:48:43 2019 +0000

    amount.py: merge def check_overflow from bank.git
---
 taler/util/amount.py | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/taler/util/amount.py b/taler/util/amount.py
index e017e67..9200a38 100644
--- a/taler/util/amount.py
+++ b/taler/util/amount.py
@@ -129,10 +129,37 @@ class Amount:
         parsed = re.search(exp, amount_str)
         if not parsed:
             raise BadFormatAmount(amount_str)
+
+        ##
+        # Checks if the input overflows.
+        #
+        # @param arg the input number to check.
+        # @return True if the overflow occurs, False otherwise.
+        def check_overflow(arg):
+            # Comes from 2^53 - 1
+            JAVASCRIPT_MAX_INT = "9007199254740991"
+            if len(JAVASCRIPT_MAX_INT) < len(arg):
+                return True
+            if len(JAVASCRIPT_MAX_INT) == len(arg):
+                # Assume current system can afford to store
+                # a number as big as JAVASCRIPT_MAX_INT.
+                tmp = int(arg)
+                tmp_js = int(JAVASCRIPT_MAX_INT)
+
+                if tmp > tmp_js - 1:  # - 1 leaves room for the fractional part
+                    return True
+            return False
+
+        if check_overflow(parsed.group(2)):
+            raise AmountOverflow("integer part")
+
         value = int(parsed.group(2))
         fraction = 0
         for i, digit in enumerate(parsed.group(3) or "0"):
             fraction += int(int(digit) * (Amount._fraction() / 10**(i + 1)))
+            if check_overflow(str(fraction)):
+                raise AmountOverflow("fraction")
+
         return cls(parsed.group(1), value, fraction)
 
     ##

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]