Skip to content

Commit a4eec9c

Browse files
Fix overflow when trading divisible against divisible
1 parent 33d1d44 commit a4eec9c

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

src/qt/metadexdialog.cpp

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "omnicore/tally.h"
2222
#include "omnicore/utilsbitcoin.h"
2323
#include "omnicore/wallettxs.h"
24-
24+
#include "omnicore/uint256_extensions.h"
2525
#include "amount.h"
2626
#include "sync.h"
2727
#include "uint256.h"
@@ -520,8 +520,7 @@ void MetaDExDialog::recalcTotal(bool useBuyFields)
520520
unsigned int propertyId = global_metadex_market;
521521
bool divisible = isPropertyDivisible(propertyId);
522522
bool testeco = isTestEcosystemProperty(propertyId);
523-
int64_t price = 0, amount = 0;
524-
int64_t totalPrice = 0;
523+
int64_t price = 0, amount = 0, totalPrice = 0;
525524

526525
if (useBuyFields) {
527526
amount = StrToInt64(ui->buyAmountLE->text().toStdString(),divisible);
@@ -530,14 +529,36 @@ void MetaDExDialog::recalcTotal(bool useBuyFields)
530529
amount = StrToInt64(ui->sellAmountLE->text().toStdString(),divisible);
531530
price = StrToInt64(ui->sellPriceLE->text().toStdString(),true);
532531
}
533-
totalPrice = amount * price;
534532

535-
// error and overflow detection
536-
if (0 >= amount || 0 >= price || totalPrice < amount || totalPrice < price || (totalPrice / amount != price)) {
537-
if (useBuyFields) { ui->buyTotalLabel->setText("N/A"); return; } else { ui->sellTotalLabel->setText("N/A"); return; }
533+
if (0 >= amount || 0 >= price) {
534+
if (useBuyFields) {
535+
ui->buyTotalLabel->setText("N/A");
536+
} else {
537+
ui->sellTotalLabel->setText("N/A");
538+
}
539+
return;
540+
}
541+
542+
uint256 amount256 = ConvertTo256(amount);
543+
uint256 price256 = ConvertTo256(price);
544+
uint256 totalPrice256 = amount256 * price256;
545+
if (divisible) totalPrice256 = totalPrice256 / COIN;
546+
547+
if (totalPrice256 > std::numeric_limits<int64_t>::max()) {
548+
QMessageBox::critical( this, "Unable to calculate total",
549+
"The total amount for the entered trade details would exceed the maximum amount of tokens." );
550+
if (useBuyFields) {
551+
ui->buyPriceLE->setText("");
552+
ui->buyTotalLabel->setText("N/A");
553+
} else {
554+
ui->sellPriceLE->setText("");
555+
ui->sellTotalLabel->setText("N/A");
556+
}
557+
return;
538558
}
539559

540-
if (divisible) totalPrice = totalPrice/COIN;
560+
totalPrice = ConvertTo64(totalPrice256);
561+
541562
QString totalLabel = QString::fromStdString(FormatDivisibleMP(totalPrice));
542563
if (testeco) {
543564
if (useBuyFields) { ui->buyTotalLabel->setText(totalLabel + " TOMNI"); } else { ui->sellTotalLabel->setText(totalLabel + " TOMNI"); }
@@ -596,13 +617,31 @@ void MetaDExDialog::sendTrade(bool sell)
596617
if(sell) {
597618
amountSell = StrToInt64(ui->sellAmountLE->text().toStdString(),divisible);
598619
price = StrToInt64(ui->sellPriceLE->text().toStdString(),true);
599-
if(divisible) { amountDes = (amountSell * price)/COIN; } else { amountDes = amountSell * price; }
620+
uint256 amountSell256 = ConvertTo256(amountSell);
621+
uint256 priceSell256 = ConvertTo256(price);
622+
uint256 totalPrice256 = amountSell256 * priceSell256;
623+
if (divisible) totalPrice256 = totalPrice256 / COIN;
624+
if (totalPrice256 > std::numeric_limits<int64_t>::max()) {
625+
QMessageBox::critical( this, "Unable to send MetaDEx trade",
626+
"The total amount for the entered trade details would exceed the maximum amount of tokens." );
627+
return;
628+
}
629+
amountDes = ConvertTo64(totalPrice256);
600630
if(testeco) { propertyIdDes = 2; } else { propertyIdDes = 1; }
601631
propertyIdSell = global_metadex_market;
602632
} else {
603633
amountDes = StrToInt64(ui->buyAmountLE->text().toStdString(),divisible);
604634
price = StrToInt64(ui->buyPriceLE->text().toStdString(),true);
605-
if(divisible) { amountSell = (amountDes * price)/COIN; } else { amountSell = amountDes * price; }
635+
uint256 amountDes256 = ConvertTo256(amountDes);
636+
uint256 priceDes256 = ConvertTo256(price);
637+
uint256 totalPrice256 = amountDes256 * priceDes256;
638+
if (divisible) totalPrice256 = totalPrice256 / COIN;
639+
if (totalPrice256 > std::numeric_limits<int64_t>::max()) {
640+
QMessageBox::critical( this, "Unable to send MetaDEx trade",
641+
"The total amount for the entered trade details would exceed the maximum amount of tokens." );
642+
return;
643+
}
644+
amountSell = ConvertTo64(totalPrice256);
606645
if(testeco) { propertyIdSell = 2; } else { propertyIdSell = 1; }
607646
propertyIdDes = global_metadex_market;
608647
}

0 commit comments

Comments
 (0)