Improve currency validation in signs (#3987)

Sanitise currency strings in more cases when handling signs. Should fix #3979.
This commit is contained in:
Josh Roy 2021-03-13 11:36:18 -05:00 committed by GitHub
parent 0ce4029483
commit 1cf2b11f1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 10 deletions

View file

@ -393,20 +393,20 @@ public class EssentialsSign {
protected final BigDecimal getMoney(final String line, final IEssentials ess) throws SignException { protected final BigDecimal getMoney(final String line, final IEssentials ess) throws SignException {
final boolean isMoney = line.matches("^[^0-9-\\.]?[\\.0-9]+[^0-9-\\.]?$"); final boolean isMoney = line.matches("^[^0-9-\\.]?[\\.0-9]+[^0-9-\\.]?$");
return isMoney ? getBigDecimalPositive(NumberUtil.sanitizeCurrencyString(line, ess)) : null; return isMoney ? getBigDecimalPositive(line, ess) : null;
} }
protected final BigDecimal getBigDecimalPositive(final String line) throws SignException { protected final BigDecimal getBigDecimalPositive(final String line, final IEssentials ess) throws SignException {
final BigDecimal quantity = getBigDecimal(line); final BigDecimal quantity = getBigDecimal(line, ess);
if (quantity.compareTo(MINTRANSACTION) < 0) { if (quantity.compareTo(MINTRANSACTION) < 0) {
throw new SignException(tl("moreThanZero")); throw new SignException(tl("moreThanZero"));
} }
return quantity; return quantity;
} }
protected final BigDecimal getBigDecimal(final String line) throws SignException { protected final BigDecimal getBigDecimal(final String line, final IEssentials ess) throws SignException {
try { try {
return new BigDecimal(line); return new BigDecimal(NumberUtil.sanitizeCurrencyString(line, ess));
} catch (final ArithmeticException | NumberFormatException ex) { } catch (final ArithmeticException | NumberFormatException ex) {
throw new SignException(ex.getMessage(), ex); throw new SignException(ex.getMessage(), ex);
} }

View file

@ -161,7 +161,7 @@ public class SignTrade extends EssentialsSign {
if (split.length == 2 && amountNeeded) { if (split.length == 2 && amountNeeded) {
final BigDecimal money = getMoney(split[0], ess); final BigDecimal money = getMoney(split[0], ess);
BigDecimal amount = getBigDecimalPositive(split[1]); BigDecimal amount = getBigDecimalPositive(split[1], ess);
if (money != null && amount != null) { if (money != null && amount != null) {
amount = amount.subtract(amount.remainder(money)); amount = amount.subtract(amount.remainder(money));
if (amount.compareTo(MINTRANSACTION) < 0 || money.compareTo(MINTRANSACTION) < 0) { if (amount.compareTo(MINTRANSACTION) < 0 || money.compareTo(MINTRANSACTION) < 0) {
@ -219,7 +219,7 @@ public class SignTrade extends EssentialsSign {
if (split.length == 2) { if (split.length == 2) {
try { try {
final BigDecimal money = getMoney(split[0], ess); final BigDecimal money = getMoney(split[0], ess);
final BigDecimal amount = notEmpty ? getBigDecimalPositive(split[1]) : getBigDecimal(split[1]); final BigDecimal amount = notEmpty ? getBigDecimalPositive(split[1], ess) : getBigDecimal(split[1], ess);
if (money != null && amount != null) { if (money != null && amount != null) {
return new Trade(amountType == AmountType.COST ? money : amount, ess); return new Trade(amountType == AmountType.COST ? money : amount, ess);
} }
@ -295,12 +295,12 @@ public class SignTrade extends EssentialsSign {
final String[] split = line.split("[ :]+"); final String[] split = line.split("[ :]+");
if (split.length == 2) { if (split.length == 2) {
final BigDecimal amount = getBigDecimal(split[1]).add(value); final BigDecimal amount = getBigDecimal(split[1], ess).add(value);
setAmount(sign, index, amount, ess); setAmount(sign, index, amount, ess);
return; return;
} }
if (split.length == 3) { if (split.length == 3) {
final BigDecimal amount = getBigDecimal(split[2]).add(value); final BigDecimal amount = getBigDecimal(split[2], ess).add(value);
setAmount(sign, index, amount, ess); setAmount(sign, index, amount, ess);
return; return;
} }
@ -318,7 +318,7 @@ public class SignTrade extends EssentialsSign {
if (split.length == 2) { if (split.length == 2) {
final BigDecimal money = getMoney(split[0], ess); final BigDecimal money = getMoney(split[0], ess);
final BigDecimal amount = getBigDecimal(split[1]); final BigDecimal amount = getBigDecimal(split[1], ess);
if (money != null && amount != null) { if (money != null && amount != null) {
final String newline = NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.shortCurrency(value, ess).substring(1); final String newline = NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.shortCurrency(value, ess).substring(1);
if (newline.length() > 15) { if (newline.length() > 15) {