[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] [taler-bank] branch master updated: check if parsed numbers
From: |
gnunet |
Subject: |
[GNUnet-SVN] [taler-bank] branch master updated: check if parsed numbers do not exceed 2^53 - 1, as for the well known JavaScript limit. |
Date: |
Wed, 02 May 2018 15:15:24 +0200 |
This is an automated email from the git hooks/post-receive script.
marcello pushed a commit to branch master
in repository bank.
The following commit(s) were added to refs/heads/master by this push:
new 0ff63bf check if parsed numbers do not exceed 2^53 - 1, as for the
well known JavaScript limit.
0ff63bf is described below
commit 0ff63bf08ff3ffb1e8ad50fd110185e1d8b8f4ef
Author: Marcello Stanisci <address@hidden>
AuthorDate: Wed May 2 15:14:36 2018 +0200
check if parsed numbers do not exceed 2^53 - 1,
as for the well known JavaScript limit.
---
talerbank/app/amount.py | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/talerbank/app/amount.py b/talerbank/app/amount.py
index 814b9e0..022d108 100644
--- a/talerbank/app/amount.py
+++ b/talerbank/app/amount.py
@@ -30,6 +30,12 @@ class CurrencyMismatch(Exception):
"%s vs %s" % (curr1, curr2))
+class AmountOverflow(Exception):
+ hint = "Overflowing amount"
+ def __init__(self, part) -> None:
+ super(BadFormatAmount, self).__init__(
+ "This part exceedes 2^53 -1: " + part)
+
class BadFormatAmount(Exception):
hint = "Amount given was incorrect"
def __init__(self, faulty_str) -> None:
@@ -70,10 +76,33 @@ class Amount:
parsed = re.search(exp, amount_str)
if not parsed:
raise BadFormatAmount(amount_str)
+
+ # True if overflows.
+ 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)
# Comare two amounts, return:
--
To stop receiving notification emails like this one, please contact
address@hidden
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] [taler-bank] branch master updated: check if parsed numbers do not exceed 2^53 - 1, as for the well known JavaScript limit.,
gnunet <=